mysql问题之Communications link failure

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值