Mysql Communications link Failure Problem solving

Source: Internet
Author: User

Problem phenomenon

 com. Mysql. Jdbc. Exceptions. Jdbc4. Communicationsexception: communicationsthe last packet sent Successfully to the server was 0 milliseconds ago. the driver have not received any packets from the Server        

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

While I is seeking the Internet to find the solution for this error, I figured out that there is many solutions that wor Ked for at least one person, but others say that it doesn ' t work for them! Why there is many approaches to this error? It seems this error can occur generally when there are 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 your to try all the solutions one by one and don ' t give up!

Here's the solutions that I found on the internet and for each of the them, there was at least on person who he problem has been solved with that solution.

Point:for the solutions that's 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 is 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 tha T 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 is some thing like this:

[Java]View Plaincopy
    1. DbName = "My_database";
    2. Dbusername = "root";
    3. Dbpassword = "";
    4. String connectionString = "jdbc:mysql://localhost/" + DbName + "? user=" + Dbusername + "&password=" + DBPA  ssWOrd + "&useunicode=true&characterencoding=utf-8";

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

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

[Java]View Plaincopy
    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 would not be 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 have 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 is some people how solved their problem with following these steps.

But what solved my problem? My problem was, 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-error. What I did is 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-to-solve this problem. I suggest think about your own situation and choose above solutions. If you take this error on the beginning of the program and you is not able to connect to the database at all, you might h Ave problem in your connection string. If you take this error after several successful interaction to the database, the problem might is with number of Conne Ctions and think about changing ' wait_timeout ' and other MySQL settings or rewrite your code how that reduce Numbe R of Connections.

Mysql Communications link Failure Problem solving

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.