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.
}
}
Related
Database settings
DATABASES = {
'default': {
'ENGINE': os.environ.get('DB_ENGINE', "mysql"),
'NAME': os.environ.get('DB_NAME', "django_db"),
'USER': os.environ.get('DB_USER', "root"),
'PASSWORD': os.environ.get('DB_PASS', "123456798"),
'HOST': os.environ.get('DB_HOST', "localhost"),
'PORT': os.environ.get('DB_PORT'),
}
}
error
django.core.exceptions.ImproperlyConfigured: 'mysql' isn't an available database backend or couldn't be imported. Check the above exception. To use one of the built-in backends, use 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
Connecting with mysql to generate migration but facing issue
Its my first time and facing following above issue please guide.
Instead of :
mysql in 'ENGINE': os.environ.get('DB_ENGINE', "mysql"),
Try this:
'ENGINE': os.environ.get('DB_ENGINE', "django.db.backends.mysql"),
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_db',
'USER': 'root',
'PASSWORD': '123456798',
}
}
Try this to establish a connection.
I don't usually add HOST or Port but it still connect to database.
I only mention Host or Port if they don't have the default settings.
I using django version 3.0.2.
I'd like to use postgreSQL as my instance db.
And there are two version postgreSQL in server.
After config setting.py DATABASES parameter, and runserver.
It showed error.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dj_web_console',
'USER': 'django',
'PASSWORD': 'django',
'HOST': 'localhost',
'PORT': '',
}
}
psycopg2.OperationalError: FATAL: password authentication failed for user "django"
I'm sure that the username and password are correct.
How to config pg_path in Django as odoo:
In this case, I can use the specific version of pgsql. And run smoothly.
I solve this by indicating the port of the version I installed.
In this case, I have a version 9.6 which install in 5432 and the other is 5433.
So I just solved this by filling in 5433.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dj_web_console',
'USER': 'django',
'PASSWORD': 'django',
'HOST': 'localhost',
'PORT': '5433',
}
}
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'")
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>'
}
}
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.