CloudSQL PDO(unix_socket) problems on Google App Engine
Asked Answered
P

1

2

I'm trying to connect to my CloudSQL instance FROM App Engine. The way I've set it up is to use a static IP, which I can use to connect to it from outside App Engine (namely, on my development environment). However, when the app runs within GAE, the only way to connect to the database is using unix_socket. The only documentation I've found on this is Google's own docs.

According to the documentation, this should be enough to connect:

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

I am able to connect to the database using the root password I've created through the CloudSQL console, as well as using the additional users I created through a local MySQL client. SO... if I try to connect on GAE using the IP, it fails (as documented). But if I try using unix_socket=/cloudsql/..., I get the following error:

SQLSTATE[HY000] [1045] Access denied for user 'bookie'@'localhost' (using password: YES)

(where the username is bookie, of course).

If I include a host setting in the PDO initialization string, such as:

$db = new PDO('mysql:host=123.456.789.101;unix_socket=/cloudsql/...');

I get this error instead:

SQLSTATE[HY000] [2002] Unable to find the socket transport "tcp" - did you forget to enable it when you configured PHP?

Enlighten me, if you will...


Update & Solution

As mentioned by Razvan, the problem was that both root as well as any other user I had created were not granted any privileges on localhost. root had privileges on localhost, but it didn't have a password set for it there.

Two solutions:

1) Connect with no password for root:

$db = new PDO('mysql:unix_socket=...', 'root', '');

2) Fix the permissions for other users using google_sql.py (or however you can connect to some SQL/MySQL client):

GRANT ALL ON `localhost`.* to bookie;
Purvey answered 3/11, 2013 at 9:16 Comment(3)
If you use user 'root' with no password does is connect?Rhyton
I'll try it out. Since I did set a password for root through the cloudsql console, I'm not sure it'll work. However, since I haven't tried this yet, it's worth a try.Purvey
Yep, this did in fact work. Since I had set a password for root through cloudsql console, I was confused why this would work... so going back to the database through SQLCmd, I realized that the privileges of my users were not set on localhost. That did the trick. Thanks!Purvey
M
3

The connections from App Engine will show in Cloud SQL with host = 'localhost'. So make sure the accounts on the mysql side are in agreement with the ones you are trying to connect with.

Note: you can the permissions for a particular user using a query like "SHOW GRANTS FOR 'bookie'@'localhost'".

Makhachkala answered 4/11, 2013 at 1:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.