Search code examples
pythonmysqldjangogoogle-cloud-sqlunix-socket

Django attempts to connect to wrong socket


I am attempting to set up django to connect to google cloud sql following the instructions found here: Connecting MySQL Client Using the Cloud SQL Proxy. After I start the proxy with:

./cloud_sql_proxy -dir=/cloudsql -instances=my-instance -credential_file=/app/keyfile.json > /logs/proxy.txt &

Django fails to connect to the db, and gives this error when you try to load a page:

(2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

I'm new to unix sockets so I could be interpreting this incorrectly, but it appears to me that django is trying to connect to a socket at /var/run/mysqld/mysqld.sock.

My database configuration in Django is:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'HOSTS': '/cloudsql/my-instance-description',
        'NAME': 'main',
        'USER': '****',
        'PASSWORD': '****',
    }
}

Am I interpreting this correctly? If so, what do I need to change so that django tries to connect to the socket at /cloudsql/my-instance-description rather than the default?


Solution

  • I think the problem is a typo. Use HOST instead of HOSTS.