NodeJs:Unable 从 Google App Engine 连接 Google Cloud SQL
NodeJs:Unable to connect Google Cloud SQL from Google App Engine
我从事 App Engine 和 Cloud SQL 已有一段时间了。
最近我 禁用 public 访问我的数据库 。从那时起,我无法使用我的 App Engine 连接到 CloudSQL(App Engine 和 Cloud SQL 都在同一个 Google Cloud 项目中)。
我能够连接的唯一方法是将云上的 App Engine IP 列入白名单SQL。但是在新的部署中 IP 会发生变化,因此添加静态 IP 并不是一个理想的解决方案。
在 Cloud SQL 连接选项卡中显示以下内容:
App Engine authorization
All apps in this project are authorized by default. To authorize apps in other projects, follow the steps below.
Apps in this project: All authorized.
Which is not true in this case.
Cloud SQL API 和 Cloud SQL Admin API 已启用。
用谷歌搜索问题并按照文档和其他 Whosebug 问题中的步骤进行操作:
1)
2)
3) "PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR" in Node-MySqL
4) Error: Cannot enqueue Query after fatal error in mysql node
5) not able to connect google cloud sql from google app engine
所有这些都没有太大帮助。
connection = mysql.createConnection({
host : 'INSTANCE_CONNECTION_NAME',
user : 'db_user',
password : 'db_password',
database : 'db_name'
});
预期: Get a Connection.
实际: getaddrinfo ENOTFOUND INSTANCE_CONNECTION_NAME INSTANCE_CONNECTION_NAME:3306
不确定为什么需要 INSTANCE_CONNECTION_NAME
两次。
我尝试复制您的 use-case 场景,以下步骤对我有用。
您的 Cloud SQL 实例没有 public IP,然后要从 App Engine 连接到它,您需要为这两种服务使用相同的 VPN 网络。因此,您必须编辑您的 Cloud SQL 实例以指示您将要使用的网络并编辑 app.yaml 配置文件。
同样重要的是,这两项服务都将 运行 在同一地区。
在 app.yaml
配置文件中,您必须添加以下标签:
network:
name: [YOUR_NETWORK_NAME]
subnetwork_name: [YOUR_SUBNETWORK_NAME]
并使用数据库环境变量:
env_variables:
SQL_USER: [YOUR_USER]
SQL_PASSWORD: [YOUR_PASSWORD]
SQL_DATABASE: [YOUR_DATABASE-NAME]
# e.g. my-awesome-project:us-central1:my-cloud-sql-instance
INSTANCE_CONNECTION_NAME:<INSTANCE_CONNECTION_NAME>
# [END gae_flex_mysql_env]
最后是以下几行:
beta_settings:
cloud_sql_instances: <INSTANCE_CONNECTION_NAME>
请记住,您必须将“<>”或“[]”中的内容替换为您需要的数据。
有关更多信息,您可以查看此文档
Connecting from App Engine.
如果我理解你的问题,请让我说明一下。
您拥有 public IP,但已将其设置为仅供项目中的应用程序访问。如果是这种情况,并且考虑到您使用的是灵活的环境,我发现以下方法很有用
1)建立连接时,将Host写成172.17.0.6
2) 并在 app.yaml 中指定 INSTANCE_CONNECTION_NAME,如实例概述页面中所述。
3) 部署并 运行。
这将有效,因为使用 Docker 构建了灵活的 NodeJs 环境。并且默认使用的DB容器在172.17.0.6
,这里访问实例连接名,在App Engine
和CloudSQL DB
之间建立连接。
我从事 App Engine 和 Cloud SQL 已有一段时间了。
最近我 禁用 public 访问我的数据库 。从那时起,我无法使用我的 App Engine 连接到 CloudSQL(App Engine 和 Cloud SQL 都在同一个 Google Cloud 项目中)。
我能够连接的唯一方法是将云上的 App Engine IP 列入白名单SQL。但是在新的部署中 IP 会发生变化,因此添加静态 IP 并不是一个理想的解决方案。
在 Cloud SQL 连接选项卡中显示以下内容:
App Engine authorization
All apps in this project are authorized by default. To authorize apps in other projects, follow the steps below.Apps in this project: All authorized. Which is not true in this case.
Cloud SQL API 和 Cloud SQL Admin API 已启用。
用谷歌搜索问题并按照文档和其他 Whosebug 问题中的步骤进行操作:
1)
2)
3) "PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR" in Node-MySqL
4) Error: Cannot enqueue Query after fatal error in mysql node
5) not able to connect google cloud sql from google app engine
所有这些都没有太大帮助。
connection = mysql.createConnection({ host : 'INSTANCE_CONNECTION_NAME', user : 'db_user', password : 'db_password', database : 'db_name' });
预期: Get a Connection.
实际: getaddrinfo ENOTFOUND INSTANCE_CONNECTION_NAME INSTANCE_CONNECTION_NAME:3306
不确定为什么需要 INSTANCE_CONNECTION_NAME
两次。
我尝试复制您的 use-case 场景,以下步骤对我有用。 您的 Cloud SQL 实例没有 public IP,然后要从 App Engine 连接到它,您需要为这两种服务使用相同的 VPN 网络。因此,您必须编辑您的 Cloud SQL 实例以指示您将要使用的网络并编辑 app.yaml 配置文件。 同样重要的是,这两项服务都将 运行 在同一地区。
在 app.yaml
配置文件中,您必须添加以下标签:
network:
name: [YOUR_NETWORK_NAME]
subnetwork_name: [YOUR_SUBNETWORK_NAME]
并使用数据库环境变量:
env_variables:
SQL_USER: [YOUR_USER]
SQL_PASSWORD: [YOUR_PASSWORD]
SQL_DATABASE: [YOUR_DATABASE-NAME]
# e.g. my-awesome-project:us-central1:my-cloud-sql-instance
INSTANCE_CONNECTION_NAME:<INSTANCE_CONNECTION_NAME>
# [END gae_flex_mysql_env]
最后是以下几行:
beta_settings:
cloud_sql_instances: <INSTANCE_CONNECTION_NAME>
请记住,您必须将“<>”或“[]”中的内容替换为您需要的数据。
有关更多信息,您可以查看此文档 Connecting from App Engine.
如果我理解你的问题,请让我说明一下。
您拥有 public IP,但已将其设置为仅供项目中的应用程序访问。如果是这种情况,并且考虑到您使用的是灵活的环境,我发现以下方法很有用
1)建立连接时,将Host写成172.17.0.6
2) 并在 app.yaml 中指定 INSTANCE_CONNECTION_NAME,如实例概述页面中所述。
3) 部署并 运行。
这将有效,因为使用 Docker 构建了灵活的 NodeJs 环境。并且默认使用的DB容器在172.17.0.6
,这里访问实例连接名,在App Engine
和CloudSQL DB
之间建立连接。