将 Google App Engine Flexible 中的简单 Node 应用程序连接到 Google Cloud SQL

Connect simple Node app in Google App Engine Flexible to Google Cloud SQL

我将 postGraphile 用作 GraphQL 服务器 - 在本地运行良好。

正在尝试将其推送到 App Engine 实例中。

我这辈子都想不出如何让 App Engine 连接到云端 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

帮忙?

您似乎在使用 App Engine Flex。如果您检查 Connecting from App Engine page,则需要检查以下内容:

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

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

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

最后,完成后,您应该可以在 172.17.0.1:<PORT> 连接。

好的,好的!

我只是想将 postGraphile 部署到云中,只需要命令行参数即可在我托管在 Google 云 SQL 中的 psql 和 Angular 应用托管在 Google App Engine 中。

过去,我使用 Google App Engine 来托管中间层。经过多次努力,我发现我需要使用 cloud_sql_instances 连接到 psql 实例,并且我需要在命令行中设置主机和端口。

确保您的 Cloud SQL postgres database 项目已打开,Google Cloud Admin SQL 已打开,并且 App Engine 已打开。无论您的前端是什么,都在 App Engine 中保留默认服务。

这是我的 app.yaml 部署文件:

beta_settings:
  cloud_sql_instances: webstr-dev-######:us-central1:webstr-dev=tcp:5432

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

manual_scaling:
  instances: 1
resources:
 cpu: .5
 memory_gb: .5
 disk_size_gb: 10

health_check:
  enable_health_check: False

# [END runtime]


handlers:
  - url: /(.*)
    static_files: ./
    upload: ./(.*)

#  settings to keep gcloud from uploading files not required for deployment
skip_files:
  - ^node_modules$
  - ^README\..*
  - ^package-lock.json
  - \.gitignore
  - \.es*
  - ^\.git$
  - ^errors\.log

在beta_set

之下
  • tings,cloud_sql_instances: webstr-dev-######:us-central1:webstr-dev=tcp:5432 告诉我们,在本例中 'webstr-dev-######' in central region 1,我们正在 gcp 项目中打开一个到云实例的 unix 管道,连接到云 sql实例websr-dev.
  • =tcp:5432 将该 unix 套接字映射到 tcp 端口 5432。
  • 我无法直接使用 unix 端口,这就是 tcp 端口片在那里的原因
  • 您可以从您的 Cloud SQL 实例中的 Cloud SQL 界面的此区域中获取完整的实例名称:

在我的 package.json 中,我指定了 postgraphile、一些项目详细信息和启动脚本。简称:

{
  "name": "myprojectname",
  "version": "1.0.0",
  "scripts": {
    "start": "postgraphile --port 8080 -o --watch --enhance-graphiql -o -q / --host 0.0.0.0 -c postgres://user:password@172.17.0.1:5432/str_dev",

  },
  "engines": {
    "node": "^10.15",
    "npm": "^6.9"
  },
  "license": "ISC",
  "dependencies": {
    "postgraphile": "^4.4.3"
  }
}

该项目将在 https://[project-name].appspot.com/ 结束 - 您的 graphql 端点将是 url。 - 您可以访问 grap

hiql 在 https://[project-name].appspot.com/graphiql

关于启动命令,优点是: - --port 8080 Google 云会将端口 8080 映射到您正在创建的服务的名称,因此您不需要像我们在本地 postgraphile 上那样指定 :5000,只需指定终点 urlhttps://[project-name].appspot.com/ - -q / 开关将 graphql 端点置于服务本身的名称处 - --host 0.0.0.0 允许GAE的nginx成功绑定服务 - -o 规避烦人的 CORS 废话 - --watch 让 postgraphile 跟上您的数据库更改

DEPLOYING: I deployed the project with gcloud. I used gcloud init to setup my connection to my google cloud project. Then I used gcloud app deploy in this directory to push it up.