首页 > 解决方案 > 将 Google App Engine Flexible 中的简单节点应用程序连接到 Google Cloud SQL

问题描述

我使用 postGraphile 作为 GraphQL 服务器 - 在本地工作得很好。

试图将其推送到 App Engine 实例中。

我一生都无法弄清楚如何让 App Engine 连接到 Cloud SQL。我可以从我的计算机直接连接到 Cloud SQL(我被列入白名单),我什至尝试在列出应用引擎实例的 IP 时无效

这是我的app.yaml设置:

# per google's instructions, I'vd added the instance here, and now added a tcp port
beta_settings:
  cloud_sql_instances: webstr-dev-237715:us-central1:webstr-dev=tcp:5432

# [START runtime]
runtime: nodejs
env: flex
threadsafe: yes
service: wgraphile

在我的package.json中,我使用连接参数运行 postgragphile。如果我运行此连接字符串:

postgraphile -o -c postgres://webstr:[SECRET]@localhost:5432/str_dev

我收到连接被拒绝错误:

A serious error occurred when building the initial schema. Exiting because `retryOnInitFail` is not set. Error details:

Error: connect ECONNREFUSED 127.0.0.1:5432
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)

如果我用这个连接字符串运行它:

postgraphile -o -c postgres://webstr:[SECRET]@172.17.0.1:5432/str_dev

我收到此连接重置错误:

Postgres connection: postgres://webstr:[SECRET]@172.17.0.1/str_dev
  ‣ Postgres schema(s):  public


A serious error occurred when building the initial schema. Exiting because `retryOnInitFail` is not set. Error details:

Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:111:27)

我也尝试过使用带有直接 IP 的 unix 套接字连接字符串,例如:

postgres://webstr:[SECRET]@35.202.32.69:5432/str_dev?unix_sock=/cloudsql/<cloud_sql_instance_name>/.s.PGSQL.5432

帮助?

标签: google-app-enginegoogle-cloud-sql

解决方案


您似乎正在使用 App Engine Flex。如果您查看“从 App Engine 连接”页面,则需要检查以下内容:

  1. 您需要确保已启用 Cloud SQL Admin API
  2. 您需要确保 App Engine 服务帐号(默认为[YOUR_PROJECT_NUMBER]@gae-api-prod.google.com.iam.gserviceaccount.com)具有Cloud SQL ClientIAM 权限

然后,您app.yml需要以下内容(看起来就像您正在做的那样):

beta_settings:
  cloud_sql_instances: <INSTANCE_CONNECTION_NAME>=tcp:<PORT>

最后,完成此操作后,您应该可以在172.17.0.1:<PORT>.


推荐阅读