unable to connect to google cloud sql database after deploying an app

160 views
Skip to first unread message

Rajan Subramanian

unread,
Mar 14, 2018, 2:07:54 PM3/14/18
to Google App Engine

Hello, im using python 3.5 on a flexible environment in google app engine.  I am trying to deploy a django app and am running into issues.  In my cloudsql, i have created an instance: retail18 with a username 'root' and password: 'check'.  the database is called 'live'

When i fire up my ipython notebook, i can connect to this 'live' database using pymysql.  I give it the host: 127.0.0.1, username 'root' and password 'check', port: 3306.  I can connect with no problems.  

After following the online instructions on how to deploy django on a flexible environment, I was successful in deploying.  However, when the user enters the site and types his credentials, i get a 'bad gateway' request.  I need to use the database to query the results.  Hers my settings file: 

"""
Django settings for scoville_matching_app project.

Generated by 'django-admin startproject' using Django 1.11.

For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '552_9elkj5@t^h1)=x((aefibvc0mfpjh!s8v2qyo4uxvcz09t'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'scovilleapp', #name of the app we added
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'scoville_matching_app.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'scoville_matching_app.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

# Check to see if MySQLdb is available; if not, have pymysql masquerade as
# MySQLdb. This is a convenience feature for developers who cannot install
# MySQLdb locally; when running in production on Google App Engine Standard
# Environment, MySQLdb will be used.
try:
import MySQLdb # noqa: F401
except ImportError:
import pymysql
pymysql.install_as_MySQLdb()


# [START dbconfig]
DATABASES = {
'default': {
# If you are using Cloud SQL for MySQL rather than PostgreSQL, set
# 'ENGINE': 'django.db.backends.mysql' instead of the following.
'ENGINE': 'django.db.backends.mysql',
'NAME': 'retail_service_data',
'USER': 'root',
'PASSWORD': 'check',
# For MySQL, set 'PORT': '3306' instead of the following. Any Cloud
# SQL Proxy instances running locally must also be set to tcp:3306.
'PORT': '3306',
}
}
# [END db_setup]
# In the flexible environment, you connect to CloudSQL using a unix socket.
# Locally, you can use the CloudSQL proxy to proxy a localhost connection
# to the instance
DATABASES['default']['HOST'] = '/cloudsql/scovilleapp:us-east1:retail18'
if os.getenv('GAE_INSTANCE'):
pass
else:
DATABASES['default']['HOST'] = '127.0.0.1'
# [END dbconfig]

# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# [START staticurl]
# Fill in your cloud bucket and switch which one of the following 2 lines
# is commented to serve static content from GCS
# STATIC_URL = 'https://storage.googleapis.com/<your-gcs-bucket>/static/'
STATIC_URL = 'https://storage.googleapis.com/scovilleapp/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

####
Heres my app.yaml file: 

# [START django_app]
# [START runtime]
runtime: python
threadsafe: yes
env: flex
entrypoint: gunicorn -b :$PORT -b scoville_matching_app.wsgi

beta_settings:
cloud_sql_instances: 'scovilleapp:us-east1:retail18'

runtime_config:
python_version: 3.5
# [END runtime]
handlers:
- url: /static
static_dir: static/
- url: /.*
script: main.application

# Only pure Python libraries can be vendored
# Python libraries that use C extensions can
# only be included if they are part of the App Engine SDK
# Using Third Party Libraries: https://cloud.google.com/appengine/docs/python/tools/using-libraries-python-27

Thanks

Julio Brito

unread,
Mar 14, 2018, 3:41:04 PM3/14/18
to Google App Engine
mysqlclient works 

Katayoon (Cloud Platform Support)

unread,
Mar 14, 2018, 5:40:53 PM3/14/18
to google-a...@googlegroups.com

Hello Rajan,


Here, you can find a sample on Running Django in the App Engine Flexible Environment to double-check the steps you have taken to develop your Django app.


For specific technical questions that do not relate to a bug or a feature request, I also recommend that you post your redacted question on Stack Overflow using one of the tags monitored by our community technical support team.



Reply all
Reply to author
Forward
0 new messages