简体   繁体   中英

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

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 .

Regular App Engine instances can't currently access Cloud SQL Second Generation instances. This is briefly mentioned in the docs :

Note: Access to Cloud SQL Second Generation instances can be granted only for Managed VMs.

This should hopefully change before Second Generation instances are out of Beta.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM