admin管理员组

文章数量:1618699

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端口是否进行过代理

本文标签: 报错疑难问题mysqlCommunicationsfailure