Reputation: 584
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.
Upvotes: 2
Views: 2251
Reputation: 207912
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
Upvotes: 2