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

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

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动