Search code examples
pythonmysqldjangogoogle-app-enginegoogle-cloud-sql

Google cloud sql: Lost connection to MySQL server at 'reading initial communication packet'


I've set up a default django/django-wiki project. Local tests work fine. Connecting to cloud sql from the local server (with google.appengine.ext.django.backends.rdbms) doesn't work, I believe due to some authentication issue. More importantly, I can't connect from the production server.

I've made sure not to deploy my local MySQLdb sitting in my virtual environment directory.

I have the following in app.yaml:

- name: MySQLdb
  version: "latest"

My DATABASE entry is the following:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbname',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': '/cloudsql/appname:sqlinstance',
        'PORT': '',
    }
}

It seems to be working, or at least doesn't complain about missing packages or mysql import issues. My problem is the following (obtained from the production logs, but also visible via the django debug output):

File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/MySQLdb-1.2.4b4/MySQLdb/connections.py", line 190, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 38")

What could be causing this? Does this mean a connection was made to something but there was no reply? Other posts seem to mention this as an issue when connecting from an external source, but this connection is from app engine AFAIK.


Solution

  • Well, this was much more straight forward than expected. I had assumed the cloud sql, created with my google account would just grant access to my GAE app by default. This has to be set explicitly, from Developer Console->Storage->Cloud SQL->instance-name->Edit->AUTHORIZED APP ENGINE APPLICATIONS. Or when you create the SQL instance, open the advanced options.

    I just added the name of my app in the box, hit save and everything worked.