Got timeout reading communication packets解决方法

2023-06-12,,

Got timeout reading communication packets解决方法

http://www.th7.cn/db/mysql/201702/225243.shtml

[Note] Aborted connection xxxx to db:

问题现象:在tail -f/data/logs/mysql/error.log日志中出现大量的如下信息(web用的是Zabbix,设置连接超时时间为100秒):

' host: 'localhost' (Got timeout reading communication packets)
2017-02-05T15:30:19.272811+08:00 28546 [Note] Aborted connection 28546 to db: 'zabbix' user: 'zabbix' host: 'localhost' (Got timeout reading communication packets)
2017-02-05T15:30:22.388624+08:00 28547 [Note] Aborted connection 28547 to db: 'zabbix' user: 'zabbix' host: 'localhost' (Got timeout reading communication packets)
2017-02-05T15:30:27.119216+08:00 28554 [Note] Aborted connection 28554 to db: 'zabbix' user: 'zabbix' host: 'localhost' (Got timeout reading communication packets)

解决办法:
修改[root@lovebuy114 ~]# grep timeout /etc/my.cnf
interactive_timeout = 120
wait_timeout = 120

log_warnings=1 //注意,我这里原来是2。修改成1后,问题现象果然但是已经不存在了。

在命令行中可以这样修改:
mysql>set global log_warning=1;
mysql>set global interactive_timeout = 120;
mysql>set global wait_timeout = 120;

参数简要说明:
1)interactive_timeout:
参数含义:服务器关闭交互式连接前等待活动的秒数。交互式客户端定义为在mysql_real_connect()中使用CLIENT_INTERACTIVE选项的客户端。
参数默认值:28800秒(8小时)

解决无Notice的办法:
grep timeout /etc/my.cnf innodb_lock_wait_timeout = 60
interactive_timeout = 28800
wait_timeout = 22
grep log_warnings /etc/my.cnflog_warnings=2
From:http://blog.csdn.net/jamesyao008/article/details/45098073

修改后,无效,原因是现在是变成了Notice,不是警告:

tail -f/data/logs/mysql/error.log
2017-02-05T15:38:19.678134+08:00 128 [Note] Aborted connection 128 to db: 'zabbix' user: 'zabbix' host: 'localhost' (Got timeout reading communication packets)
2017-02-05T15:38:22.452504+08:00 131 [Note] Aborted connection 131 to db: 'zabbix' user: 'zabbix' host: 'localhost' (Got timeout reading communication packets)


连接、网络类超时

http://www.cnblogs.com/xiaoboluo768/p/6222862.html

共有如下几个:
connect_timeout:默认为10S
wait_timeout:默认是8小时,即28800秒
interactive_timeout:默认是8小时,即28800秒
net_read_timeout:默认是30S
net_write_timeout:默认是60S

handshake流程

在TCP三次握手的基础之上,简历MySQL通讯协议的连接,这个连接建立过程受connect_timeout参数控制
    --------------------TCP established--------------------
    MySQL Server(10.10.20.96)------->Client(10.10.20.51)
    Client(10.10.20.51)------->MySQL Server(10.10.20.96)
    MySQL Server(10.10.20.96)------->Client(10.10.20.51)

--------------------established--------------------

在MySQL通讯协议建立连接之后,此时客户端连接的超时受wait_timeout和interactive_timeout参数控制
    建立连接后无交互:MySQL server ---wait_timeout--- Client
    建立连接交互后:MySQL server ---interactive_timeout--- Client

在如果客户端有数据包传输,那么这个数据包的传输超时由net_read_timeout和net_write_timeout参数控制
    -------------------client与server端有数据传输时-------------------
    client ----->MySQL Server(net_read_timeout)
    client <-----MySQL Server(net_write_timeout)

从上面的结果中可以看到,第一个会话中修改global wait_timeout=5之后,新的连接上来,超过5秒没有发送新的数据包,连接就被断开。

net_write_timeout
mysql服务端向客户端写(发送)数据时,服务端等待客户端响应的超时时间,当服务端正在写数据到客户端时,net_write_timeout控制何时超时

net_read_timeout
mysql服务端从客户端读取(接收)数据时,服务端等待客户端响应的超时时间,当服务端正在从客户端读取数据时,net_read_timeout控制何时超时


MySQL · 答疑解惑 · MySQL 的那些网络超时错误
http://mysql.taobao.org/monthly/2017/05/04/

阿里云内核月报

前言

我们在使用/运维 MySQL 过程中,经常会遇到一些网络相关的错误,比如:
Aborted connection 134328328 to db: 'test' user: 'root' host: '127.0.0.1' (Got timeout reading communication packets)

