Django on Google App Engine with Cloud SQL in dev environment
Asked Answered
C

1

4

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.

Certain answered 8/1, 2014 at 17:35 Comment(7)
Did you install the python mysql library? you also need to include it on your app.yaml file, see developers.google.com/appengine/docs/python/cloud-sqlGoldin
Yes I have installed python-mysql using the source.Do I have to add it in the PYTHONPATH?Certain
If I change the engine to "google.appengine.ext.django.backends.rdbms" , then it is able to access the live cloudSQL instance. I am not able to setup django with local mysql.Certain
from source? why don't you use pip?Goldin
I had installed initially using apt-get.But python -c "import MySQLdb" failed. So I had to install using the source.Certain
pip install mysql-python Requirement already satisfied (use --upgrade to upgrade): mysql-python in /usr/local/lib/python2.7/dist-packages Cleaning up...Certain
As I have mentioned, Django db commands are working fine.This means the mysql-python is installed properly.Certain
F
2

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.
    }
}
Foran answered 19/1, 2014 at 14:36 Comment(8)
I have tried using django.db.backends.mysql for local setup, but it ain't working. pastebin.com/ZXHv0FPQ . I feel that the python-mysql module is not being accessible in GAE environment. If i try to run an django app, then 'django.db.backends.mysql' works for me.Certain
@Certain there is an I/O error IOError: [Errno 13] file not accessible: '/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.4-py2.7-linux-x86_64.egg' Can you check the permission on this file for the user running this project?Foran
-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.eggCertain
Tried to bring up a new ubuntu setup on VBox.Installed django,gae and python-mysqldb. The only difference is that I had installed python-mysqldb using the source and the other one from apt-get. I removed python-mysqldb using apt-get remove and re-installed, but still not workin.Certain
@Certain you want to use the local mysql database with your GAE app in django for local development, right?Foran
Yes, that's what I am trying to achieve.Certain
what I don't understand is, why are you trying to have two databases for local development - one local mysql database and other cloudsql database, while you can use the same cloudsql database for development(as well as for production).Foran
let us continue this discussion in chatCertain

© 2022 - 2024 — McMap. All rights reserved.