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>'
}
}
Related
I have a database that i would like to use for an app in my new Django project.
I tried adding it to the setting.py by doing the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Colta_create'),
'USER': 'root',
'PASSWORD': 'toor123',
'HOST': '',
'PORT': ''
}
}
I have installed mysqlclient, but i get this error:
django.db.utils.OperationalError: (1049, "Unknown database 'colta_create'")
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.
I'm having some trouble connecting to my AWS RDS Database from my Python/Django application. I'm getting this error when trying to run my application locally:
django.db.utils.OperationalError: (2026, 'SSL connection error: error:00000001:lib(0):func(0):reason(1)')
I couldn't find any answers online as I am not trying to connect via SSL.
My Database settings in settings.py is straightforward:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '<my db name>',
'USER': '<my username>',
'PASSWORD': '<my password>',
'HOST': '<my aws host>',
'PORT': '<my port>',
}
}
I was getting the same error trying to access the MySQL RDS via terminal, but was able to resolve that error by adding --skip-ssl to the end of the command. I've looked around but haven't yet found a way to implement that same fix with my Django application.
This is my first time working with Python, Django and AWS (and also posting a question on stack overflow) so I apologize in advance if my terminology is off. Thank you in advance for any help you can provide.
did you try
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '<my db name>',
'USER': '<my username>',
'PASSWORD': '<my password>',
'HOST': '<my aws host>',
'PORT': '<my port>',
'OPTIONS': {
'skip-ssl',
},
}
}
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.
}
}