无法从 GCE (Google Compute Engine) 连接 Google Cloud SQL(2nd)

Can't connect Google Cloud SQL(2nd) from GCE (Google Compute Engine)

即使我将我的 GCE 实例的 public IP(外部 IP)添加为授权网络,我也无法从 GCE 连接 Google Cloud SQL。当我将“0.0.0.0”添加到授权网络时它会起作用。显然我不想那样做。授权的网络设置可能是原因。但我找不到它。有谁知道这件事。

我正在使用 Google Cloud SQL 版本 2 测试版。我正在尝试从 GCP 云控制台进行连接。虽然可能没有必要,但我将外部 IP 设置从临时更改为静态,但它没有用。

mysql -u root -p -h xxxx <--- I can login normally if I add "0.0.0.0" into authorized network. 

我已经仔细检查了同一个问题..

1. Ensure your Cloud SQL instance has an IPv4 address.
2. Find out the public IP address of your GCE instance and add it as an authorized network on your Cloud SQL instance.
3. Add a MySQL username and password for your instance with remote access.
4. When connecting from GCE use you standard MySQL connection system (e.g. mysqli_connect) with the username and password you just set up, connecting to the IPv4 address of your Cloud SQL instance.

编辑 1

我注意到了这个描述。

Note: Connecting to Cloud SQL from Compute Engine using the Cloud SQL Proxy is currently available only for Cloud SQL Second Generation instances.

https://cloud.google.com/sql/docs/compute-engine-access

这是否意味着我必须使用代理..?


编辑 2

$ mysql -u root -p -h (Cloud SQL Instance's IP)
Enter password: 
ERROR 2003 (HY000): Can't connect to MySQL server on '(Cloud SQL Instance's IP)' (110)

编辑 3

Does it mean that I have to use the Proxy..?

根据 Vadim 所说的官方文档,Cloud SQL Proxy 似乎是可选的,但它在安全性、灵活性和价格方面听起来更好。 (静态IP会收费。不过代理设置对我来说可能比较复杂..)

https://cloud.google.com/sql/docs/compute-engine-access

If you are connecting to a Cloud SQL First Generation instance, then you must use its IP address to connect. However, if you are using a Cloud SQL Second Generation instance, you can also use the Cloud SQL Proxy or the Cloud SQL Proxy Docker image.


编辑 4

我找到了原因...我很愚蠢...我尝试从 Google 云 Shell 连接,但那不是我的 gce 实例。当我尝试从我的 gce 实例连接时它起作用了。

您是否在授权网络下添加了 GCE VM 的 public IP?

来自你的post:

2. Find out the public IP address of your GCE instance and add it as an   authorized network on your Cloud SQL instance.

官方文档在这里: https://cloud.google.com/sql/docs/external#appaccessIP