Java - Solving A "Communications Link Failure" With JDBC and MySQL - Stack Overflow
Java - Solving A "Communications Link Failure" With JDBC and MySQL - Stack Overflow
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration
required.
log in
tour
help
careers 2.0
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at Connect.main(Connect.java:16)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
I've set the classpath, made sure my.cnf had the skip network option commented out.
java version is 1.2.0_26 (64 bit) mysql 5.5.14 mysql connector 5.1.17
I made sure that the user had access to my database.
java
mysql
jdbc
Note the CausedBy at the bottom. The SQL server never accepted the connection. What happens if do (from
a command line) telnet localhost 3306 ? Is the mySQL server running? Jim Garrison Jul 28 '11 at
20:59
Check this post. Might help: stackoverflow.com/questions/15949/ maclema Jul 28 '11 at 21:07
Problem solved, added a bind-address entry to my.cnf. Anthony Jul 28 '11 at 21:28
What address did you put for your bind-address? prolink007 Oct 11 '11 at 21:45
@Anthony You should put your comment as an answer and accept it ... Fildor Nov 1 '11 at 17:24
17 Answers
I have had the same problem in two of my programs. My error was 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
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/mysql/my.cnf
Windows: D:\Program Files\mysql\bin\my.ini
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.
edited May 7 at 16:39
youssefhassan
686 1 6 11
Gud soheil. Its work for me when i connect in network. Thanks RED.Skull Jan 29 '13 at 7:06
I added wait_timeout only, referred to dev.mysql.com/doc/refman/5.1/en/ Siddharth Apr 4 '13 at 22:06
Where are the variables TOMCAT6_SECURITY and validationQuery ? Thanks! Charles Wood Mar 27
at 17:03
add a comment
In my case it was an idle timeout, that caused the connection to be dropped on the server. The connection
was kept open, but not used for a long period of time. Then a client restart works, while I believe a
reconnect will work as well.
A not bad solution is to have a daemon/service to ping the connection from time to time.
edited Sep 30 '12 at 14:46
add a comment
It happens (in my case) when there is not enough memory for MySQL. A restart fixes it, but if that's the
case consider a nachine with more memory, or limit the memory taken by jvms
answered May 27 '12 at 7:47
Bozho
273k 39 514 713
add a comment
Go to Windows services in the control panel and start the MySQL service. For me it worked. When I was
doing a Java EE project I got this error" Communication link failure". I restarted my system and then it
worked.
After that I again got the same error even after restarting my system. Then I tried to open the MySQL
command line console and login with root, even then it gave me an error.
Finally when I started the MySQL service from Windows services, it worked.
edited Mar 10 '13 at 9:39
Arjan Tijms
21.6k 6 49 83
add a comment
tinku
177 3 17
Setting the bind-address to the server's network IP instead of the localhost default, and setting
privileges on my user worked for me.
my.cnf:
bind-address = 192.168.123.456
MySql Console:
GRANT ALL PRIVILEGES ON dbname.* to username@'%' IDENTIFIED BY 'password';
answered Apr 3 '13 at 21:14
Kevin Lawrence
516 4 13
add a comment
If you are using hibernate, this error can be caused for keeping open a Session object more time than
wait_timeout
I've documented a case in here for those who are interested.
answered Sep 3 '13 at 16:01
Raul Luna
170 2 9
add a comment
Ok. This worked after changing MySQL port and connecting to that new port. Air Jul 1 at 0:26
add a comment
It is majorly because of weak connection between mysql client and remote mysql server.
In my case it is because of flaky VPN connection.
answered Oct 4 '13 at 18:15
Ankit Singhal
151 3
add a comment
As the detailed answer above says, this error can be caused by many things.
I had this problem too. My setup was Mac OSX 10.8, using a Vagrant managed VirtualBox VM of Ubuntu
12.04, with MySQL 5.5.34.
I had correctly setup port forwarding in the Vagrant config file. I could telnet to the MySQL instance both
from my Mac and from within the VM. So I knew the MySQL daemon was running and reachable. But
when I tried to connect over JDBC, I got the "Communications link failure" error.
In my case, the problem was solved by editing the /etc/mysql/my.cnf file. Specifically, I commented out the
"#bind-address=127.0.0.1" line.
answered Feb 12 at 2:57
devdanke
464 3 11
add a comment
If you are using local emulator, you have to use IP address 10.0.2.2 instead of localhost to access to your
local MySQL server.
answered Mar 7 at 11:05
Carlos
6 1
add a comment
I've just faced the same problem. It happened because the MySQL Daemon was binded to the IP of the
machine, which makes required to make connection with an user that has permission to connect
@your_machine. In this case, the user should have permission to connect
USER_NAME@MACHINE_NAME_OR_IP
I wanted remote access to my machine so I changes in my.cnf to
bind-address = MY_IP_ADDRESS
To fix the issue I changed to
bind-address = 0.0.0.0
Which will allow a users from localhost AND even outside (in my case). Both below permissions will work if
you bind the MySQL to 0.0.0.0:
USER_NAME@MACHINE_NAME_OR_IP
USER_NAME@localhost
answered Mar 19 at 4:28
Renann
18 5
add a comment
In my case,
1. Change the remote machine mysql configuration at /etc/mysql/my.cnf : change bindaddress = 127.0.0.1 to #bind-address = 127.0.0.1
2. On the remote machine, change mysql user permissions with GRANT ALL PRIVILEGES ON *.*
TO 'user'@'%' IDENTIFIED BY 'password';
3. IMPORTANT: restart mysql on the remote machine: sudo /etc/init.d/mysql restart
answered May 13 at 19:39
cindyxiaoxiaoli
91 3
add a comment
= 192.168.0.103 #127.0.0.1
I was experiencing similar problem and the solution for my case was
For me the solution was to change in the conf file of mysql server the parameter bind-address="127.0.0.1"
or bind-address="x.x.x.x" to bind-address="0.0.0.0". Thanks.
answered yesterday
user1969545
11 1
add a comment
Not the answer you're looking for? Browse other questions tagged java mysql jdbc
or ask your own question.