简体   繁体   中英

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. Both Cloud Run and Cloud SQL are in same project. Cloud SQL has public IP.

Cloud Run is running a containerized Django/uwsgi/nginx application. 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:

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

Service account that is attached to Cloud Run service have Compute Engine default service account which basically means it has all the access.

Solution : For anyone who may come to this question, use socket reference, not IP and Port. Since Cloud Run creates socket to connect to Cloud SQL and Django's IP:3306 doesn't work.

My update django DB settings:

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.

Cloud Run uses an unix socket to connect to SQL.

From your error message, it looks like it tries to connect directly to the 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,

socket format is: /cloudsql/connection_id

See more here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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