Connect django to Google Cloud SQL Second Generation

人盡茶涼 提交于 2019-12-01 00:51:50

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/34933941/connect-django-to-google-cloud-sql-second-generation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!