# PDO异常：SQLSTATE[HY000] [2006] MySQL服务器已断开连接，位于/var/www/app/src/Database/Connection.php:42

- **ID:** `php/pdo-mysql-server-has-gone-away`
- **领域:** php
- **类别:** network_error
- **错误码:** `2006`
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

MySQL服务器因超时（wait_timeout或interactive_timeout）、数据包大小超过max_allowed_packet或服务器崩溃/重启而关闭连接，且PHP PDO连接未重新建立。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| PHP 7.4 | active | — | — |
| PHP 8.1 | active | — | — |
| PHP 8.2 | active | — | — |
| MySQL 5.7 | active | — | — |
| MySQL 8.0 | active | — | — |

## 解决方案

1. ```
   Increase MySQL's wait_timeout and interactive_timeout in my.cnf, e.g., set wait_timeout=28800; interactive_timeout=28800. Then restart MySQL.
   ```
2. ```
   Increase max_allowed_packet in my.cnf to handle large queries, e.g., set max_allowed_packet=64M. Restart MySQL.
   ```
3. ```
   Implement connection retry logic in PHP: catch the exception and reconnect using a new PDO instance. Example:
try {
    $stmt = $pdo->query('SELECT 1');
} catch (\PDOException $e) {
    if ($e->getCode() == 2006) {
        $pdo = new PDO($dsn, $user, $pass);
        $stmt = $pdo->query('SELECT 1');
    }
}
   ```

## 无效尝试

- **** — The error is caused by MySQL's timeout, not PHP's execution time; changing PHP settings does not prevent the server from closing idle connections. (70% 失败率)
- **** — Persistent connections can mask the issue but may lead to stale connections being reused, causing the same error later; they also consume server resources. (50% 失败率)
- **** — Restarting temporarily reopens connections, but the underlying timeout or packet size issue remains, causing the error to recur. (80% 失败率)
