Currently , I have deployed my django project on google app engine. I need to run python manage.py migrate command so that auth_user table should be created on my google cloud instance . But don't know where to run this command.
If I get it right, your app runs on App Engine (sandboxed environment) and uses Cloud SQL.
1) Configure your database in settings.py as you can see below.
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/project-id:instance-name',
'NAME': 'database-name',
'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': 'django.db.backends.mysql',
'INSTANCE': 'cloud-sql-instance-ip-address',
'NAME': 'database-name',
'USER': 'root',
'PASSWORD': 'password',
}
}
else:
# Running in development, so use a local MySQL database.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'database-name',
'USER': 'username',
'PASSWORD': 'password',
}
}
2) Set environment variable SETTINGS_MODE to prod (or do not set if you want to access your local MySQL server).
3) Run the below command from your machine.
$ SETTINGS_MODE=prod python manage.py migrate
You can find more details in App Engine documentation - Management commands and Alternate development database and settings.
Related
I have a django app hosted on google appengine and google cloud sql as database. I follows this link https://cloud.google.com/appengine/docs/python/cloud-sql/django to sync and migrate db schema to cloudsql. This worked fine for me. But when I tried syncdb today, I am getting an error as shown below.
OperationalError: could not connect: https://www.googleapis.com/sql/v1/jdbc/openConnection?alt=proto
returned "Not Found">
My database settings is
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/<projectname>:<instancename>',
'NAME': '<db-name>',
'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': '<projectname>:<instancename>',
'NAME': 'db-name',
'USER': 'root',
}
}
I changed my db settings and authorized my local ip address at google api console. Now it is working fine.
My new db settings is
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/<projectname>:<instancename>',
'NAME': '<db-name>',
'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': 'django.db.backends.mysql',
'HOST': '<instance-ip-address>',
'NAME': 'db-name',
'USER': 'root',
'PASSWORD': '<password>'
}
}
My app engine app is doing well, but I want to toggle the "only allow secure connections" option in the Cloud SQL configuration. When i do so, I can still connect remotely and in local development. However, my deployed app stops working with a 1045 error "access denied"
I cannot figure out where to store/upload the ssl certificates on the deployed app or how to call them from settings.py
Can someone please help?
Here is the relevant section of settings.py (redacted)
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/<myappengineapp>:<mycloudsqlinstance>',
'NAME': 'test',
'USER': 'root',
'PASSWORD': '<mypassword>',
'OPTIONS':
<ssl goes in here?>
}
}
else:
# Running in development, but want to access the Google Cloud SQL instance
Is there a step that I missed, perhaps a "syncdb" step for Google Cloud SQL? I read both of these:
Using Google Cloud SQL
Django Support
My application has access to Google Cloud SQL but I think I just have to create the tables in Google Cloud SQL. What's the command to do that? Maybe that will fix it. Running python manage.py syncdb updates SQL on localhost but I'd like to update the database on Google Cloud SQl.
EDIT: Below are the Google Cloud SQL settings and the settings.py for my project.
There are three type of connections:
1) your dev machine to your local mysql instance (Easy!)
2) your app engine instance (in Google Cloud) to Google Cloud SQL instance (the same way described in the document)
3) your dev machine to cloudsql instance (Hard): the encouraged way is to obtain an static IP (for a while that doesn't cost you much money), and use the IP to connect cloudsql instance.
Django code [3] should allow you switch between above cases. The command [1] allows you to syncdb to cloudsql instance. If you want your django model to manipulate data in cloudsql instance, you can add config [2] to app.yaml
[1]
SETTING_MODE=prod ./manage.py syncdb
[2]
env_variables:
SETTINGS_MODE: prod
[3]
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': 'django.db.backends.mysql',
'HOST': 'static-ip-of-cloudsql-instance',
'PORT': '3306',
'NAME': 'django_test',
'USER': 'username',
'PASSWORD': 'password'
}
}
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 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.
}
}
I configured a postgressql database on ec2 instance. Now I want to talk to this database server using a different ec2 instance which is running a python/django framework.
My settings.py file contains:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ['DB_NAME'],
'USER': os.environ['DB_USERNAME'],
'PASSWORD': os.environ['DB_PASSWORD'],
'HOST': os.environ['DB_HOSTNAME'],
'PORT': os.environ['DB_PORT'],
}
}
Where "DB_NAME" and other variables are defined in .ebextensions/*.config file under option_settings. When I push the code to AWS, the deployment log files shows that DB_NAME does not exists. I am not sure where I am going wrong. The Elasticbeanstalk console shows the variables though.