python - Google App Engine 上的 Django 在开发环境中使用 Cloud SQL

标签 python mysql django google-app-engine google-cloud-sql

我正在尝试使用 Django on GAE 和 CloudSQL 作为数据库创建一个应用程序。
我用了这个 google developers linkthis link用于设置开发环境。我无法连接到本地 mysql 数据库。

这是我尝试使用的数据库设置。

if (os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine') or
os.getenv('SETTINGS_MODE') == 'prod'):
DATABASES = {
    'default': {
        'ENGINE': 'google.appengine.ext.django.backends.rdbms',
        'INSTANCE': 'instance:appid',
        'NAME': 'database_name',
    }
}
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'USER': 'root',
            'PASSWORD': '',
            'HOST': 'localhost',
            'NAME': 'database_name',
        }
    }

我的应用程序在生产 GAE 上运行完美,但是当我尝试在开发环境中启动应用程序时,出现此错误

File "/home/sandeep/Downloads/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 635, in __init__
raise IOError(errno.EACCES, 'file not accessible', filename)
IOError: [Errno 13] file not accessible: '/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.4-py2.7-linux-x86_64.egg'

http://pastebin.com/ZXHv0FPQ 处完成堆栈跟踪

我通过下载源代码并运行“python setup.py install”安装了“python-mysql”

编辑 1
我还尝试将 MySQLdb 添加到库中。

- name: MySQLdb
  version: "latest"

遇到这个错误

the library "MySQLdb" is not supported
  in "/home/sandeep/development/UploadImage/src/app.yaml", line 14, column 1

编辑 2
Django syncdb 使用此设置工作正常,并且 django 能够为我创建表。但是,当我尝试通过“dev_appserver.py”运行时,我得到了上面的堆栈跟踪。
我能够在开发环境中访问 c​​loudSQL。

最佳答案

这应该像提到的那样工作 here .我认为此代码片段没有任何问题。

import os
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
    # Running on production App Engine, so use a Google Cloud SQL database.
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': '/cloudsql/your-project-id:your-instance-name',
            'NAME': 'django_test',
            'USER': 'root',
        }
    }
elif os.getenv('SETTINGS_MODE') == 'prod':
    # Running in development, but want to access the Google Cloud SQL instance
    # in production.
    DATABASES = {
        'default': {
            'ENGINE': 'google.appengine.ext.django.backends.rdbms',
            'INSTANCE': 'your-project-id:your-instance-name',
            'NAME': 'django_test',
            'USER': 'root',
        }
    }
else:
    # Running in development, so use a local MySQL database.
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'django_test',
            'USER': 'root',
            'PASSWORD': 'root',
        }
    }

我也一直在 django 中使用带有 cloudsql 的 Google App Engine,这里是我一直用于部署和本地开发的设置,它工作得很好!!

GAE 中的部署设置

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'HOST': '/cloudsql/instance:appid',
        'NAME': 'name_of_database',
        'USER': 'mysql_user',
    }
}

使用 App engine sdk 进行本地开发的设置

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'name_of_database',
        'USER': 'mysql_user',
        'PASSWORD': 'pwd',
        'HOST': 'ip_address_of_cloudsql_instance',   # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
    }
}

关于python - Google App Engine 上的 Django 在开发环境中使用 Cloud SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21002638/

相关文章:

mysql - 如何在将一个表中的行插入另一个表时更新值?

Django admin GenericForeignKey 内联

Python 点击​​ : How to print full help details on usage error?

python - 如何在 Matplotlib 中绘制非数字数据

python - 尝试运行方法时,获取 'NoneType is not callable'

python - 在 MongoDB 中的一个查询中更新字段和 $push 到数组

php - 如何使用 ajax post 在 ionic 中实现此搜索查询?

php - 根据数组按顺序获取数据

javascript - 什么是 Django 端点?

javascript - 我如何在 Javascript 和 Django 中执行此操作?