Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
mysql问题之Communications link failure
公司生产环境突然有一天程序报错,Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure,第一反应,mysql的问题,用本地的mysql连接工具连接,发现没有问题,查看网上解决方案。
问题描述:
[2020-07-07 11:18:12] [ERROR] http-apr-8080-exec-1 com.alibaba.druid.pool.DruidDataSource 1190: discard connection
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 134,949 milliseconds ago. The last packet sent successfully to the server was 5 milliseconds ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
。。。省略
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
问题原因:
mysql服务器默认的“wait_timeout”是28800秒即8小时,意味着如果一个连接的空闲时间超过8个小时,MySQL将自动断开该连接,而连接池却认为该连接还是有效的(因为并未校验连接的有效性),当应用申请使用该连接时,就会导致上面的报错。
解决问题方案:
(1)登录到mysql服务器
mysql -h主机地址 -u用户名 -p用户密码(例如 mysql -hlocalhost -uroot -p123456)
或本地mysql连接工具亦可
(2)查看wait_timeout(mysql默认的wait_timeout为28800)
show global variables like ‘wait_timeout’;
(3)修改wait_timeout(本文设置为604800,可根据具体需要进行设置)
set global wait_timeout=604800;
set global interactive_timeout=604800;
备注:需要登录mysql的账户权限足够,本文使用的是root账户
参考文档:
https://www.jianshu.com/p/06568c5e5888
https://blog.csdn.net/huoyuanshen/article/details/52037842