Search code examples
djangopython-2.7google-app-engineoauth-2.0google-cloud-sql

Connect Django to Google Cloud SQL


I'm trying to connect Django to the Google cloud SQL, working with python 2.7 and django 1.5 under windows. I went through the instructions on this page: https://developers.google.com/appengine/docs/python/cloud-sql/django

My settings.py file has basic database settings of the form:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'INSTANCE': 'my_project:instance1',
        'NAME': 'my_database',
    }
}

With of course a proper could SQL instance and a database created through the SQL prompt of the google apis console

When I try to run manage.py syncdb for the first time in order to obtain an OAuth2 token, I get this:

OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")

Before you ask, I did make sure that both the django and google packages are in my PYTHONPATH, as well as "C:\Program Files (x86)\Google\google_appengine\lib\django-1.5"

Any help would be really welcome!


Solution

  • That database configuration only makes sense when connecting from AppEngine. If you want to access your CloudSQL database from your local machine using django, you should use the google.appengine.ext.django.backends.rdbms engine.

    You can see the different configuration options here: https://developers.google.com/appengine/docs/python/cloud-sql/django#development-settings

    EDIT: The google.appengine.ext.django.backends.rdbms engine has been deprecated. If you want to connect to Google Cloud SQL from your local machine you should use IP connectivity. You can use the Cloud SQL instance IP (IPv4 or IPv6) and connect using the standard django.db.backends.mysql engine.