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
http://blog.itpub.net/22664653/viewspace-2143671
https://www.cnblogs.com/ivictor/p/5979731.html