Announcements
This site is in read only until July 22 as we migrate to a new platform; refer to this community post for more details.
Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Ajay Hinduja Swiss - App Engine Can’t Connect to Cloud SQL (MySQL/Postgres)

Hi everyone,

I’m trying to deploy a Django project to Google App Engine, but I’m encountering a database connection issue. I’ve followed the official deployment guide, but App Engine is unable to connect to the Cloud SQL database I created.

I’ve tested with both PostgreSQL and MySQL, and in both cases, I get a connection error. Here are a few things I’ve already checked:

Cloud SQL Admin API is enabled.

The App Engine default service account has the Cloud SQL Client role.

The database name, user credentials, and instance connection name are all correct.

When I connect via Cloud SQL Proxy, everything works fine.

Here’s the error I receive:

OperationalError at /login
(2002, "Can't connect to local MySQL server through socket '...:southamerica-east1:my-sql' (111)")

I’d appreciate any help or suggestions. Could this be related to missing environment configuration or networking rules specific to App Engine?

 

Thanks in advance!
Ajay Hinduja Swiss

0 1 69
1 REPLY 1

Hi @ajay-hinduja,

Welcome to Google Cloud Community! 

The error message you're encountering, "(2002, 'Can't connect to local MySQL server through socket...')" indicates that your App Engine instance is unable to connect to your Cloud SQL instance, often due to networking misconfigurations, incorrect socket paths, or issues with the Cloud SQL proxy. It may also indicate that the MySQL server isn't running, or that you're using the wrong Unix socket, TCP/IP port, or a blocked port due to firewall rules.

Here’s a quick guide to help you resolve the issue:

  1. Use the Correct Database Connection Settings for App Engine:

When deploying Django on App Engine Standard, you must configure your database connection to use a Unix socket path instead of a TCP hostname like 127.0.0.1 or a public IP. This involves specifying the Cloud SQL instance connection name in a format the connector understands. 

This approach applies to both MySQL and PostgreSQL, and Django's settings.py should reflect this to ensure proper integration. Google provides a general Django deployment guide that covers these configurations in detail.

  • For MySQL, use something like this in settings.py:
DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.mysql',

        'HOST': '/cloudsql/YOUR_PROJECT_ID:REGION:INSTANCE_NAME',

        'NAME': 'your_database_name',

        'USER': 'your_database_user',

        'PASSWORD': 'your_database_password',

    }

}
  • For PostgreSQL, it's the same pattern:
DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.postgresql',

        'HOST': '/cloudsql/YOUR_PROJECT_ID:REGION:INSTANCE_NAME',

        'NAME': 'your_database_name',

        'USER': 'your_database_user',

        'PASSWORD': 'your_db_password',

    }

}
  1. Add Cloud SQL Instance to app.yaml:

To ensure App Engine connects to your Cloud SQL instance, you must explicitly specify the instance in your app.yaml using the beta_settings.cloud_sql_instances field. This enables App Engine to provision the correct Unix socket for the connection. If this setting is missing, the socket won’t be mounted, leading to connection errors. Additionally, make sure the App Engine service account has the Cloud SQL Client role and that you're deploying with the correct active account (you can verify using gcloud app describe).

runtime: python310  # or your actual version

env_variables:

  DJANGO_SETTINGS_MODULE: "your_project.settings"

  DB_USER: "your_database_user"

  DB_PASSWORD: "your_database_password"

  DB_NAME: "your_database_name"



beta_settings:

  cloud_sql_instances: YOUR_PROJECT_ID:REGION:INSTANCE_NAME
  1. Ensure Django is within the Correct Environment:

Ensure your settings.py in Django is correctly reading environment variables using os.environ to dynamically set database connection parameters. Set INSTANCE_CONNECTION_NAME either in the env_variables section of app.yaml or through a secret manager, and verify that the production connection string is being used—avoiding any accidental use of local development settings.

import os



DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.mysql',  # or postgresql

        'NAME': os.environ.get('DB_NAME'),

        'USER': os.environ.get('DB_USER'),

        'PASSWORD': os.environ.get('DB_PASSWORD'),

        'HOST': '/cloudsql/{}'.format(os.environ.get('INSTANCE_CONNECTION_NAME')),

    }

}
  1. Check Region Match: 

Ensure your App Engine and Cloud SQL instance are located in the same region or at least within the same continent. While cross-region connections can technically work, they often lead to latency and potential connection instability.

  1. Firewall and Private IP (only if applicable):

If you're using Private IP to connect App Engine to Cloud SQL instead of a Unix socket or public IP with Cloud SQL Proxy, ensure that App Engine is attached to a VPC connector, the database has authorized access from the VPC's IP range, the correct private IP is set in the HOST field, and firewall rules allow traffic on port 3306 (MySQL) or 5432 (PostgreSQL). That said, using a Unix socket remains the preferred method on App Engine Standard.

If you need further assistance with your project, feel free to reach out to Google Cloud Support at any time.

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.