MySql连接报错communications link failure疑难问题

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

1 出现原因

java连接数据库时出现Communications link failure怎么都登录不上
在这里插入图片描述

2 解决方案(我这种情况无效)

方法1

【1】登录mysql,cmd命令
mysql -h主机地址 -u用户名 -p用户密码(例如 mysql -hlocalhost -uroot -p123456)

【2】查看wait_timeout,cmd命令:
show global variables like 'wait_timeout';


【3】若wait_timeout太小,修改之。cmd命令:
set global wait_timeout=604800;
set global interactive_timeout=604800;

方法2

连接URL上添加参数:&autoReconnect=true

3 其他原因排查

如果试过上面两个方法都没有解决的话,排查服务器环境,及数据库端口是否有问题。

经过排查,如果数据库端口通过nginx反向代理,或者数据库端口通过其他服务器进行代理操作的话,是nginx反向代理超时导致的无法连接数据库

需要修改nginx的配置文件,通过steam代理mysl

stream {
     upstream mysql {
         zone myapp1 64k;
         server localhost:3306 weight=1 max_fails=3 fail_timeout=30s;
     }
     server {
         listen 10086;
         proxy_connect_timeout 1s;
         proxy_timeout 3s;
         proxy_pass mysql;
    }
}

其他情况,也可以同理排查mysql端口是否进行过代理

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

似一筷扣肉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值