2013
php
network_error
ai_generated
true
PDOException: SQLSTATE[HY000] [2013] Lost connection to MySQL server during query in /var/www/app/src/Database/QueryBuilder.php on line 78
ID: php/pdo-mysql-query-timeout
80%Fix Rate
86%Confidence
1Evidence
2024-06-05First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 7.4 | active | — | — | — |
| 8.0 | active | — | — | — |
| 8.1 | active | — | — | — |
| 8.2 | active | — | — | — |
| 8.3 | active | — | — | — |
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.
generic中文
长时间运行的查询超过了 MySQL 的 wait_timeout 或 net_read_timeout,或者 MySQL 服务器在查询过程中重启或崩溃,导致连接断开。
Official Documentation
https://dev.mysql.com/doc/refman/8.0/en/error-messages-client.html#error_cr_server_gone_errorWorkarounds
-
85% success 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
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
-
80% success 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 }
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 } -
75% success 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); } }
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); } }
中文步骤
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); } }
Dead Ends
Common approaches that don't work:
-
85% fail
PDO timeout affects connection attempts, not query execution. The MySQL server's own timeout settings are the limiting factor.
-
90% fail
Persistent connections can help with connection overhead but do not prevent timeouts during queries.