MySQL第二天早上第一次连接超时报错,解决方法com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure Last packet sent to the server was 0 ms ago

最近碰到一个mysql5数据库的问题。就是一个标准的servlet/tomcat网络应用,后台使用mysql数据库。问题是待机一晚上后,第二天早上第一次登录总是失败。察看日志发现如下错误:

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

Last packet sent to the server was 0 ms ago.”

经过一番调研,发现很多人都碰到过类似问题,但网上令人满意的回答并不多。mysql网站上的提问也很多,但并没有正确答案;百度知道上倒是有一个近似正确的回答。现将本人的解决办法总结一下:

上述问题是由mysql5数据库的配置引起的。mysql5将其连接的等待时间(wait_timeout)缺省为8小时。在其客户程序中可以这样来查看其值:

mysql﹥

mysql﹥ show global variables like 'wait_timeout';

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

| Variable_name | Value |

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

| wait_timeout | 28800 |

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

1 row in set (0.00 sec)

28800 seconds,也就是8小时。

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

你可能会想到在tomcat的数据源配置中有没有办法解决?的确,在jdbc连接url的配置中,你可以附上“autoReconnect=true”,但这仅对mysql5以前的版本起作用。增加“validation query”似乎也无济于事。

本人觉得最简单的办法,就是对症下药:既然问题是由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

测试显示问题解决了。

也可以直接设置

mysql修改wait_timeout

  mysql  mysql> show global variables like 'wait_timeout';

  其默认值为8小时

  mysql的一个connection空闲时间超过8小时,mysql会自动断开该连接。

  1.修改配置

  vi /etc/my.cnf

  [mysqld]  wait_timeout=10

  # /etc/init.d/mysql restart

  2.直接用sql命令行修改  mysql> set global wait_timeout=604800;

除了wait_timeout,还有一个'interactive_timeout'

同样可以执行SHOW GLOBAL VARIABLES LIKE  'interactive_timeout'来查询
执行set global interactive_timeout=604800;来设置

转载于:https://blog.51cto.com/8644390/1388991

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:相关推荐

  1. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 数据库报错

    -- 查询mysql 数据库链接空闲时间持有最大空闲时间,单位为秒 SHOW VARIABLES WHERE VAriable_name = 'interactive_timeout'; -- 会出现 ...

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

    昨天数据库做了一些改动,早上再打开的项目的时候就出现了这样的问题 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communicat ...

  3. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException

    新建的网站出现如下错误: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully r ...

  4. Mysql学习总结(62)——MySQL连接com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link问题

    数据库出现如下连接问题 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The ...

  5. 问题记录——com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure...

    最近在搞一个Spring boot + Mybatis + Mysql的项目,用Mybatis访问数据库时,报了如下的错误,先在网上搜索了,试了各种办法都不行, 奇葩的是,连接另外1个数据库又没问题. ...

  6. mysql第二天无法连接_MySQL第二天早上第一次连接超时报错,解决方法com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:...

    http://zeusami.iteye.com/blog/1112827 MySQL第二天早上第一次连接超时报错,解决方法com.mysql.jdbc.exceptions.jdbc4.Commun ...

  7. Cause com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure 问题解决 报错

    Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 问题解决 Cau ...

  8. 解决com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link

    一.可能导致这样的原因 1.配置文件中的url不正确(排除) <url>jdbc:mysql://localhost:3306/env</url> 2.mysql的连接等待时间 ...

  9. mysql连接com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link

    jdbc驱动:mysql-connector-java-5.1.39-bin.jar 这个有问题, 换成:mysql-connector-java-5.1.34.jar 就可以了 转载于:https: ...

  10. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'rtjhyt' in 'field list'

    今天向数据库中插入语句遇到了这样的异常: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'rtjh ...

最新文章

  1. Oracle 10g 中通过DBLink访问MySQL数据库
  2. SSO 单点登录会话管理
  3. ASVL_PAF_RGB24_B8G8R8 格式转换
  4. springboot:banner.txt
  5. Bash Cookbook 学习笔记 【高级】
  6. python语法实例_python基本语法练习实例
  7. python安装pyserial模块_Python使用模块Pyserial模块报
  8. 015. 深入JVM学习—Java引用类型
  9. python中function函数的用法_Python中Function(函数)和methon(方法)
  10. zabbix 监控项-计算公式监控磁盘使用率
  11. android UI进阶之布局的优化
  12. SAP EWM 根据物料号获得包装规格
  13. 实战Nginx(4)-压缩模块与http首部响应报文模块
  14. Struts1与Struts2的区别和对比(深度好文)
  15. 重新安装OFFICE2010失败
  16. as3.0点击获取TLF文本的实例名
  17. 2021年程序人生的随想总结
  18. 网站制作教程:新手如何用PageAdmin建站系统做网站
  19. linux docker升级,Docker 升级到最新版本
  20. JSX介绍-基本使用

热门文章

  1. eff java_eff java 39条
  2. Layer 提示框tips使用(批量提示)
  3. ELK 把date替换为logstash的@timestamp
  4. PHPExcel基本使用
  5. 软件基本功:做自说明的测试文档,
  6. 使用Rufus制作LINUX启动安装U盘(成功)
  7. 编译出错 recompile with -fPIC
  8. 编译OpenJDK12:freetypeScaler.obj error LINK2019 无法解析的外部符号
  9. VS Newtonsoft的引用问题
  10. 计算机员工工资管理系统源代码,C员工工资管理系统源代码.doc