简体   繁体   中英

PHP times out connecting to Cloud SQL instance

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM