Mysql Communications link failure 问题的解决

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:

问题现象

 

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.

 

 

 

I spend some days to solve this problem. I have tested many approaches that have been mentioned in different web sites, but non of them worked. Finally I changed my code and found out what was the problem. I'll try to tell you about different approaches and sum them up here.

While I was seeking the internet to find the solution for this error, I figured out that there are many solutions that worked for at least one person, but others say that it doesn't work for them! why there are many approaches to this error? It seems this error can occur generally when there is a problem in connecting to the server. Maybe the problem is because of the wrong query string or too many connections to the database.

So I suggest you to try all the solutions one by one and don't give up!

Here are the solutions that I found on the internet and for each of them, there is at least on person who his problem has been solved with that solution.

point: For the solutions that you need to change the MySQL settings, you can refer to the following not:

  • Linux: /etc/my.cnf

  • Windows: D:\Program Files\mysql\bin\my.ini

Here are the solutions:

  • changing "bind-address" attribute

Uncomment "bind-address" attribute or change it to one of the following Ips:

bind-address="127.0.0.1"

or

bind-address="0.0.0.0"

  • commenting out "skip-networking"

If there is a "skip-networking" line in your MySQL config file, make it comment by adding "#" sign at the beginning of that line.

  • change "wait_timeout" and "interactive_timeout"

Add these lines to the MySQL config file:

wait_timeout = number

interactive_timeout = number

connect_timeout = number

  • check Operating System proxy settings

Make sure the Fire wall, or Anti virus soft wares don't block MySQL service.

  • change connection string

Check your query string. your connection string should be some thing like this:

[java]  view plain copy
 
  1. dbName = "my_database";  
  2. dbUserName = "root";  
  3. dbPassword = "";  
  4. String connectionString = "jdbc:mysql://localhost/" + dbName + "?user=" + dbUserName + "&password=" + dbPassword + "&useUnicode=true&characterEncoding=UTF-8";  

 

Make sure you don't have spaces in your string. All the connection string should be continues without any space characters.

Try to replace "localhost" with your port, like 127.0.0.1. Also try to add port number to your connection string, like:

[java]  view plain copy
 
  1. String connectionString = "jdbc:mysql://localhost:3306/my_database?user=root&password=Pass&useUnicode=true&characterEncoding=UTF-8";  

 

Usually default port for MySQL is 3306.

Don't forget to change username and password to the username and password of your MySQL server.

  • update your JDK driver library file
  • test different JDK and JREs (like JDK 6 and 7)
  • don't change max_allowed_packet

"max_allowed_packet" is a variable in MySQL config file that indicates the maximum packet size, not the maximum number of packets. So it will not help to solve this error.

  • change tomcat security

change TOMCAT6_SECURITY=yes to TOMCAT6_SECURITY=no

  • use validationQuery property

use validationQuery="select now()" to make sure each query has responses

  • AutoReconnect

Add this code to your connection string:

&autoReconnect=true&failOverReadOnly=false&maxReconnects=10

Although non of these solutions worked for me, I suggest you to try them. Because there are some people how solved their problem with following these steps.

But what solved my problem? My problem was that I had many SELECTs on database. Each time I created connection and then closed it. Although I closed the connection every time, but the system faced with many connections and gave me that error. What I did was that I defined my connection variable as a public (or private) variable for whole class and initialized it in the constructor. Then every time I just used that connection. It solved my problem and also increased my speed dramatically.

Conclusion

There is no simple and unique way to solve this problem. I suggest you to think about your own situation and choose above solutions. If you take this error at the beginning of the program and you are not able to connect to the database at all, you might have problem in your connection string. But If you take this error after several successful interaction to the database, the problem might be with number of connections and you may think about changing "wait_timeout" and other MySQL settings or rewrite your code how that reduce number of connections.

 

 

1、修改my.cnf: 
 
[mysqld]  
wait_timeout=31536000  
interactive_timeout=31536000  
 
将过期时间修改为1年。 
 
2、在连接URL上添加参数:&autoReconnect=true&failOverReadOnly=false 
 
我使用的是第一种方式,如果不会改修改本篇日志说明相应的配置已经起作用。
参考地址:http://www.2cto.com/database/201305/211381.html

本文转自 netcorner 博客园博客,原文链接: http://www.cnblogs.com/netcorner/p/4346181.html    ,如需转载请自行联系原作者

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
6月前
|
SQL druid 关系型数据库
MySQL连接超时时间wait_timeout导致间歇性报错:communication link failure
MySQL连接超时时间wait_timeout导致间歇性报错:communication link failure
229 1
|
8月前
|
关系型数据库 MySQL 数据库连接
[已解决]踩过的坑之mysql连接报“Communications link failure”错误
[已解决]踩过的坑之mysql连接报“Communications link failure”错误
|
9月前
|
关系型数据库 MySQL Java
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
这个时候我们就要检查一下我们链接数据库的**url、root、password、**驱动是否正确,经过我的排查原来是我的url链接名不是localhost,真尴尬,改成localhost就成功了。
152 0
|
10月前
|
关系型数据库 MySQL Java
Mysql 报错:Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
Mysql 报错:Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
234 0
|
关系型数据库 MySQL Java
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
216 0
|
安全 关系型数据库 MySQL
java程序启动连接mysql时报错com.mysql.jdbc.exceptions.jdbc4.CommunicationsExcepti:Communications link failur
java程序启动连接mysql时报错com.mysql.jdbc.exceptions.jdbc4.CommunicationsExcepti:Communications link failur
424 0
|
SQL 安全 数据库连接
解决Exception in thread “main“ com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications
解决Exception in thread “main“ com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications
解决Exception in thread “main“ com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications
|
SQL Oracle 关系型数据库
MySQL下的DB Link
在实际工作中,我们可能会遇到需要操作其他数据库实例的部分表,但又不想系统连接多库。此时我们就需要用到数据表映射。如同Oracle中的DBlink一般,使用过Oracle DBlink数据库链接的人都知道可以跨实例来进行数据查询,同样的,Mysql自带的FEDERATED引擎完美的帮我们解决了该问题。本篇文章介绍FEDERATED引擎的开启和使用。
177 0
|
数据库连接 数据库 Java
解决:com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure(真实有效)
数据库连接失败 在数据库连接失败,经常会有蛮多一系列的问题导致的原因,这个时候一定要多去尝试一下各种方法,并且做好自己的梳理! 一、例如我在SpringBoot项目中使用了阿里的数据库连接池Driud。
43212 0
|
Web App开发 Java 关系型数据库
【MySQL】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.
11396 0