简体   繁体   English

Google Cloud run 无法访问 Cloud SQL

[英]Google cloud run cannot reach Cloud SQL

Everything was working fine just a moment ago but suddenly Google Cloud Run cannot connect with Cloud SQL.刚才一切正常,但突然 Google Cloud Run 无法与 Cloud SQL 连接。 Both Cloud Run and Cloud SQL are in same project. Cloud Run 和 Cloud SQL 都在同一个项目中。 Cloud SQL has public IP. Cloud SQL 具有公共 IP。

Cloud Run is running a containerized Django/uwsgi/nginx application. Cloud Run 正在运行一个容器化的 Django/uwsgi/nginx 应用程序。 Getting following error:得到以下错误:

MySQLdb._exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'xx.xxx.xx.xxx:3306' (110)")

Django settings:姜戈设置:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': my_db_name,
        'USER': my_db_user,
        'PASSWORD': my_db_password,
        'HOST': cloud_sql_ip_address,
        'PORT': '3306',
    }
}

Below is the Cloud Run yaml piece:下面是 Cloud Run yaml 部分:

annotations:
        run.googleapis.com/client-name: gcloud
        client.knative.dev/user-image: my_custom_manage
        run.googleapis.com/client-version: 347.0.0
        run.googleapis.com/cloudsql-instances: my_project_id:us-central1:my_sql_server
        autoscaling.knative.dev/maxScale: '10'
        run.googleapis.com/sandbox: gvisor

I have also checked this - https://cloud.google.com/sql/docs/mysql/connect-run我也检查过这个 - https://cloud.google.com/sql/docs/mysql/connect-run

Service account that is attached to Cloud Run service have Compute Engine default service account which basically means it has all the access.附加到 Cloud Run 服务的Compute Engine default service account具有Compute Engine default service account ,这基本上意味着它拥有所有访问权限。

Solution : For anyone who may come to this question, use socket reference, not IP and Port.解决方案:对于可能遇到此问题的任何人,请使用套接字引用,而不是 IP 和端口。 Since Cloud Run creates socket to connect to Cloud SQL and Django's IP:3306 doesn't work.由于 Cloud Run 创建套接字以连接到 Cloud SQL,因此 Django 的IP:3306不起作用。

My update django DB settings:我的更新 django 数据库设置:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': my_db_name,
        'USER': my_db_user,
        'PASSWORD': my_db_password,
        'HOST': f'/cloudsql/cloud_sql_connection_name'
    }
}

I am not sure though why it was working fine before with IP:3306 , Cloud Run should have given error in the start itself.我不确定为什么它之前使用IP:3306工作正常,但 Cloud Run 应该在启动时出错。

Cloud Run uses an unix socket to connect to SQL. Cloud Run 使用 Unix 套接字连接到 SQL。

From your error message, it looks like it tries to connect directly to the IP.从您的错误消息来看,它似乎试图直接连接到 IP。

I would check the application code and see if there was an undetected update, the connection string should be based on a socket and not on an IP,我会检查应用程序代码,看看是否有未检测到的更新,连接字符串应该基于套接字而不是 IP,

socket format is: /cloudsql/connection_id套接字格式为:/cloudsql/connection_id

See more here 在这里查看更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM