P粉0684862202023-08-25 09:47:17
I also encountered the same problem in two programs. My error is this:
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.
It took me several days to solve this problem. I tested many methods mentioned on different websites but none of them worked. Finally I changed the code and figured out the problem. I'll try to introduce you to different methods and summarize them here.
When I searched the internet for solutions to this error, I found that there were many solutions that worked for at least one person, but others said it didn't work for them!
strong> Why are there many ways to fix this error? It seems usuallythis error occurs when there is a problem connecting to the server. Maybe the problem is because of wrong query string or too many connections to the database.So I suggest you try all the solutions one by one and don't give up!
Below are the solutions I found on the internet, for each solution at least one person's problem has been solved by that solution.
Tips: For solutions that require changing MySQL settings, please refer to the following files:
Linux: /etc/mysql/my.cnf
or /etc/my.cnf
(depending on the Linux distribution and MySQL package used) p>
Windows: C:\**ProgramData**\MySQL\MySQL Server 5.6\my.ini
(note it is ProgramData, not Program Files)
Changebind-address
Properties:
Uncomment the bind-address
property or change it to one of the following IPs:
bind-address="127.0.0.1"
or
bind-address="0.0.0.0"
Comment out "skip-networking"
If there is a skip-networking
line in the MySQL configuration file, please comment it out by adding # symbols at the beginning of the line.
Change "wait_timeout" and "interactive_timeout"
Add these lines to the MySQL configuration file:
[wait_timeout][1] = *number* interactive_timeout = *number* connect_timeout = *number*
Ensure Java does not translate "localhost" as [:::1] instead of [127.0.0.1]
Because MySQL can recognize 127.0.0.1
(IPv4
), but not :::1
(IPv6
)< /p>
This can be avoided by using one of two methods:
Use 127.0.0.1
instead of localhost
in the connection string to avoid localhost
being converted to ::: 1
Run java using option -Djava.net.preferIPv4Stack=true
to force java to use IPv4
instead of IPv6
. On Linux this can also be achieved by running (or putting this into /etc/profile
:
export _JAVA_OPTIONS="-Djava.net.preferIPv4Stack=true"
Check operating system proxy settings, firewall and anti-virus programs
Make sure the MySQL service is not blocked by firewall or anti-virus software.
Temporarily stop iptables on Linux. If iptables are misconfigured, they may allow tcp packets to be sent to the mysql port, but prevent tcp packets from returning to the same connection.
# Redhat enterprise and CentOS systemctl stop iptables.service # Other linux distros service iptables stop
Stop antivirus software on Windows.
Change connection string
Check your query string. Your connection string should look like this:
dbName = "my_database"; dbUserName = "root"; dbPassword = ""; String connectionString = "jdbc:mysql://localhost/" + dbName + "?user=" + dbUserName + "&password=" + dbPassword + "&useUnicode=true&characterEncoding=UTF-8";
Make sure there are no spaces in the string. All connection strings should be contiguous without any space characters.
Try replacing "localhost" with the loopback address 127.0.0.1. Also try adding the port number to the connection string, for example:
String connectionString = "jdbc:mysql://localhost:3306/my_database?user=root&password=Pass&useUnicode=true&characterEncoding=UTF-8";
Usually the default port of MySQL is 3306.
Don’t forget to change the username and password to those of your MySQL server.
"max_allowed_packet" is a variable in the MySQL configuration file that indicates the maximum packet size, not the maximum number of packets. So this doesn't help in solving this error.
Change TOMCAT6_SECURITY=yes to TOMCAT6_SECURITY=no
Use validationQuery="select now()" to ensure that each query has a response
Add this code to your connection string:
&autoReconnect=true&failOverReadOnly=false&maxReconnects=10
While none of these solutions worked for me, I recommend you give them a try. Because some people solved their problems by following these steps.
But what solved my problem?
My problem is that I have a lot of SELECTs in the database. Every time I create a connection and then close it. Although I close the connection every time, the system faces many connections and gives me this error. What I did was define the connection variable as a public (or private) variable for the entire class and initialize it in the constructor. Then every time I use that connection. It solved my problem and improved my speed tremendously.
#in conclusion# There is no simple and unique way to solve this problem. It is recommended that you consider the above solutions based on your own situation. If you get this error at the beginning of your program and can't connect to the database at all, there might be something wrong with your connection string. However, if you get this error after many successful interactions with the database, the problem may be with the number of connections and you may consider changing "wait_timeout" and other MySQL settings, or rewriting the code to reduce the number of connections.