doudou3935 2013-11-03 09:16
浏览 52
已采纳

Google App Engine上的CloudSQL PDO(unix_socket)问题

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;
  • 写回答

1条回答 默认 最新

  • dtqjbbr5283 2013-11-04 01:22
    关注

    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'".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)