Python and SQLAlchemy error -pymysql.err.OperationalError(2006, “MySQL server has gone away …


After sitting for a few minutes and then attempting to reconnect to your database through the application (read, add, delete, or update functions) you receive the error:

pymysql.err.OperationalError) (2006, “MySQL server has gone away (BrokenPipeError(32, ‘Broken pipe’)

Insert the following


app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {"pool_pre_ping": True, "pool_recycle": 300,}

https://medium.com/@heyjcmc/controlling-the-flask-sqlalchemy-engine-a0f8fae15b47

 

Insert the following into app.py, just after creating the database connection.


# Prevent --> pymysql.err.OperationalError) (2006, "MySQL server has gone away (BrokenPipeError(32, 'Broken pipe') class SQLAlchemy(SQLAlchemy): def apply_pool_defaults(self, app, options): super(SQLAlchemy, self).apply_pool_defaults(app, options) options["pool_pre_ping"] = True # <-- MWC