无法通过云 SQL 代理从云 Shell 通过代理连接

Cannot Connect by Cloud SQL Proxy from Cloud Shell By Proxy

我正在关注 GAE 的 Django 示例,但在通过代理从 Google 云 Shell 连接到云 SQL 实例时遇到问题。可能与权限设置有关,因为我看到请求未授权,

其他上下文,

  1. "gcloud beta sql connect auth-instance --user=root"连接没问题

  2. 我有一个 SQL 代理客户端的服务帐户。

我可能漏掉了什么。有人可以阐明一下吗?提前致谢。

提前致谢。


代理日志:

./cloud_sql_proxy -instances=auth-158903:asia-east1:auth-instance=tcp:3306

2017/02/17 14:00:59 Listening on 127.0.0.1:3306 for auth-158903:asia-east1:auth-instance
2017/02/17 14:00:59 Ready for new connections
2017/02/17 14:01:07 New connection for "auth-158903:asia-east1:auth-instance"
2017/02/17 14:03:16 couldn't connect to "auth-158903:asia-east1:auth-instance": dial tcp 107.167.191.26:3307: getsockopt: connection timed out

客户端日志:

mysql -u root -p --host 127.0.0.1
Enter password: 
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

我也尝试使用凭证文件,但还是不行,

./cloud_sql_proxy -instances=auth-158903:asia-east1:auth-instance=tcp:3306 -credential_file=Auth-2eede8ae0d0b.jason

2017/02/17 14:21:36 using credential file for authentication; email=sql-proxy-client@auth-158903.iam.gserviceaccount.com
2017/02/17 14:21:36 Listening on 127.0.0.1:3306 for auth-158903:asia-east1:auth-instance
2017/02/17 14:21:36 Ready for new connections
2017/02/17 14:21:46 New connection for "auth-158903:asia-east1:auth-instance"
2017/02/17 14:21:48 couldn't connect to "auth-158903:asia-east1:auth-instance": ensure that the account has access to "auth-158903:asia-east1:auth-instance" (and make sure there's no typo in that name). Error during get instance auth-158903:asia-east1:auth-instance: googleapi:     **Error 403: The client is not authorized to make this request., notAuthorized**

如果我只提供我的服务帐户 "Cloud SQL Client" IAM 角色,我就可以准确地重现这个问题。当我也为我的服务帐户提供 "Cloud SQL Viewer" 角色时,它就可以连接了。我建议您尝试一下,看看是否有帮助。

云 SQL 代理使用端口 3307 而不是更常见的 MySQL 端口 3306。这是因为它以不同的方式使用 TLS 并且具有不同的 IP ACL。因此,允许 MySQL 流量的防火墙默认不允许云 SQL 代理。

看看您的网络上是否有阻止端口 3307 的防火墙。要使用 Cloud SQL 代理,请授权此端口用于出站连接。

看起来像是网络连接问题。 如果您使用私有 IP,请仔细阅读此内容: https://cloud.google.com/sql/docs/mysql/private-ip

请注意,云 SQL 实例位于 Google 托管网络中,代理旨在用于简化 VPC 网络中与数据库的连接。

简而言之:运行 cloud-sql-proxy 来自本地机器将无法工作,因为它不在 VPC 网络中。它应该在连接到与数据库相同的 VPC 的 Compute Engine VM 上工作。

我通常做的解决方法是从本地计算机使用 gcloud ssh 并通过计算引擎中的小型 VM 进行端口转发,例如:

gcloud beta compute ssh --zone "europe-north1-b" "instance-1" --project "my-project" -- -L 3306:cloud_sql_server_ip:3306

然后您可以连接到 localhost:3306(确保没有其他任何东西 运行 或将第一个端口号更改为本地可用的端口号)