Skip to content

Instantly share code, notes, and snippets.

@sokil
Last active November 5, 2024 20:11
Show Gist options
  • Select an option

  • Save sokil/c143d4a2fefd96c9c017d53a155ec709 to your computer and use it in GitHub Desktop.

Select an option

Save sokil/c143d4a2fefd96c9c017d53a155ec709 to your computer and use it in GitHub Desktop.
Mysql Server Has Gove Away
  • Error Code: 2013. Lost connection to MySQL server during query
  • Error Code: 2006. MySQL server has gone away
  • Error Code: 2003. Lost connection to backend server

Set global timeout:

mysql> SET GLOBAL wait_timeout=10;

Don forget to set interactive timeout if cli used:

mysql> SET GLOBAL interactive_timeout=10;
$p = new PDO('mysql:host=localhost;dbname=mysql', 'root', 'root');  
var_dump($p->errorInfo());
php shell code:1:
array(3) {
  [0] =>
  string(0) ""
  [1] =>
  NULL
  [2] =>
  NULL
}
var_dump($p->query('select @@wait_timeout')->fetchAll());
php shell code:1:
array(1) {
  [0] =>
  array(2) {
    '@@wait_timeout' =>
    string(2) "10"
    [0] =>
    string(2) "10"
  }
}
var_dump($p->errorInfo());
php shell code:1:
array(3) {
  [0] =>
  string(5) "00000"
  [1] =>
  NULL
  [2] =>
  NULL
}

WAIT 10 SECONDS ....

$p->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
var_dump($stmt->execute());
PHP Warning:  Error while sending QUERY packet. PID=26299 in php shell code on line 1
PHP Stack trace:
PHP   1. {main}() php shell code:0
PHP   2. PDOStatement->execute() php shell code:1
PHP Warning:  PDOStatement::execute(): SQLSTATE[HY000]: General error: 2006 MySQL server has gone away in php shell code on line 1
PHP Stack trace:
PHP   1. {main}() php shell code:0
PHP   2. PDOStatement->execute() php shell code:1
php shell code:1:
bool(false)
var_dump($p->errorInfo());
php shell code:1:
array(3) {
  [0] =>
  string(5) "00000"
  [1] =>
  NULL
  [2] =>
  NULL
}
var_dump($p->query('select @@wait_timeout'));
PHP Warning:  PDO::query(): SQLSTATE[HY000]: General error: 2006 MySQL server has gone away in php shell code on line 1
PHP Stack trace:
PHP   1. {main}() php shell code:0
PHP   2. PDO->query() php shell code:1
php shell code:1:
bool(false)
var_dump($p->errorInfo());
php shell code:1:
array(3) {
  [0] =>
  string(5) "HY000"
  [1] =>
  int(2006)
  [2] =>
  string(26) "MySQL server has gone away"
}
$p->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$stmt = $p->prepare('select @@wait_timeout');
var_dump($stmt->execute());
PHP Warning:  Error while sending QUERY packet. PID=26299 in php shell code on line 1
PHP Stack trace:
PHP   1. {main}() php shell code:0
PHP   2. PDOStatement->execute() php shell code:1
PHP Warning:  Uncaught PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away in php shell code:1
Stack trace:
#0 php shell code(1): PDOStatement->execute()
#1 {main}
  thrown in php shell code on line 1
var_dump($p->errorInfo());
php shell code:1:
array(3) {
  [0] =>
  string(5) "00000"
  [1] =>
  NULL
  [2] =>
  NULL
}
var_dump($p->query('select @@wait_timeout'));
PHP Warning:  Uncaught PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away in php shell code:1
Stack trace:
#0 php shell code(1): PDO->query('select @@wait_t...')
#1 {main}
  thrown in php shell code on line 1
php > var_dump($p->errorInfo());
php shell code:1:
array(3) {
  [0] =>
  string(5) "HY000"
  [1] =>
  int(2006)
  [2] =>
  string(26) "MySQL server has gone away"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment