`

mysql link fail

 
阅读更多
问题现象



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 plaincopy

dbName = "my_database"; 
dbUserName = "root"; 
dbPassword = ""; 
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 plaincopy

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
分享到:
评论

相关推荐

    解决mysql failed to open table mysql.event

    在MySQL数据库系统中,"failed to open table mysql.event" 是一个常见的错误,通常表示数据库无法正确访问或加载`mysql.event`表。`mysql.event`是MySQL服务器用来存储定时任务(也称为事件Scheduler)的地方,当这...

    C_link_mysql.zip_C MYSQL_c++ mysql_connect_mysql c#_mysql c++

    在本项目"C_link_mysql.zip"中,我们关注的是如何使用C和C++语言与MySQL数据库进行交互,主要涉及以下几个核心知识点: 1. **C语言连接MySQL**:在C语言环境中,我们可以使用MySQL提供的C API(应用程序编程接口)...

    C_link_mySql51.

    标题"C_link_mySql51."指的是使用C语言与MySQL数据库进行连接的一个项目或者教程。描述中提到的关键点是,这个过程需要C语言的编程环境以及MySQL的头文件和库文件。接下来,我们将深入探讨如何在C语言环境中配置和...

    mysql 异常com.mysql.jdbc.CommunicationsException

    本次异常的具体描述为:“Communications link failure due to underlying exception: **BEGINNESTED EXCEPTION** java.io.EOFException STACK TRACE: java.io.EOFException at com.mysql.jdbc.MysqlIO.readFully...

    mysql手册mysql手册mysql手册mysql手册mysql手册mysql手册mysql手册

    mysql手册mysql手册mysql手册mysql手册mysql手册mysql手册mysql手册mysql手册mysql手册mysql手册mysql手册mysql手册mysql手册mysql手册mysql手册mysql手册mysql手册mysql手册mysql手册mysql手册mysql手册mysql手册...

    SpringMvc+Eclipselink+JPA+ActiveMQ+MySQL在线聊天

    在本项目中,"SpringMvc+Eclipselink+JPA+ActiveMQ+MySQL在线聊天" 实现了一个基于Web的即时通讯系统,模仿了微信的聊天功能。让我们逐一解析这个项目所涉及的关键技术: 1. **Spring MVC**: - Spring MVC是...

    龙蜥anolis8系统安装mysql5.7

    wget https://your_download_link/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz tar -zxvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz ``` 解压后的目录应包含MySQL的所有文件和目录。 4. **配置MySQL**: ...

    新版 MySQL DBA 高级视频 基于MySQL 5.7 MySQL 8.0版本.rar

    ├─新版MySQL DBA 课件ppt │ 第一课数据库介绍篇.pdf │ 第七课MySQL数据库设计.pdf │ 第三十一课percona-toolkits 的实战及自动化.pdf │ 第三课MySQL授权认证.pdf │ 第九课MySQL字符集.pdf │ 第二十一课MySQL...

    安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql

    安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql安装Mysql...

    C#操作Mysql数据库的MySql.Data.dll下载

    首先,我们要理解什么是DLL(Dynamic Link Library)文件。DLL是Windows操作系统中的一个共享库,其中包含了可被多个程序同时使用的函数和资源。MySql.Data.dll就是这样一个库,它封装了与MySQL数据库通信所需的全部...

    C#连接MySQL需要的MySql.Data.dll,MySql.Web.dll

    在C#编程环境中,连接MySQL数据库通常依赖于特定的数据提供者,这就是MySql.Data.dll和MySql.Web.dll的角色。这两个动态链接库(DLL)文件是MySQL官方提供的.NET数据访问组件,使得C#开发者能够轻松地与MySQL服务器...

    mysql驱动jar 文件适用MySQL5.7

    MySQL驱动jar文件是Java应用程序与MySQL数据库之间通信的关键组件,主要功能是提供Java Database Connectivity (JDBC) API,使得Java开发者能够通过编写Java代码来执行SQL语句,从而操作MySQL数据库。标题提到的...

    Mysql5.6、Mysql5.7 JDBC驱动

    MySQL是世界上最受欢迎的关系型数据库管理系统之一,而JDBC(Java Database Connectivity)是Java语言与数据库交互的标准接口。本文将深入探讨Mysql5.6和Mysql5.7的JDBC驱动,以及如何使用`mysql-connector-java-...

    mysql驱动(适用于mysql5.7版本)

    MySQL驱动是Java应用程序连接到MySQL数据库的关键组件,它允许程序通过Java Database Connectivity (JDBC) API与MySQL服务器进行通信。本压缩包包含了两个适用于MySQL 5.7版本的驱动文件:`mysql-connector-java-...

    mysql 5.5版 头文件(mysql.h等)

    例如,`mysql_init()`用于初始化一个MySQL连接结构,`mysql_real_connect()`用于建立实际的数据库连接,`mysql_query()`用于执行SQL查询,而`mysql_store_result()`和`mysql_use_result()`则用于处理查询结果。...

    ado_link_mysql.rar_delphi 驱动安装

    在本压缩包“ado_link_mysql.rar_delphi 驱动安装”中,包含了一个使用Delphi 7.0开发的示例程序,该程序旨在演示如何通过ADO(ActiveX Data Objects)连接到MySQL数据库。ADO是Microsoft提供的一个数据访问接口,它...

    mysql安装教程(保姆级)-mysql-8.0.32-winx64

    mysql安装教程(保姆级)---mysql-8.0.32-winx64 mysql安装教程(保姆级)---mysql-8.0.32-winx64 mysql安装教程(保姆级)---mysql-8.0.32-winx64 mysql安装教程(保姆级)---mysql-8.0.32-winx64 mysql安装教程...

    mysql下载,mysql工具

    MySQL是一种广泛使用的开源关系型数据库管理系统(RDBMS),它基于结构化查询语言(SQL)。MySQL因其高效、可靠和易于管理的特点,在Web应用程序开发、数据分析、小型到大型企业系统等领域都得到了广泛应用。以下是...

    MYSQL

    4.12.1 在 Win32 上安装 MySQL 4.12.2 在 Win95 /Win98上启动 MySQL 4.12.3 在 NT 上启动 MySQL 4.12.4 在 Win32 上运行 MySQL 4.12.5 用 SSH 从 Win32 连接一个远程MySQL 4.12.6 MySQL-Win...

    MySQL ODBC 5.1 Driver(Mysql驱动免安装版)

    MySQL ODBC 5.1 Driver 是一个用于连接MySQL数据库的开放数据库连接(ODBC)驱动程序,它允许用户通过标准的ODBC接口访问MySQL数据库,适用于Windows操作系统。这个版本是免安装的,意味着用户可以直接解压使用,...

Global site tag (gtag.js) - Google Analytics