Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect django to Google Cloud SQL Second Generation

I am using a Cloud SQL Second generation. Django is running on the appspot, and connection to the database is working fine when testing on the localhost. I can also connect with the mysql client to the instance remotely, create databases, create users, the usual stuff. I can even remotely upload fixtures.

But I am unable to get django on the appspot connected to the SQL instance.

When trying this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'INSTANCE': 'instance:connection:name',
        'NAME': 'database',
        'USER': 'dbuser',
        'PASSWORD': 'pass',
    }
}

I get an OperationalError:

(2001, "Can't create UNIX socket (-1)")

When trying this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'database',
        'USER': 'dbuser',
        'PASSWORD': 'pass',
        'HOST': '1.2.3.4', # my SQL instance IPv4 address
    }
}

I get an OperationalError:

(2004, "Can't create TCP/IP socket (-1)")

When trying this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'database',
        'USER': 'dbuser',
        'PASSWORD': 'pass',
        'HOST': '/cloudsql/my-instance-id',
    }
}

I get an OperationalError:

(2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 38")

What are the DATABASES settings to use for django when using a Google Cloud SQL Second Generation?

EDIT

There is a page describing the required settings, but I would say that is for First Generation.

Also, there is a related question, but that applies also for First Generation.

EDIT2

I have seen that by default there are no "Authorized applications" for Cloud SQL Second Generation, and there is no option to authorize an application on the Google Cloud Platform console. This could be the reason why connections are not working. Unfortunately I do not yet know how to authorize an application for Second Generation instances.

EDIT3

Trying to authorize applications:

gcloud sql instances patch <sql-instance> --authorized-gae-apps <gae-app>

But unfortunately:

ERROR: (gcloud.sql.instances.patch) Failed to update an instance because it exceeded the maximum number of app IDs that can be attached to the instance.

No idea how to this for Second Generation

like image 299
blueFast Avatar asked Jan 21 '16 20:01

blueFast


People also ask

Does Google Cloud support Django?

Stay organized with collections Save and categorize content based on your preferences. Django apps that run on Google Cloud are running on the same infrastructure that powers all of Google's products, which generally improves the application's ability to adapt to a variable workload.

How do I connect Google Cloud database to SQL?

In the Google Cloud console, go to the Cloud SQL Instances page. To open the Overview page of an instance, click the instance name. Select Connections from the SQL navigation menu. In the Authorized networks section, click Add network and enter the IP address of the machine where the client is installed.


1 Answers

are different configurations in the first generation and the second configuration. I had the same problem and fix it by changing the name of the instance:

/cloudsql/[INSTANCE_CONNECTION_NAME]

Before you have to authorize the app engine application.

Source.

like image 135
DGLeiva Avatar answered Oct 10 '22 12:10

DGLeiva