Periodic "Lost connection to MySQL server during query" after Dockerizing Flask Web App Periodic "Lost connection to MySQL server during query" after Dockerizing Flask Web App flask flask

Periodic "Lost connection to MySQL server during query" after Dockerizing Flask Web App


I solved this issue by migrating from a mariadb container to mysql. I still don't know what the root cause is.


Try the following to rule out that you have stale connections in your pool:

From https://docs.sqlalchemy.org/en/13/core/pooling.html#pool-disconnects

The approach adds a small bit of overhead to the connection checkout process, however is otherwise the most simple and reliable approach to completely eliminating database errors due to stale pooled connections. The calling application does not need to be concerned about organizing operations to be able to recover from stale connections checked out from the pool.

Pessimistic testing of connections upon checkout is achievable by using the Pool.pre_ping argument, available from create_engine() via the create_engine.pool_pre_ping argument:

engine = create_engine("mysql+pymysql://user:pw@host/db", pool_pre_ping=True)


I dont knwo wich versions u are using but did u try to set 'SQLALCHEMY_POOL_SIZEand 'SQLALCHEMY_POOL_RECYCLE'