在 Google 云构建过程中连接到数据库时出现问题
Issue connecting to Database during Google Cloud Build process
类似于(我将在下面更详细地描述link)
问题:
当 运行 我的 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']
我已采取的调试步骤:
- 我已将云构建服务添加为云 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 Admin"。
类似于
问题: 当 运行 我的 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']
我已采取的调试步骤:
- 我已将云构建服务添加为云 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 Admin"。