python network_error ai_generated true

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server during query')

ID: python/mysql-connection-reset

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-10-02First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.4.x active
2.0.x active

Root Cause

MySQL 服务器关闭了连接,可能是由于超时、服务器重启或网络问题。

generic

中文

MySQL 服务器关闭了连接,可能是由于超时、服务器重启或网络问题。

Workarounds

  1. 90% success
    使用 SQLAlchemy 的 pool_pre_ping=True 参数,自动检测并重建死连接。
  2. 85% success
    设置连接池回收时间:pool_recycle=3600

Dead Ends

Common approaches that don't work:

  1. 40% fail

    增加 MySQL 的 wait_timeout,但需要重启服务器且影响所有连接。

  2. 60% fail

    在应用层无限重试,但可能造成资源浪费和死循环。