简体   繁体   English

应用引擎无法连接到Google云sql

[英]App engine fails to connect to Google cloud sql

My Google app engine application fails to connect to Google cloud sql instance with this error: 我的Google应用引擎应用无法通过此错误连接到Google云sql实例:

'PDOException' with message 'SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.

I've checked the documentation and followed instructions closely. 我检查了文档,并严格按照说明进行操作。 I added the app as an authorised application and set the root password using the cloud console. 我将应用程序添加为授权应用程序,并使用云控制台设置root密码。 I also gave the instace an ip address and can connect to it from Workbench on the local development machine. 我还给了instace一个ip地址,可以从本地开发机器上的Workbench连接到它。 Using workbench, I added a user and configured permissions. 使用工作台,我添加了用户和配置的权限。 But I'm still unable to connect to it from the dev version of the app (same ip as workbench) or the deployed app. 但我仍然无法从应用程序的开发版本(与工作台相同的ip)或已部署的应用程序连接到它。

Here's my connection line: 这是我的连接线:

$conn = new PDO('mysql:unix_socket=/cloudsql/****:****;charset=utf8', 'the_username', 'its_password');

this question appears to be similar to this problem. 这个问题似乎与这个问题相似。 However, even after setting up a new user and granting permissions I'm still unable to connect. 但是,即使在设置新用户并授予权限后,我仍然无法连接。 Any idea where I went wrong? 知道我哪里错了吗?

Thanks 谢谢

Note that setting the root passwords only applies the connections coming over IP. 请注意,设置root密码仅应用来自IP的连接。 The connections from App Engines show to the MySQL server as coming from localhost. App Engines的连接显示MySQL服务器来自localhost。 So make sure the password for 'root@localhost' is the one you configured in the code. 因此,请确保“root @ localhost”的密码是您在代码中配置的密码。

A quick way to check the state of the passwords is to connect over IP and issue the following query: 检查密码状态的快速方法是通过IP连接并发出以下查询:

mysql> SELECT user,host,password FROM mysql.user;
+-------+-----------+-------------------------------------------+
| user  | host      | password                                  |
+-------+-----------+-------------------------------------------+
| root  | localhost |                                           |
| root  | 127.0.0.1 |                                           |
| root  | ::1       |                                           |
|       | localhost |                                           |
| root  | %         | *3D56A309CD04FA2EEF181462E59011F075C89548 |
| admin | 127.0.0.1 |                                           |
+-------+-----------+-------------------------------------------+
6 rows in set (0.11 sec)

mysql> SELECT password('xxx');
+-------------------------------------------+
| password('xxx')                           |
+-------------------------------------------+
| *3D56A309CD04FA2EEF181462E59011F075C89548 |
+-------------------------------------------+
1 row in set (0.11 sec)

mysql>

The above shows how an instance shows up after setting the root password for connections over IP ('root@%') to 'xxx'. 上面显示了在通过IP('root @%')到'xxx'的连接设置root密码后如何显示实例。 Note that the password for 'root@localhost' is blank so connections from App Engine still do not require any passwords. 请注意,“root @ localhost”的密码为空,因此App Engine的连接仍然不需要任何密码。

When connecting to Cloud SQL from an authorized App Engine application, the password is not required (actually it will fail if you try to connect with password). 从授权的App Engine应用程序连接到Cloud SQL时,不需要密码(实际上,如果您尝试使用密码连接,则会失败)。

Change your connection string to jdbc:google:mysql:///? 将您的连接字符串更改为jdbc:google:mysql:///? user=root omitting the &password= part user = root省略&password = part

One thing I tried was trying to connect to my application via a MySQL client directly to my proxy to see if my code was the problem: See: Connecting mysql Client Using the Cloud SQL Proxy 我尝试过的一件事是尝试通过MySQL客户端直接连接到我的应用程序到我的代理,看看我的代码是否是问题:请参阅: 使用Cloud SQL Proxy连接mysql客户端

That still didn't work for me, which lead me to believe it may be something with the actual network connection. 这仍然不适合我,这让我相信它可能与实际的网络连接有关。 I noticed the Cloud SQL proxy I was running locally did return the right instance IP address (the error looked something like this: 我注意到我在本地运行的Cloud SQL代理确实返回了正确的实例IP地址(错误看起来像这样:

couldn't connect to "{connectionName}": dial tcp {ip address}:3307: getsockopt: operation timed out

You can confirm the IP Address returned there matches with the one in the Cloud SQL Console. 您可以确认返回的IP地址与Cloud SQL Console中的IP地址匹配。

I then tried connecting via a different internet connection and was able to connect. 然后我尝试通过不同的互联网连接进行连接并且能够连接。 Turns out it was the firewall in the original connection that was not letting the connection happen! 原来是原始连接中的防火墙没有让连接发生! Double check any security settings in place that may prevent the connection from going through. 仔细检查可能阻止连接通过的任何安全设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM