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

4
我正在尝试使用Django在GAE上创建一个应用程序,并将CloudSQL作为数据库。
我使用了这个谷歌开发者链接这个链接来设置开发环境。但我无法连接到本地mysql数据库。

以下是我正在尝试使用的DATABASE设置。

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”运行时,我得到了上面的堆栈跟踪信息。
我可以在开发环境中访问cloudSQL。


我已经使用源码安装了python-mysql。我需要将其添加到PYTHONPATH中吗? - Sandeep
如果我将引擎更改为“google.appengine.ext.django.backends.rdbms”,那么它就能够访问实时的cloudSQL实例。 我无法在本地mysql上设置django。 - Sandeep
1
从源代码安装?为什么不使用 pip ? - Juan Enrique Muñoz Zolotoochin
我最初使用apt-get安装,但是python -c "import MySQLdb"失败了。所以我不得不使用源代码进行安装。 - Sandeep
pip install mysql-python 要求已经满足(使用--upgrade进行升级):mysql-python在/usr/local/lib/python2.7/dist-packages中。 清理中... - Sandeep
显示剩余2条评论
1个回答

2

这应该像这里所述的那样工作。我认为这个代码片段没有任何问题。

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',
        }
    }

我也一直在使用Google App Engine和Django中的Cloud SQL,以下是我用于部署和本地开发的设置,它可以正常运行!

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.
    }
}

@Sandeep,这里出现了一个I/O错误 IOError: [Errno 13] file not accessible: '/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.4-py2.7-linux-x86_64.egg'。你能检查一下运行该项目的用户对该文件的权限吗? - Santosh Ghimire
-rwxr-xr-x 1 root staff 112570 Jan 8 06:06 /usr/local/lib/python2.7/site-packages/MySQL_python-1.2.4-py2.7-linux-x86_64.egg - Sandeep
是的,这就是我想要实现的。 - Sandeep
我不理解的是,为什么你想在本地开发中使用两个数据库——一个本地MySQL数据库和另一个CloudSQL数据库,而你可以使用同一个CloudSQL数据库进行开发(以及生产)。 - Santosh Ghimire
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/45604/discussion-between-sandeep-and-santosh-ghimire - Sandeep
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接