将django连接到Google Cloud SQL第二代 [英] Connect django to Google Cloud SQL Second Generation

查看:587
本文介绍了将django连接到Google Cloud SQL第二代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Cloud SQL第二代。 Django在appspot上运行,当在本地主机上测试时,与数据库的连接工作正常。我也可以将mysql客户端连接到远程实例,创建数据库,创建用户,通常的东西。我甚至可以远程上传装置



但是我无法在连接到SQL实例的appspot上获取django。



尝试此操作时:

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

我得到一个 OperationalError


(2001,无法创建UNIX套接字(-1))


当尝试这样:

  DATABASES = {
'default':{
' ENGINE':'django.db.backends.mysql',
'NAME':'database',
'USER':'dbuser',
'PASSWORD':'pas s',
'HOST':'1.2.3.4',#我的SQL实例IPv4地址
}
}

我得到一个 OperationalError


(2004,无法创建TCP / IP套接字(-1))


>

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

我得到一个code> OperationalError :


(2013年,丢失与MySQL服务器的连接读取初始
通讯包',系统错误:38)


什么是 DATABASES 在使用Google Cloud SQL Second Generation时用于django的设置?



编辑



有一个描述所需的设置,但我会说这是第一代。



此外,还有一个相关问题,但也适用于第一代。 p>

EDIT2



我看到,默认情况下,云SQL第二代没有授权应用程序,那里无法在Google Cloud Platform控制台上授权应用程序。这可能是连接不起作用的原因。不幸的是,我还不知道如何授权第二代实例的应用程序。



EDIT3



试图< a href =https://cloud.google.com/sql/docs/access-control#appengine =nofollow noreferrer>授权应用程序:

  gcloud sql实例补丁< sql-instance> -  gae-apps< gae-app> 

但不幸的是:


错误:(gcloud.sql.instances.patch)无法更新实例
,因为它超出了实例可以附加
的最大应用程序数量。


不知道如何为第二代

解决方案

是第一代和第二配置中的不同配置。我有同样的问题,通过更改实例的名称修复它:

  / cloudsql / [INSTANCE_CONNECTION_NAME] 

在您必须授权应用引擎应用程序之前。



来源


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.

这篇关于将django连接到Google Cloud SQL第二代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