MySQL 的网络超时相关参数有好几个,这个超时到底是对应哪个参数呢?
在之前的月报中,我们介绍过 MySQL 的 网络通信模块 ,包括各模块间的关系,数据网络包是如何发送接受的,以及结果集的数据格式,大家可以先回顾下。

这里我们对 mysqld 处理网络包时,遇到的超时异常情况进行分析,希望大家在遇到网络相关的报错时,能更好理解和排查问题。
问题分析

MySQL 是平等网络协议,就是说 client 和 server 之间的网络交互是一来一回的,client 发送完请求后,必须等待 server 响应包回来,才能发下一个请求。
对 mysqld 来说,就是接收网络请求,然后内部处理,将结果集返回给客户端,然后等待下一个请求:

先看下 mysqld server 和网络超时相关的参数有哪些:
interactive_timeout
wait_timeout
net_read_timeout
net_write_timeout
connect_timeout

在底层实现上,不管是读还是写操作,超时都是通过 poll(&pfd, 1, timeout) 做的,参数之间的区别是针对连接的不同状态。

读超时
wait_timeout 是给读请求用的,在 do_command 开始就做设置:
my_net_set_read_timeout(net, thd->variables.net_wait_timeout);

这个时候,连接是空闲的,等待用户的请求。
等读完用户的请求包后,连接就变成 active 的,在调用 dispatch_command 执行 SQL 前,通过
my_net_set_read_timeout(net, thd->variables.net_read_timeout);
把超时设置回 net_read_timeout,之后在执行 SQL 请求过程中,server 和 client 基本不会有网络交互,所以这个超时基本用不上。
有一个特殊的情况是 LOAD DATA LOCAL FILE 命令,server 在执行过程中,需要和 client 再做网络交互。

interactive_timeout 是给交互模式的客户端使用的,比如我们常用的 mysql client 工具,这个是在认证过程中设置的,逻辑如下:
static void
server_mpvio_update_thd(THD *thd, MPVIO_EXT *mpvio)
{
  thd->client_capabilities= mpvio->client_capabilities;
  thd->max_client_packet_length= mpvio->max_client_packet_length;
  if (mpvio->client_capabilities & CLIENT_INTERACTIVE)
    thd->variables.net_wait_timeout= thd->variables.net_interactive_timeout;
  thd->security_ctx->user= mpvio->auth_info.user_name;
  if (thd->client_capabilities & CLIENT_IGNORE_SPACE)
    thd->variables.sql_mode|= MODE_IGNORE_SPACE;
}
如果客户端的能力位上设置了 CLIENT_INTERACTIVE,会用 interactive_timeout 的值覆盖 wait_timeout 的值。
而一般情况下,我们应用在建立连接时,是不会设置这个能力位的。

写超时
net_write_timeout 对应写超时,在连接认证完成后,server 和 client 交互过程中写超时一真是不变的。

认证超时
connect_timeout 是给连接认证过程用的,读和写都用这个值,认证完成后,读和写分别设置为 net_read_timeout 和 net_write_timeout。

总结

可以看到和读相关的超时参数是最多的,也比较容易搞混乱。

如果是认证过程中超时,不管是读还是,都是 connect_timeout;
对于读网络超时,一般是 wait_timeout/interactive_timeout,基本不会是 net_read_timeout(特例是业务用到 LOAD DATA LOCAL FILE);
对于写网络超时,都是 net_write_timeout。

在遇到超时情况下,可以根据这些原则判断对那个参数做调整。

比如下面这种情况:
2017-05-15 19:32:41 47930 [Warning] Aborted connection 6 to db: 'unconnected' user: 'root' host: 'localhost' (Got timeout reading communication packets)

很可能需要调整的 wait_timeout/interactive_timeout。
2017-05-15 20:06:27 5063 [Warning] Aborted connection 12 to db: 'test' user: 'root' host: 'localhost' (Got timeout writing communication packets)

需要调整 net_write_timeout

需要注意的是,MySQL 的关于网络的错误,除了超时以外都认为是 error,没有做进一步的细分,比如可能会看到下面这种日志,有可能是客户端异常退出了,也有可能是网络链路异常。
2017-05-15 19:34:57 47930 [Warning] Aborted connection 8 to db: 'unconnected' user: 'root' host: 'localhost' (Got an error reading communication packets)

2017-05-15 20:07:39 5063 [Warning] Aborted connection 13 to db: 'test' user: 'root' host: 'localhost' (Got an error writing communication packets)

f

Got timeout reading communication packets解决方法的相关教程结束。

《Got timeout reading communication packets解决方法.doc》

下载本文的Word格式文档,以方便收藏与打印。