5

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 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for your quick response. That is what I initially tried but still gives me the same error. Host: instance-ip, User: root, Pass: password I set. Any ideas why this would give that error if I submitted the Public IP of the site?
Very strange, if you've opened up the network to connect from your IP, it should just work. Could you show the PDO connection string you're using? (please obfuscate the IP and password, I just want to see the general structure)
Your edit sure sounds like a firewall problem. Are you sure you opened your IP range for access to the database? (ie step 1 at the link in the answer)
I didn't set the range - just the public IP found when I ping my website. Should I set it as something else?
@user2727128 You should set the IP you're trying to connect from, ie the machine where you're running the client, otherwise CloudSQL will not allow the connection.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.