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;