将 Django 连接到 Google Cloud SQL [英] Connect Django to Google Cloud SQL
问题描述
我正在尝试将 Django 连接到 Google Cloud SQL,在 Windows 下使用 python 2.7 和 django 1.5.我浏览了此页面上的说明:https://developers.google.com/appengine/docs/python/cloud-sql/django
I'm trying to connect Django to the Google cloud SQL, working with python 2.7 and django 1.5 under windows. I went through the instructions on this page: https://developers.google.com/appengine/docs/python/cloud-sql/django
我的 settings.py 文件具有以下形式的基本数据库设置:
My settings.py file has basic database settings of the form:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'INSTANCE': 'my_project:instance1',
'NAME': 'my_database',
}
}
当然还有一个合适的 SQL 实例和通过 google apis 控制台的 SQL 提示创建的数据库
With of course a proper could SQL instance and a database created through the SQL prompt of the google apis console
当我第一次尝试运行 manage.py syncdb
以获取 OAuth2 令牌时,我得到:
When I try to run manage.py syncdb
for the first time in order to obtain an OAuth2 token, I get this:
OperationalError: (2003, 无法连接到 'localhost' 上的 MySQL 服务器(10061)")
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")
在你问之前,我确实确保 django 和 google 包都在我的 PYTHONPATH 中,以及C:Program Files (x86)Googlegoogle_appenginelibdjango-1.5"
Before you ask, I did make sure that both the django and google packages are in my PYTHONPATH, as well as "C:Program Files (x86)Googlegoogle_appenginelibdjango-1.5"
非常欢迎任何帮助!
推荐答案
该数据库配置仅在从 AppEngine 连接时才有意义.如果要使用 django 从本地计算机访问 CloudSQL 数据库,则应使用 google.appengine.ext.django.backends.rdbms
引擎.
That database configuration only makes sense when connecting from AppEngine. If you want to access your CloudSQL database from your local machine using django, you should use the google.appengine.ext.django.backends.rdbms
engine.
您可以在此处查看不同的配置选项:https://developers.google.com/appengine/docs/python/cloud-sql/django#development-settings
You can see the different configuration options here: https://developers.google.com/appengine/docs/python/cloud-sql/django#development-settings
EDIT:google.appengine.ext.django.backends.rdbms
引擎已被弃用.如果您想从本地机器连接到 Google Cloud SQL,您应该使用 IP 连接.您可以使用 Cloud SQL 实例 IP(IPv4 或 IPv6)并使用标准 django.db.backends.mysql
引擎进行连接.
EDIT: The google.appengine.ext.django.backends.rdbms
engine has been deprecated. If you want to connect to Google Cloud SQL from your local machine you should use IP connectivity. You can use the Cloud SQL instance IP (IPv4 or IPv6) and connect using the standard django.db.backends.mysql
engine.
这篇关于将 Django 连接到 Google Cloud SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!