4
votes

I'm building a django (1.9) app using cloudsql and a 'new' flexible environment. I have this same error: Django on Google AppEngine with CloudSQL: How to connect database (Error 2002, Can't connect to local MySQL server..)

However, in that thread, they refer to docs which are older and explain to put the database key ,HOST, in the format:

DATABASES = {
   'default': {
      'ENGINE': 'django.db.backends.mysql',
      'HOST': '/cloudsql/myapp-test01:myapp-db-test01',
      'NAME': 'test01',
      'USER': 'test01',
    }

Whereas in the documentation for running django on the 'flexible' environment, it is explained to open your database to the world (0.0.0.0/0) and use the format:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '<your-database-name>',
        'USER': '<your-database-user>',
        'PASSWORD': '<your-database-password>',
        'HOST': '<your-database-host>',
        'PORT': '3306',
        }
    }

Where is presumably the IP address given by CloudSQL.

Please see the docs here: https://cloud.google.com/python/django/flexible-environment

Note the 'Flexible Environment' which seems to be distinctly different (but I don't know why).

I'm writing, because I when I change from IP address format to /cloudsq/... format I get the error

OperationalError: (2002, "Can't connect to local MySQL server through socket '/cloudsql/desgn-test-01:db-test-01' (2)")

Any advice is highly appreciated. It seems impossible that google really expects us to leave databases open to 0.0.0.0, and in the docs they even say it is only for testing but provide no further information.

I should also mention, I'm using two databases in my django config and the 'default' one is a sqlite db (this part of the site is working fine). I get the error when I try to connect to my app that is using the cloud sql parameter. This ONLY happens when deployed, locally everything works perfect.

1
first gen or second gen instance? first gen are not supported from app engine flex & for 2nd gen you need to include the region in the connection string (i recommend copy/pasting the "instance connection name" from Cloud Console) - Vadim
It is a second gen instance.. that's what I mean by 'new'... sorry, bit new to GAE. I have exactly as you suggested, copy/pasted 'instance connection name', but to no avail... (that is actually how it was as well). - John
Take a look at cloud.google.com/sql/docs/app-engine-connect#gaev2-csqlv2. Please follow the steps there and see if that works. - Vadim
Thank you. This seems an important piece of information and has resolved my issue! - John
Glad to hear it's working, can you post an answer with your configuration so that others can learn from it? - Vadim

1 Answers

2
votes

I've made a PR for the documentation on github, but the key changes that were required in order for me to change my DEBUG flag from True to False are described below.

Be aware, that it seems once you change the DEBUG flag, GAE requires that your Cloud SQL database uses the /cloudsql/ socket connection.

Also, I missed the very critical piece of information regarding the ALLOWED_HOSTS, which should include .appspot.com

Production

Once you are ready to serve your content in production, there are several changes required for the configuration. Most notable changes are:

  • Add ".appspot.com" to your ALLOWED_HOSTS
  • Change the DEBUG variable to False in your settings.py file.
  • If you are using a Cloud SQL database instance, in order to change from DEBUG = True to DEBUG = False you will need to properly configure the database. See instructions here and be sure to change your app.yaml file as well as the HOST key in your DATABASES object.