python network_error ai_generated true

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2006, 'MySQL server has gone away')

ID: python/flask-sqlalchemy-operational-error-mysql-gone-away

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-09-26First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2.x active — — —
3.x active — — —

Root Cause

Idle connections in the SQLAlchemy pool timed out server-side; pool_recycle not configured and pool_pre_ping off.

generic

中文

SQLAlchemy 连接池中的空闲连接在服务端超时;未配置 pool_recycle 且 pool_pre_ping 关闭。

Workarounds

  1. 93% success
    app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {'pool_pre_ping': True, 'pool_recycle': 280}
  2. 92% success
    In create_engine: create_engine(url, pool_pre_ping=True, pool_recycle=280)

Dead Ends

Common approaches that don't work:

  1. 65% fail

    Only delays the problem; bad connections still get reused.

  2. 80% fail

    Disables pooling; performance collapses under load.