MySQL每隔一小会不操作报错ERROR 2013 (HY000) Lost connection to MySQL server during query问题
连接OA的MySQL库发现每隔一小会不操作就报错如下:
mysql> select now(); +---------------------+ | now() | +---------------------+ | 2022-01-11 09:47:41 | +---------------------+ 1 row in set (0.00 sec) mysql> select now(); ERROR 2013 (HY000): Lost connection to MySQL server during query mysql> select now(); ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 453751 Current database: *** NONE *** +---------------------+ | now() | +---------------------+ | 2022-01-11 09:48:27 | +---------------------+ 1 row in set (0.00 sec)
实际上有两个参数如下:
interactive_timeout = 1800 ##交互式会话空闲超时时间(mysql工具、mysqldump等)
wait_timeout = 1800 ##非交互式会话空闲超时时间,mysql api程序,jdbc连接数据库等
查看数据库的这两个参数值(单位:s):
mysql> show variables where Variable_name in ('interactive_timeout','wait_timeout'); +---------------------+-------+ | Variable_name | Value | +---------------------+-------+ | interactive_timeout | 30 | | wait_timeout | 30 | +---------------------+-------+ 2 rows in set (0.00 sec)
由于我是使用mysql数据库本身的命令行交互式客户端mysql,因此调整调整interactive_timeout的值即可。
通过调整interactive_timeout=10,30,60后,会话1连接进去不操作,会话2持续观察:
会话2:
mysql> show full processlist; +--------+------+---------------------+-------+---------+------+-------+-----------------------+ | Id | User | Host | db | Command | Time | State | Info | +--------+------+---------------------+-------+---------+------+-------+-----------------------+ | 2 | root | ::1:53493 | td_oa | Sleep | 1 | | NULL | | 41 | root | ::1:53566 | td_oa | Sleep | 28 | | NULL | | 45 | root | ::1:53574 | td_oa | Sleep | 20 | | NULL | | 1709 | root | ::1:56311 | td_oa | Sleep | 68 | | NULL | | 454546 | root | 192.168.1.188:54046 | NULL | Sleep | 7 | | NULL | | 454553 | root | 192.168.1.188:54048 | NULL | Query | 0 | init | show full processlist | +--------+------+---------------------+-------+---------+------+-------+-----------------------+ 6 rows in set (0.00 sec)
差不多在Time列接近和超过两个时间点操作会话1,会发现在接近但没超过interactive_timeout的时候操作会话1不会报错。
在刚刚超过interactive_timeout的时候,会话1就立刻报错:ERROR 2013 (HY000): Lost connection to MySQL server during query
若是更改wait_timeout的值则无法对交互式的mysql生效,一开始还调整这个参数一直不生效。
最后设置interactive_timeout的值为300,注意这里由于global因此只对新回话生效:
mysql> set global interactive_timeout=300; Query OK, 0 rows affected (0.00 sec) mysql> show global variables where Variable_name in ('interactive_timeout','wait_timeout'); +---------------------+-------+ | Variable_name | Value | +---------------------+-------+ | interactive_timeout | 300 | | wait_timeout | 30 | +---------------------+-------+ 2 rows in set (0.00 sec) mysql> show variables where Variable_name in ('interactive_timeout','wait_timeout'); +---------------------+-------+ | Variable_name | Value | +---------------------+-------+ | interactive_timeout | 300 | | wait_timeout | 300 | +---------------------+-------+ 2 rows in set (0.00 sec)
wait_timeout受影响,详细的可以参考这篇写的不错的文章:http://blog.itpub.net/22664653/viewspace-2143671
对于数据库,非交互式的客户端才是连接大军。
mysql> show status like 'aborted%'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | Aborted_clients | 1061 | | Aborted_connects | 3 | +------------------+-------+ 2 rows in set (0.00 sec)
如果状态Aborted_clients表示由于客户端没有正确关闭连接而中止的连接数。
当Aborted Clients增大的时候意味着有客户端成功建立连接,但是由于某些原因断开连接或者被终止了。
另外,Aborted_connects表示尝试连接到MySQL服务器失败的次数。这个状态变量可以结合host_cache表和其错误日志一起来分析问题。
1、客户端没有权限但是尝试访问MySQL数据库。
2、 客户端输入的密码有误。
3、 A connection packet does not contain the right information.
4、 超过连接时间限制,主要是这个系统变量connect_timeout控制(mysql默认是10s,基本上,除非网络环境极端不好,一般不会超时。)
参考链接
http://blog.itpub.net/24113018/viewspace-1783921/
https://zhuanlan.zhihu.com/p/88138358