类似于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']
我已采取的调试步骤:
- 我已将 Cloud Build Service 添加为 Cloud SQL 管理员
- 我通过使用
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)'
其他错误包括:
监听错误的端口。在日志中,您将看到数据库监听的端口。 postgres 的默认值是 5432,你的是 3306。
验证您的 Cloud Build 服务具有正确的权限。通过 IAM 仪表板将 [projectnumber]@cloudbuild.gserviceaccount.com 添加为“Cloud SQL 管理员”。
关于google-app-engine - 在 Google Cloud Build 过程中连接到数据库时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60290615/