在开发环境中使用Cloud SQL的Google App Engine上的Django [英] Django on Google App Engine with Cloud SQL in dev environment

查看:129
本文介绍了在开发环境中使用Cloud SQL的Google App Engine上的Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Django在GAE和CloudSQL上创建一个应用程序作为数据库。


我使用这个 Google开发人员链接这个链接设置开发环境。我不能连接到本地的mysql数据库。



这是我试图使用的DATABASE设置。



如果(os.getenv('SERVER_SOFTWARE','').startswith('Google App Engine')或
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上工作正常,但是当我尝试在dev env上启动应用程序时,我收到此错误

 文件/home/sandeep/Downloads/google_appengine/google/appengine/tools/dev_appserver_import_hook.py,第635行,__init__ 
raise IOError(errno.EACCES,'file not accessible',filename)
IOError:[Errno 13]文件不可访问:'/usr/local/lib/python2.7/site-packages/MySQL_python-1.2。 4-py2.7-linux-x86_64.egg'

http://pastebin.com/ZXHv0FPQ



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



编辑1

I还尝试将MySQLdb添加到库中。

   - 名称:MySQLdb 
版本:最新

得到这个错误

 库MySQLdb不支持
在/home/sandeep/development/UploadImage/src/app.yaml中,第14行第1列

编辑2

Django syncdb在这个设置下工作正常,django能够为我创建表。但是,当我尝试通过dev_appserver.py运行时,我得到了上面的stacktrace。


我可以在开发环境中访问cloudSQL。

解决方案

按照此处提及的工作。我没有这个代码片段有什么问题。

  import os 
如果os.getenv('SERVER_SOFTWARE ','').startswith('Google App Engine'):
#在生产App Engine上运行,因此使用Google Cloud SQL数据库。
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':
#正在开发中运行,但要在生产中访问Google Cloud SQL实例
#。
DATABASES = {
'default':{
'ENGINE':'google.appengine.ext.django.backends.rdbms',
'INSTANCE':'your-project -id:your-instance-name',
'NAME':'django_test',
'USER':'root',
}
}
其他:
#运行在开发中,所以使用本地的MySQL数据库。
DATABASES = {
'default':{
'ENGINE':'django.db.backends.mysql',
'NAME':'django_test',
'USER':'root',
'PASSWORD':'root',
}
}

我还在django中使用Google App Engine和cloudql,这里是我一直用于部署和本地开发的设置,它的工作原理很好!!



在GAE中部署的设置

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

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

  DATABASES = {
默认':{
'ENGINE':'django.db.backends.mysql',
'NAME':'name_of_database',
'USER':'mysql_user',
'PASSWORD':'pwd',
'HOST':'ip_address_of_cloudsql_instance',#通过域套接字为本地主机或通过TCP为localhost为127.0.0.1。
}
}


I am trying to create an application with Django on GAE and CloudSQL as the db.
I used this google developers link and this link for setting up the dev-environment. I am not able to connect to local mysql db.

Here is the DATABASE setting which I am trying to use.

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

My app is working perfectly on production GAE, but when I try to start the app on dev env, I am getting this error

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'

Complete stack-trace at http://pastebin.com/ZXHv0FPQ

I had installed the "python-mysql" by downloading the source and running "python setup.py install"

Edit 1
I have also tried adding the MySQLdb to the library.

- name: MySQLdb
  version: "latest"

Got this error

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

EDIT 2
Django syncdb is working fine with this settings and django is able to create the tables for me.But,when I try to run via "dev_appserver.py", then I got the above stacktrace.
I am able to access the cloudSQL in dev environment.

解决方案

This should work as mentioned here. I don't there is anything wrong with this code snippet.

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

I have also been using Google App Engine with cloudsql in django and here are the settings that I have been using for deployment and local development and it works just fine !!

Settings for deployment in GAE

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

Settings for local development with 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.
    }
}

这篇关于在开发环境中使用Cloud SQL的Google App Engine上的Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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