1
votes

I have a GAE app setup and authorized with a Google Cloud SQL instance, but I can't seem to get rid of this error:

OperationalError: (_mysql_exceptions.OperationalError) (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 38")

I'm using Python27 with Flask-SQLAlchemy to connect to the db. Here's the database info:

config.py

CLOUDSQL_DB_NAME = 'db-name'
GAE_PROJECT_ID = 'project-id'
CLOUDSQL_INSTANCE_NAME = 'instance-name'

SQLALCHEMY_DATABASE_URI = 'mysql+mysqldb://root@/{db_name}?unix_socket=/cloudsql/{project_id}:{instance_name}'.format(db_name = CLOUDSQL_DB_NAME,
                                                                                                                      project_id = GAE_PROJECT_ID,
                                                                                                                      instance_name = CLOUDSQL_INSTANCE_NAME)

app.yaml

libraries:
- name: MySQLdb
  version: latest
- name: jinja2
  version: 2.6

# [START env_variables]
env_variables:
    CLOUDSQL_CONNECTION_NAME: connection-name
    CLOUDSQL_USER: root
    CLOUDSQL_PASSWORD: password
# [END env_variables]

This is my first app using Flask on GAE, so I'm not sure if I'm missing something here...

1

1 Answers

0
votes

In case anyone else has this issue, I figured out the problem. It turns out the documentation for the URI was slightly incomplete since I'm using Flask instead of Webapp2, and I was missing a couple of attributes.

Here's the correct config.py that fixed the problem:

# Google Cloud SQL information
CLOUDSQL_USER = 'root'
CLOUDSQL_PASSWORD = '********'
CLOUDSQL_DB_NAME = 'db-name'
GAE_PROJECT_ID = 'project-name'
CLOUDSQL_CONNECTION_NAME = 'connection-name'


SQLALCHEMY_DATABASE_URI = (
    'mysql+mysqldb://{user}:{password}@localhost/{database}'
    '?unix_socket=/cloudsql/{connection_name}').format(
        user=CLOUDSQL_USER, password=CLOUDSQL_PASSWORD,
        database=CLOUDSQL_DB_NAME, connection_name=CLOUDSQL_CONNECTION_NAME)