{
  "id": "php/pdo-mysql-server-has-gone-away",
  "signature": "PDOException: SQLSTATE[HY000] [2006] MySQL server has gone away in /var/www/app/src/Database/Connection.php:42",
  "signature_zh": "PDO异常：SQLSTATE[HY000] [2006] MySQL服务器已断开连接，位于/var/www/app/src/Database/Connection.php:42",
  "regex": "/SQLSTATE\\[HY000\\] \\[2006\\] MySQL server has gone away/",
  "domain": "php",
  "category": "network_error",
  "subcategory": null,
  "root_cause": "The MySQL server closed the connection due to a timeout (wait_timeout or interactive_timeout), a packet size exceeding max_allowed_packet, or a server crash/restart, and the PHP PDO connection was not re-established.",
  "root_cause_type": "generic",
  "root_cause_zh": "MySQL服务器因超时（wait_timeout或interactive_timeout）、数据包大小超过max_allowed_packet或服务器崩溃/重启而关闭连接，且PHP PDO连接未重新建立。",
  "versions": [
    {
      "version": "PHP 7.4",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "PHP 8.1",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "PHP 8.2",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "MySQL 5.7",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "MySQL 8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "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.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Persistent connections can mask the issue but may lead to stale connections being reused, causing the same error later; they also consume server resources.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Restarting temporarily reopens connections, but the underlying timeout or packet size issue remains, causing the error to recur.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Increase MySQL's wait_timeout and interactive_timeout in my.cnf, e.g., set wait_timeout=28800; interactive_timeout=28800. Then restart MySQL.",
      "success_rate": 0.75,
      "how": "Increase MySQL's wait_timeout and interactive_timeout in my.cnf, e.g., set wait_timeout=28800; interactive_timeout=28800. Then restart MySQL.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Increase max_allowed_packet in my.cnf to handle large queries, e.g., set max_allowed_packet=64M. Restart MySQL.",
      "success_rate": 0.7,
      "how": "Increase max_allowed_packet in my.cnf to handle large queries, e.g., set max_allowed_packet=64M. Restart MySQL.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Implement connection retry logic in PHP: catch the exception and reconnect using a new PDO instance. Example:\ntry {\n    $stmt = $pdo->query('SELECT 1');\n} catch (\\PDOException $e) {\n    if ($e->getCode() == 2006) {\n        $pdo = new PDO($dsn, $user, $pass);\n        $stmt = $pdo->query('SELECT 1');\n    }\n}",
      "success_rate": 0.9,
      "how": "Implement connection retry logic in PHP: catch the exception and reconnect using a new PDO instance. Example:\ntry {\n    $stmt = $pdo->query('SELECT 1');\n} catch (\\PDOException $e) {\n    if ($e->getCode() == 2006) {\n        $pdo = new PDO($dsn, $user, $pass);\n        $stmt = $pdo->query('SELECT 1');\n    }\n}",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Increase MySQL's wait_timeout and interactive_timeout in my.cnf, e.g., set wait_timeout=28800; interactive_timeout=28800. Then restart MySQL.",
    "Increase max_allowed_packet in my.cnf to handle large queries, e.g., set max_allowed_packet=64M. Restart MySQL.",
    "Implement connection retry logic in PHP: catch the exception and reconnect using a new PDO instance. Example:\ntry {\n    $stmt = $pdo->query('SELECT 1');\n} catch (\\PDOException $e) {\n    if ($e->getCode() == 2006) {\n        $pdo = new PDO($dsn, $user, $pass);\n        $stmt = $pdo->query('SELECT 1');\n    }\n}"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://dev.mysql.com/doc/refman/8.0/en/gone-away.html",
  "official_doc_section": null,
  "error_code": "2006",
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2023-11-20",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}