MySQL server has gone away解决办法

发布于 2019-02-15  1.89k 次阅读


请耐心看完全部

 

情况1. MySQL服务宕机

执行以下命令,查看mysql的运行时长。

mysql> show global status like 'uptime';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Uptime        | 10170 |
+---------------+-------+

 

uptime数值很大,表明mysql服务运行很久,说明最近MySQL服务器没有重启过。

或者查看MySQL的报错日志,看看有没有重启的信息。

datou:~$ tail /var/log/mysql/error.log
170914 19:44:37 InnoDB: Completed initialization of buffer pool
170914 19:44:37 InnoDB: highest supported file format is Barracuda.
170914 19:44:37  InnoDB: Waiting for the background threads to start
170914 19:44:38 InnoDB: 5.5.57 started; log sequence number 58681764
170914 19:44:38 [Note] Server hostname (bind-address): '127.0.0.1'; port: 3306
170914 19:44:38 [Note]   - '127.0.0.1' resolves to '127.0.0.1';
170914 19:44:38 [Note] Server socket created on IP: '127.0.0.1'.
170914 19:44:38 [Note] Event Scheduler: Loaded 0 events
170914 19:44:38 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.5.57-0ubuntu0.14.04.1'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu)

 

如果日志没有相关信息,也表明mysql服务最近没有重启过,可以继续检查下面几项情况。

情况2. 连接超时

如果程序使用的是长连接,则这种情况的可能性会比较大。
即,某个长连接很久没有新的请求发起,达到了server端的timeout,被server强行关闭。
此后再通过这个connection发起查询的时候,就会报错server has gone away。

mysql> show global variables like '%timeout';
+----------------------------+----------+
| Variable_name              | Value    |
+----------------------------+----------+
| connect_timeout            | 10       |
| delayed_insert_timeout     | 300      |
| innodb_lock_wait_timeout   | 50       |
| innodb_rollback_on_timeout | OFF      |
| interactive_timeout        | 28800    |
| lock_wait_timeout          | 31536000 |
| net_read_timeout           | 30       |
| net_write_timeout          | 60       |
| slave_net_timeout          | 3600     |
| wait_timeout               | 28800    |
+----------------------------+----------+
10 rows in set (0.00 sec)

如下命令设置连接超时为5秒。

mysql> SET SESSION wait_timeout=5;
Query OK, 0 rows affected (0.00 sec)

再执行SELECT NOW();,通过这个connection发起查询的时候,就会报错server has gone away。

mysql> SELECT NOW();
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    41
Current database: study

+---------------------+
| NOW()               |
+---------------------+
| 2017-09-14 23:12:53 |
+---------------------+
1 row in set (0.01 sec)

 

实际上wait_timeout=28800,不是造成文章开头的原因。

情况3. 进程在server端被主动kill

这种情况和情况2相似,只是发起者是DBA或者其他job。发现有长时间的慢查询执行kill xxx导致。

mysql> show global status like 'com_kill';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_kill      | 0     |
+---------------+-------+
1 row in set (0.00 sec)

 

情况4. Your SQL statement was too large.

当查询的结果集超过 max_allowed_packet 也会出现这样的报错。
查看执行SQL执行文件大小是否超过 max_allowed_packet ,如果超过则需要调整参数,或者优化语句。

mysql> show global variables like 'max_allowed_packet';
+--------------------+----------+
| Variable_name      | Value    |
+--------------------+----------+
| max_allowed_packet | 16777216 |
+--------------------+----------+
1 row in set (0.00 sec)

 

计算发现SQL执行文件最大只能是16M,而文章开头执行的a.sql有24M。
修改参数,max_allowed_packet 调整为28M。

mysql> set global max_allowed_packet=1024*1024*28;
Query OK, 0 rows affected (0.00 sec)

mysql> show global variables like 'max_allowed_packet';
+--------------------+----------+
| Variable_name      | Value    |
+--------------------+----------+
| max_allowed_packet | 29360128 |
+--------------------+----------+
1 row in set (0.00 sec)

 

1. 修改 连接MySQL的php文件

增加 PDO::ATTR_PERSISTENT => false, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,

如下:

$PDO = @new PDO("mysql:host=$DB_HOST;dbname=$DB_NAME","$DB_USER","$DB_PASSWORD", array(PDO::ATTR_PERSISTENT => false, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8';"));

 

2.修改 php.ini
vim  /etc/php.ini

mysqli.reconnect = Off

修改成:

mysqli.reconnect = On

 

3.修改 程序,增加检测

注意此处使用的是mysqli的扩展,非mysql扩展

function ping(){
    if(!mysqli_ping($this->link)){
      mysqli_close($this->link); //注意:一定要先执行数据库关闭,这是关键
      $this->connect();
    }
}

 


公交车司机终于在众人的指责中将座位让给了老太太