{
  "id": "php/pdo-mysql-query-timeout",
  "signature": "PDOException: SQLSTATE[HY000] [2013] Lost connection to MySQL server during query in /var/www/app/src/Database/QueryBuilder.php on line 78",
  "signature_zh": "PDOException：SQLSTATE[HY000] [2013] 查询期间与 MySQL 服务器失去连接，位于 /var/www/app/src/Database/QueryBuilder.php 第 78 行",
  "regex": "/SQLSTATE\\[HY000\\] \\[2013\\] Lost connection to MySQL server during query/",
  "domain": "php",
  "category": "network_error",
  "subcategory": null,
  "root_cause": "A long-running query exceeds MySQL's wait_timeout or net_read_timeout, or the MySQL server restarted or crashed mid-query, causing the connection to drop.",
  "root_cause_type": "generic",
  "root_cause_zh": "长时间运行的查询超过了 MySQL 的 wait_timeout 或 net_read_timeout，或者 MySQL 服务器在查询过程中重启或崩溃，导致连接断开。",
  "versions": [
    {
      "version": "7.4",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "8.1",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "8.2",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "8.3",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "PDO timeout affects connection attempts, not query execution. The MySQL server's own timeout settings are the limiting factor.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Persistent connections can help with connection overhead but do not prevent timeouts during queries.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Increase MySQL's wait_timeout and net_read_timeout in my.cnf: [mysqld] wait_timeout = 600 net_read_timeout = 600. Restart MySQL: sudo systemctl restart mysql",
      "success_rate": 0.85,
      "how": "Increase MySQL's wait_timeout and net_read_timeout in my.cnf: [mysqld] wait_timeout = 600 net_read_timeout = 600. Restart MySQL: sudo systemctl restart mysql",
      "condition": "",
      "sources": []
    },
    {
      "action": "Optimize the slow query by adding indexes or splitting it into smaller chunks. Example: $chunks = array_chunk($largeArray, 1000); foreach ($chunks as $chunk) { // execute query with chunk }",
      "success_rate": 0.8,
      "how": "Optimize the slow query by adding indexes or splitting it into smaller chunks. Example: $chunks = array_chunk($largeArray, 1000); foreach ($chunks as $chunk) { // execute query with chunk }",
      "condition": "",
      "sources": []
    },
    {
      "action": "Implement a retry mechanism with exponential backoff in PHP: for ($attempt = 1; $attempt <= 3; $attempt++) { try { $stmt->execute(); break; } catch (PDOException $e) { if ($e->getCode() !== '2013') throw $e; usleep(pow(2, $attempt) * 100000); } }",
      "success_rate": 0.75,
      "how": "Implement a retry mechanism with exponential backoff in PHP: for ($attempt = 1; $attempt <= 3; $attempt++) { try { $stmt->execute(); break; } catch (PDOException $e) { if ($e->getCode() !== '2013') throw $e; usleep(pow(2, $attempt) * 100000); } }",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Increase MySQL's wait_timeout and net_read_timeout in my.cnf: [mysqld] wait_timeout = 600 net_read_timeout = 600. Restart MySQL: sudo systemctl restart mysql",
    "Optimize the slow query by adding indexes or splitting it into smaller chunks. Example: $chunks = array_chunk($largeArray, 1000); foreach ($chunks as $chunk) { // execute query with chunk }",
    "Implement a retry mechanism with exponential backoff in PHP: for ($attempt = 1; $attempt <= 3; $attempt++) { try { $stmt->execute(); break; } catch (PDOException $e) { if ($e->getCode() !== '2013') throw $e; usleep(pow(2, $attempt) * 100000); } }"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://dev.mysql.com/doc/refman/8.0/en/error-messages-client.html#error_cr_server_gone_error",
  "official_doc_section": null,
  "error_code": "2013",
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-06-05",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}