阿里云部署项目时遇到的Communications link failure

症状表现:

[localhost-startStop-1] ERROR com.alibaba.druid.pool.DruidDataSource - dataSource init error
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.

我的错误

这个错误有很多原因导致的,而我的bug最低级,最不应该!!
还是记录一下,因为我遇到问题的时候搜索了很多,但是不是我这种bug.
引起原因:

If you get a SQLException: Connection refused or Connection timed out or a MySQL specific CommunicationsException: 
  Communications link failure, then it means that the DB isn't reachable at all. This can have one or more of the following causes:

0.IP address or hostname in JDBC URL is wrong.
1.Hostname in JDBC URL is not recognized by local DNS server.
2.Port number is missing or wrong in JDBC URL.
3.DB server is down.
4.DB server doesn't accept TCP/IP connections.
5.DB server has run out of connections.
6.Something in between Java and DB is blocking connections, e.g. a firewall or proxy. 

我的时ip搞错了,把公网ip当作内网ip使用了。真实很low的bug.
这里写图片描述

这里写图片描述

其他错误原因

大多数的原因可能是因为durid和mysql 的原因导致的,解决方案如下:


来自网友的博客拷贝:
由mysql5数据库的配置引起的。mysql5将其连接的等待时间(wait_timeout)缺省为8小时。在其客户程序中可以这样来查看其值:

mysql﹥ 

mysql﹥ show global variables like 'wait_timeout'; 

+---------------+---------+ 

| Variable_name | Value | 

+---------------+---------+ 

| wait_timeout | 28800 | 

+---------------+---------+ 
28800 seconds,也就是8小时。 

如果在wait_timeout秒期间内,数据库连接(java.sql.Connection)一直处于等待状态,mysql5就将该连接关闭。这时,你的Java应用的连接池仍然合法地持有该连接的引用。当用该连接来进行数据库操作时,就碰到上述错误。这解释了为什么我的程序第二天不能登录 的问题。 

本人觉得最简单的办法,就是对症下药:既然问题是由mysql5的全局变量wait_timeout的缺省值太小引起的,我们将其改大就好了。 

查看mysql5的手册,发现对wait_timeout的最大值分别是24天/365天(windows/linux)。以windows为 例,假设我们要将其设为21天,我们只要修改mysql5的配置文件“my.ini”(mysql5 installation dir),增加一行:wait_timeout=1814400

需要重新启动mysql5。 

linux系统配置文件:/etc/my.cnf 

测试显示问题解决了。
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值