user6704961
user6704961

Reputation:

SQLAlchemy AppEngine standard - Lost connection to MySQL server

I'm trying to connect to a Google Cloud SQL second generation in Python from AppEngine standard (Python 2.7). Until now, I was using MySQLDB driver directly and it was fine.

I've tried to switch to SQLAlchemy, but now I'm always having this error when the code is deployed (it seems to work fine in local) resulting in a error 500 (It's not just some connections which are lost, it constantly fails) :

OperationalError: (_mysql_exceptions.OperationalError) (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 38") (Background on this error at: http://sqlalche.me/e/e3q8)

I don't understand because the setup doesn't differ from before, so it must be related to the way I use SQLAlchemy.

I use something like this :

create_engine("mysql+mysqldb://appuser:[email protected]/db_name?unix_socket=/cloudsql/gcpProject:europe-west1:instanceName")

I've tried different values (with, without the ip, ...). But it is still the same. Is is a version compatibility problem ?

I use MySQL-python in the app.yaml and SQLAlchemy 1.2.4 :

app.yaml :

 - name: MySQLdb
   version: "latest"

requirements.txt :

SQLAlchemy==1.2.4

Upvotes: 3

Views: 323

Answers (2)

user6704961
user6704961

Reputation:

It was a problem in the url. I was adding in a specific part of the code "/dbname" at the end of the connection string, resulting in something like this :

mysql+mysqldb://appuser:password@/db_name?unix_socket=/cloudsql/gcpProject:europe-west1:instanceName/dbname

So in the end, the meaning of this error can also be that the unix socket is wrong.

Upvotes: 1

oakinlaja
oakinlaja

Reputation: 906

There are a number of causes for connection loss to Google CloudSQL server but quite rightly, you have to ensure that your setup is appropriate first. I don't think this issue is about version compatibility.

According to the documentation, for your application to be able to connect to your Cloud SQL instance when the app is deployed, you require to add the user, password, database, and instance connection name variables from Cloud SQL to the related environment variables in the app.yaml file(Your displayed app.yaml does not seem to contain these environment variables).

I recommend you review the details in the link for details on how to set up your CloudSQL instance and connecting to the instance.

Upvotes: 0

Related Questions