Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Can't connect to Database with App Engine

Hi! I'm trying to deploy a Django project to Google Cloud. I followed all the steps in the guide, but App Engine is unable to connect to the database I created. I've tried both Postgres and MySQL, and in both cases I get a connection error. Some considerations:

  • I have Cloud SQL Admin enabled

  • The service account does have the SQL Client permission

  • I'm entering the correct passwords, database names, and instance name

  • When I connect using Cloud SQL Proxy, everything works fine

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

1 1 258
1 REPLY 1

The OperationalError (2002) you're encountering—specifically the failure to connect via a socket like '...:southamerica-east1:my-sql'—typically indicates a misconfiguration in how your Django application on App Engine is set up to communicate with Cloud SQL. The fact that the Cloud SQL Proxy works locally confirms that your database credentials and the Cloud SQL instance itself are configured correctly. The issue lies in the application's runtime configuration, particularly within the App Engine Standard environment, which relies on Unix socket connections for Cloud SQL.

To resolve this, there are two primary configuration points to verify:

  1. Django settings.py – Database Connection

    For App Engine Standard, your DATABASES setting must use the Unix socket path rather than a TCP hostname (such as 127.0.0.1 or a public IP). The HOST field should be set to the following:

     
    '/cloudsql/YOUR_PROJECT_ID:YOUR_REGION:YOUR_INSTANCE_ID'
    

    This applies to both MySQL and PostgreSQL configurations. For example, in settings.py, your MySQL configuration should resemble:

     
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'your-database-name',
            'USER': 'your-database-user',
            'PASSWORD': 'your-password',
            'HOST': '/cloudsql/your-project-id:southamerica-east1:your-instance-id',
        }
    }
    
  2. app.yaml – Cloud SQL Instance Declaration

    App Engine must be explicitly informed that it should connect to your Cloud SQL instance. In your app.yaml, declare the Cloud SQL instance as follows:

     
    runtime: python310
    env_variables:
      DB_NAME: your-database-name
      DB_USER: your-database-user
      DB_PASS: your-password
      CLOUD_SQL_CONNECTION_NAME: your-project-id:southamerica-east1:your-instance-id
    beta_settings:
      cloud_sql_instances: your-project-id:southamerica-east1:your-instance-id
    

    This setting ensures App Engine provisions the correct Unix socket connection. Also confirm that the service account App Engine uses has the Cloud SQL Client role and that you are deploying the app with that service account active (you can verify this using gcloud app describe).