Mysql – Google Cloud SQL – Lost connection and Timeout errors

google-cloud-platformgoogle-cloud-sqlMySQL

I keep getting the following errors when attempting a simple connection to a Google Cloud SQL database instance from a PHP script.

Lost connection to MySQL server at 'reading initial communication packet', system error: 0(2013)

and

Warning: mysqli_connect(): (HY000/2002): Connection timed out in .../users_log_in_db_connect.php on line 18

I'm aware the following errors may have already been covered but having attempted most suggestions as per related posts below I'm still unsuccessful in solving them:

LINK 1

LINK 2

For example when I set my server to localhost or 127.0.0.1 I get the following error:

 DB connection failed: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)(2002)

For the log in details I'm using my server value is the Instance IP address provided in the Google Developers Console.

I'm being driven crazy by these errors and I know I'm missing out on a really simple thing. Where could I be going wrong?

I really appreciate any suggestions.

Best Answer

This happens because the IP you are connecting from isn't authorized in the console, as stated in the docs:

If Google Cloud SQL rejects the connection, for example, because the IP address your client is connecting from is not authorized, the error you receive will be: ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

See the "configuring access control for IP connections" section on the following page:

Related Topic