4
votes

I have set up a Google Cloud SQL which is functioning perfectly however I'm having trouble connecting using any of the methods given in the documentation:

PDO

$db = new PDO('mysql:unix_socket=/cloudsql/hello-php:my-cloudsql-instance;charset=utf8','<username>','<password>');

mysql_connect

$conn = mysql_connect(':/cloudsql/hello-php:my-cloudsql-instance', '<username>', '<password>');

mysqli

$sql = new mysqli(null, '<username>', '<password>', null, null, '/cloudsql/hello-php:my-cloudsql-instance');

I can connect remotely from MySQL workbench but whenever I attempt from the website using the above functions I get a "Lost connection to MySQL server at 'reading initial communication packet'" - yet I granted the Cloud SQL access through authorized networks and checked the IP given. I believe it has something to do with the fact that I'm not trying to connect from a Google App Engine but from my own development database. How do I connect from outside of Google!?

UPDATE: I've verified my IP every which way but this code is still not working.

$host="173.xxx.xx.xxx" <-- given from Google Developers COnsole
$db_username="root";
$db_pasword="xxxx"; <-- password I set after creating the instance

mysql_connect("$host", "$db_username", "$db_password")or die("cannot connect");

This page gives the error:

Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 111 in /..../connect.php on line 5 cannot connect

When I try this PDO connection I also get an error:

$dsn = 'mysql:host=173.xxx.xx.xxx;dbname=database_name';
$username = 'root';
$password = 'xxxx';

$dbh = new PDO($dsn, $username, $password);

Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2003] Can't connect to MySQL server on '173.xxx.xx.xxx' (111)'

1

1 Answers

4
votes

You can't connect remotely using a unix socket (which works only on a local machine), you'll need to connect over the network.

Google has a brief description what you need to do to make that happen, but basically it boils down to that you need to authorize the IP number/range you want to connect from, and connect to the database using the public IP of the instance.