python
network_error
ai_generated
true
SQLAlchemy 操作错误:查询期间丢失与 MySQL 服务器的连接。
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server during query')
ID: python/mysql-connection-reset
80%修复率
87%置信度
0证据数
2024-10-02首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.4.x | active | — | — | — |
| 2.0.x | active | — | — | — |
根因分析
MySQL 服务器关闭了连接,可能是由于超时、服务器重启或网络问题。
English
MySQL 服务器关闭了连接,可能是由于超时、服务器重启或网络问题。
解决方案
-
90% 成功率
使用 SQLAlchemy 的 pool_pre_ping=True 参数,自动检测并重建死连接。
-
85% 成功率
设置连接池回收时间:pool_recycle=3600
无效尝试
常见但无效的做法:
-
40% 失败
增加 MySQL 的 wait_timeout,但需要重启服务器且影响所有连接。
-
60% 失败
在应用层无限重试,但可能造成资源浪费和死循环。