Django on Google App Engine with Cloud SQL in dev

2019-03-30 20:13发布

问题:

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.

回答1:

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