Home:ALL Converter>Connecting to Google Cloud SQL

Connecting to Google Cloud SQL

Ask Time:2014-03-13T01:55:13         Author:user2727128

Json Formatter

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)'

Author:user2727128,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/22359910/connecting-to-google-cloud-sql
yy