dbi*_*ard 3 django google-app-engine oauth-2.0 python-2.7 google-cloud-sql
我正在尝试将Django连接到Google云端SQL,在Windows下使用python 2.7和django 1.5.我浏览了此页面上的说明:https://developers.google.com/appengine/docs/python/cloud-sql/django
我的settings.py文件具有以下形式的基本数据库设置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'INSTANCE': 'my_project:instance1',
'NAME': 'my_database',
}
}
Run Code Online (Sandbox Code Playgroud)
当然可以通过google apis控制台的SQL提示创建适当的SQL实例和数据库
当我manage.py syncdb第一次尝试运行以获取OAuth2令牌时,我得到了这个:
OperationalError:(2003,"无法连接到'localhost'上的MySQL服务器'(10061)")
在你问之前,我确实确保django和google软件包都在我的PYTHONPATH中,以及"C:\ Program Files(x86)\ Google\google_appengine\lib\django-1.5"
任何帮助都会非常受欢迎!
从AppEngine连接时,该数据库配置才有意义.如果要使用django从本地计算机访问CloudSQL数据库,则应使用该google.appengine.ext.django.backends.rdbms引擎.
您可以在此处查看不同的配置选项:https: //developers.google.com/appengine/docs/python/cloud-sql/django#development-settings
编辑:该google.appengine.ext.django.backends.rdbms引擎已被弃用.如果要从本地计算机连接到Google Cloud SQL,则应使用IP连接.您可以使用Cloud SQL实例IP(IPv4或IPv6)并使用标准django.db.backends.mysql引擎进行连接.
AppEngine Standard, Python 2.7:try:
import MySQLdb # noqa: F401
except ImportError:
import pymysql
pymysql.install_as_MySQLdb()
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
# Running on production App Engine, so connect to Google Cloud SQL using
# the unix socket at /cloudsql/<your-cloudsql-connection string>
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '/cloudsql/<your-cloudsql-connection-string>',
'NAME': '<your-database-name>',
'USER': '<your-database-user>',
'PASSWORD': '<your-database-password>',
}
}
else:
# Running locally so connect to either a local MySQL instance or connect to
# Cloud SQL via the proxy. To start the proxy via command line:
#
# $ cloud_sql_proxy -instances=[INSTANCE_CONNECTION_NAME]=tcp:3306
#
# See https://cloud.google.com/sql/docs/mysql-connect-proxy
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1', # DB's IP address
'PORT': '3306',
'NAME': '<your-database-name>',
'USER': '<your-database-user>',
'PASSWORD': '<your-database-password>',
}
}
Run Code Online (Sandbox Code Playgroud)
来源:GCP Python Django 示例 AppEngine Standard Python 2.7
AppEngine Standard, Python 3.7:# Install PyMySQL as mysqlclient/MySQLdb to use Django's mysqlclient adapter
# See https://docs.djangoproject.com/en/2.1/ref/databases/#mysql-db-api-drivers
# for more information
import pymysql # noqa: 402
pymysql.install_as_MySQLdb()
if os.getenv('GAE_APPLICATION', None):
# Running on production App Engine, so connect to Google Cloud SQL using
# the unix socket at /cloudsql/<your-cloudsql-connection string>
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '/cloudsql/[YOUR-CONNECTION-NAME]',
'USER': '[YOUR-USERNAME]',
'PASSWORD': '[YOUR-PASSWORD]',
'NAME': '[YOUR-DATABASE]',
}
}
else:
# Running locally so connect to either a local MySQL instance or connect to
# Cloud SQL via the proxy. To start the proxy via command line:
#
# $ cloud_sql_proxy -instances=[INSTANCE_CONNECTION_NAME]=tcp:3306
#
# See https://cloud.google.com/sql/docs/mysql-connect-proxy
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1', # DB's IP address
'PORT': '3306',
'NAME': '[YOUR-DATABASE]',
'USER': '[YOUR-USERNAME]',
'PASSWORD': '[YOUR-PASSWORD]',
}
}
Run Code Online (Sandbox Code Playgroud)
源GCP Python Django 示例 AppEngine Standard Python 3.7
AppEngine Flexible:DATABASES = {
'default': {
# If you are using Cloud SQL for MySQL rather than PostgreSQL, set
# 'ENGINE': 'django.db.backends.mysql' instead of the following.
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'polls',
'USER': '<your-database-user>',
'PASSWORD': '<your-database-password>',
# For MySQL, set 'PORT': '3306' instead of the following. Any Cloud
# SQL Proxy instances running locally must also be set to tcp:3306.
'PORT': '5432',
}
}
# In the flexible environment, you connect to CloudSQL using a unix socket.
# Locally, you can use the CloudSQL proxy to proxy a localhost connection
# to the instance
DATABASES['default']['HOST'] = '/cloudsql/<your-cloudsql-connection-string>'
if os.getenv('GAE_INSTANCE'):
pass
else:
DATABASES['default']['HOST'] = '127.0.0.1' # DB's IP address
Run Code Online (Sandbox Code Playgroud)
源GCP Python Django 示例 AppEngine 灵活
| 归档时间: |
|
| 查看次数: |
5249 次 |
| 最近记录: |