google-app-engine - 在 Google Cloud Build 过程中连接到数据库时出现问题

标签 google-app-engine google-cloud-storage google-cloud-build

类似于this issue. (我将在下面更详细地描述该链接)

问题: 运行 Google Cloud Build 时,我收到一条错误消息:

django.db.utils.OperationalError: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/cloudsql/sample-kubernetes-268320:us-west1:cloud-build-staging/.s.PGSQL.3306"

我正在遵循解决方案posted here由合作者。他们提供了一个示例 cloudbuild.yaml,我密切关注,但没有任何运气。

正在运行的cloudbuild.yaml

steps:
  - id: proxy-install
    name: alpine:3.10
    entrypoint: sh
    args:
      - -c
      - 'wget -O /workspace/cloud_sql_proxy https://storage.googleapis.com/cloudsql-proxy/v1.16/cloud_sql_proxy.linux.386 &&  chmod +x /workspace/cloud_sql_proxy'
    waitFor: ['-']
  - id: execute-with-proxy
    name: python:3.7
    timeout: 100s
    entrypoint: sh
    args:
      - -c
      - '(/workspace/cloud_sql_proxy -dir=/workspace -instances=[INSTANCE_CONNECTION_NAME] & sleep 2) && (pip install -r requirements.txt && python test_sql.py)'
    waitFor: ['proxy-install']

我的cloudbuild.yaml

steps:
  - id: proxy-install
    name: alpine:3.10
    entrypoint: sh
    args:
      - -c
      - 'wget -O /workspace/cloud_sql_proxy https://storage.googleapis.com/cloudsql-proxy/v1.16/cloud_sql_proxy.linux.386 &&  chmod +x /workspace/cloud_sql_proxy'
    waitFor: ['-']
  - id: Test
    name: 'python:3.7.6-buster'
    env:
      - "CLOUDBUILD_TEST=True"
    timeout: 100s
    entrypoint: /bin/sh
    args:
      - -c
      - '(/workspace/cloud_sql_proxy -dir=/workspace -instances=sample-kubernetes-268320:us-west1:cloud-build-staging & sleep 2) && (pip install -r requirements.txt && cd fastestfollowup && python3 manage.py test)'
    waitFor: ['proxy-install']

我已采取的调试步骤:

  1. 我已将 Cloud Build Service 添加为 Cloud SQL 管理员
  2. 我通过使用 gcloud sqlinstances describe cloud-build-staging 并复制“connectionname”来确保我的实例名称正确

编辑: 我将 cloudbuild.yaml 文件更改为

    args:
      - -c
      - '(/workspace/cloud_sql_proxy -dir=/workspace -instances=sample-kubernetes-268320:us-west1:cloud-build-staging & sleep 2) && (pip install -r requirements.txt && cd fastestfollowup && python3 manage.py test)'

至:

    args:
      - -c
      - '(/workspace/cloud_sql_proxy -dir=/cloudsql -instances=sample-kubernetes-268320:us-west1:cloud-build-staging & sleep 2) && (pip install -r requirements.txt && cd fastestfollowup && python3 manage.py test)'

没有效果。

最佳答案

看起来您使用 -dir=/workspace 启动了代理,但随后尝试在 /cloudsql 处连接。您需要更新代理或更新应用程序用于连接的路径。

行:

args:
      - -c
      - '(/workspace/cloud_sql_proxy -dir=/workspace -instances=sample-kubernetes-268320:us-west1:cloud-build-staging & sleep 2) && (pip install -r requirements.txt && cd fastestfollowup && python3 manage.py test)'

应该阅读:

args:
      - -c
      - '(/workspace/cloud_sql_proxy -dir=/cloudsql -instances=sample-kubernetes-268320:us-west1:cloud-build-staging & sleep 2) && (pip install -r requirements.txt && cd fastestfollowup && python3 manage.py test)'

其他错误包括:

  1. 监听错误的端口。在日志中,您将看到数据库监听的端口。 postgres 的默认值是 5432,你的是 3306。

  2. 验证您的 Cloud Build 服务具有正确的权限。通过 IAM 仪表板将 [projectnumber]@cloudbuild.gserviceaccount.com 添加为“Cloud SQL 管理员”。

关于google-app-engine - 在 Google Cloud Build 过程中连接到数据库时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60290615/

相关文章:

java - 如何使用 Ajax、Java、Spring Framework 将文件从网页上传到 Google Cloud Storage

java - 谷歌应用引擎: Orchestrating Multiple Apps to Deliver a Composite Service

java - google app engine 支持 Java 的 restful API?

java - Google Storage API 中的死锁

google-cloud-platform - 如何增加 Google Cloud Build 中 inotify 观察者的数量?

google-cloud-platform - Google Cloud Build 无法通过我的私有(private) GitHub 存储库进行身份验证

python - 使用谷歌应用程序引擎部署 Bottle 应用程序时出现问题

javascript - 数据库没有更新

java - 在java中从谷歌云存储读取/下载部分文件

docker - 无法从 docker build 中的工件注册表安装私有(private)依赖项