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

- **ID:** `python/flask-sqlalchemy-operational-error-mysql-gone-away`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 2.x | active | — | — |
| 3.x | active | — | — |

## 解决方案

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)
   ```

## 无效尝试

- **** — Only delays the problem; bad connections still get reused. (65% 失败率)
- **** — Disables pooling; performance collapses under load. (80% 失败率)
