mysql link failITeye - 超凡娱乐

mysql link failITeye

2019-01-10 11:08:00 | 作者: 曦晨 | 标签: 修正,问题,现象 | 浏览: 2989

问题现象<br><br> <br><br>com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure<br><br>The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.<br> <br><br> <br><br> <br><br>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. Ill try to tell you about different approaches and sum them up here.<br><br>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 doesnt 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.<br><br>So I suggest you to try all the solutions one by one and dont give up!<br><br>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.<br><br>point: For the solutions that you need to change the MySQL settings, you can refer to the following not:<br><br>Linux: /etc/my.cnf<br><br>Windows: D:\Program Files\mysql\bin\my.ini<br><br>Here are the solutions:<br>changing "bind-address" attribute<br>Uncomment "bind-address" attribute or change it to one of the following Ips:<br><br>bind-address="127.0.0.1"<br><br>or<br><br>bind-address="0.0.0.0"<br><br>commenting out "skip-networking"<br>If there is a "skip-networking" line in your MySQL config file, make it comment by adding "#" sign at the beginning of that line.<br><br>change "wait_timeout" and "interactive_timeout"<br>Add these lines to the MySQL config file:<br><br>wait_timeout = number<br><br>interactive_timeout = number<br><br>connect_timeout = number<br><br>check Operating System proxy settings<br>Make sure the Fire wall, or Anti virus soft wares dont block MySQL service.<br><br>change connection string<br>Check your query string. your connection string should be some thing like this:<br><br>[java] view plaincopy<br> <br>dbName = "my_database";&nbsp; <br>dbUserName = "root";&nbsp; <br>dbPassword = "";&nbsp; <br>String connectionString = "jdbc:mysql://localhost/" + dbName + "?user=" + dbUserName + " password=" + dbPassword + " useUnicode=true characterEncoding=UTF-8";&nbsp; <br> <br><br>Make sure you dont have spaces in your string. All the connection string should be continues without any space characters.<br><br>Try to replace "localhost" with your port, like 127.0.0.1. Also try to add port number to your connection string, like:<br><br>[java] view plaincopy<br> <br>String connectionString = "jdbc:mysql://localhost:3306/my_database?user=root password=Pass useUnicode=true characterEncoding=UTF-8";&nbsp; <br> <br><br>Usually default port for MySQL is 3306.<br><br>Dont forget to change username and password to the username and password of your MySQL server.<br><br>update your JDK driver library file<br>test different JDK and JREs (like JDK 6 and 7)<br>dont change max_allowed_packet<br>"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.<br><br>change tomcat security<br>change TOMCAT6_SECURITY=yes to TOMCAT6_SECURITY=no<br><br>use validationQuery property<br>use validationQuery="select now()" to make sure each query has responses<br><br>AutoReconnect<br>Add this code to your connection string:<br><br> autoReconnect=true failOverReadOnly=false maxReconnects=10<br>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.<br><br>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.<br><br>Conclusion<br>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.<br><br> <br><br> <br><br>1、修正my.cnf: 【解决问题】<br> <br>[mysqld]&nbsp; <br>wait_timeout=31536000&nbsp; <br>interactive_timeout=31536000&nbsp; <br> <br>将过期时刻修正为1年。 <br> <br>2、在衔接URL上增加参数: autoReconnect=true failOverReadOnly=false <br> <br>我运用的是第一种方法,假如不会改修正本篇日志阐明相应的装备现已起作用。<br>参阅地址:http://www.2cto.com/database/201305/211381.html
版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表超凡娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章