Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

1 Answers

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.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!