python network_error ai_generated true

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2006, 'MySQL 服务器已断开')

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

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

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-09-26首次发现

版本兼容性

版本状态引入弃用备注
2.x active — — —
3.x active — — —

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 65% 失败

    Only delays the problem; bad connections still get reused.

  2. 80% 失败

    Disables pooling; performance collapses under load.