Gary
Gary

Reputation: 2339

Can't connect to MySQL server in Django 2.x.x Python AppEngine

I am trying to use cloud SQL / mysql instance for my APPEngine account. The app is an python django-2.1.5 appengine app. I have created an instance of MYSQL in the google cloud.

I have added the following in my app.yaml file copied from the SQL instance details:

beta_settings:
  cloud_sql_instances: <INSTANCE_CONNECTION_NAME>=tcp:<TCP_PORT>
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'my-db',
            'USER': 'appengine',
            'PASSWORD': 'xxx',
            'HOST': '111.111.11.11', # used actual ip
            'PORT': '3306'
        }
    }
DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': '/cloudsql/<your-project-id>:<your-cloud-sql-instance>',
            'NAME': '<your-database-name>',
            'USER': 'root',
        }
    }

My problems:

OperationalError: (2003, "Can't connect to MySQL server on '0.0.0.0' ([Errno 111] Connection refused)")

Upvotes: 1

Views: 617

Answers (2)

Tshiamo Motshabi
Tshiamo Motshabi

Reputation: 21

I've encountered this issue before and after hours of scratching my head, all I needed to do was enable "Cloud SQL Admin API" and the deployed site connected to the database. This also sets permissions on your GAE service account for cloud proxy to connect to your GAE service.

Upvotes: 1

kurtisvg
kurtisvg

Reputation: 3565

App Engine doesn't have any guarantees regarding IP address of a particular instance, and may change at any time. Since it is a Serverless platform, it abstracts away the infrastructure to allow you to focus on your app.

There are two options when using App Engine Flex: Unix domain socket and TCP port. Which one App Engine provides for you depends on how you specify it in your app.yaml:

  • cloud_sql_instances: <INSTANCE_CONNECTION_NAME> provides a Unix socket at /cloudsql/<INSTANCE_CONNECTION_NAME>
  • cloud_sql_instances: <INSTANCE_CONNECTION_NAME>=tcp:<TCP_PORT> provides a local tcp port (127.0.0.1:<TCP_PORT>).

You can find more information about this on the Connecting from App Engine page.

Upvotes: 2

Related Questions