0
votes

Here is my code for PHP for connecting to the database, a Cloud SQL instance for MySQL:

define('DB_SERVER', '192.0.0.1');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'db');

$db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME) or die(mysqli_connect_error());

Obviously the DB_SERVER is not the actual IP address, nor are the credentials, but I did put in a dummy one to see if I needed to add anything like cloudsql:/ or jdbc:// like you do in Java.

I did authorize the IP address for the webserver on Cloud SQL, but all it returns is a Connection Timed Out error; What can I do to fix this?

I'm certain that the credentials are correct, and that I can connect from other authorized IP addresses, as I have been able to access from another account & multiple IP addresses through the MySQL workbench.

1
Can you connect to the server remotely from mysql terminal? mysql -u username -h 192.0.0.1 -ppassword -D db -P {YOUR SERVER PORT} ?Karlom
@Karlom It just kind of hangs via the Google Cloud Terminal. I ran mysql -h 192.0.0.1:3306 -u user -p db too, just kind of hangs. Weird thing is, MySQL Workbench works fine for me.Dragonsnap
How did you proxy your Cloud SQL ?Le Khiem
@LeKhiem I didn't use proxies, I used the public IP to connect to the SQL database.Dragonsnap

1 Answers

0
votes

Solved:

For anyone using Siteground's services, make sure to add 0.0.0.0/0 as the accepted IP addresses if you're using public IP authorization. Then find what IP's are connected when you refresh the php page by running this right after the page is refreshed:

SELECT host FROM information_schema.processlist WHERE ID=connection_id();

It should give you multiple IPs, and check them online using an IP locator to see if they match the location of the Siteground's services. Remove 0.0.0.0/0 and add that IP address to Google Cloud SQL.