21

I've installed a stock mysql 5.5 installation, and while I can connect to the mysql service via the mysql command, and the service seems to be running, I cannot connect to it through spring+tomcat or from an external jdbc connector.

I'm using the following URL:

jdbc:mysql://myserver.com:myport/mydb

with proper username/password, but I receive the following message:

server.com: 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.

and tomcat throws:

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.
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)

Which seems to be the same issue as if I try to connect externally.

Stefan Kendall
  • 1,069
  • 3
  • 17
  • 33
  • I faced this problem when trying to connect through java to my mysql database which runs on another server in my LAN. when executing the same java program on the server which runs mysql it was connecting without problems. from the external machine I could connect to the mysql database using SQLYog for example (although I first had to change the my.cnf file to bind to 0.0.0.0 instead of to 127.0.0.1). Boden's answer and the comment about changing the JDBC connector pointed me in the right direction. I changed the JDBC connector to the latest version and suddenly it worked! – user2380870 May 14 '13 at 09:01

9 Answers9

22

This can happen for a variety of reasons. I just saw it myself a few weeks ago but I can't remember what the fix was for me.

1) Verify the address mysql is bound to, it's probably 127.0.0.1 (only) which I believe is the default (at least on standard Ubuntu server). You'll have to comment out the bind-address parameter in my.cnf to bind to all available addresses (you can't choose multiple, it's one or all).

2) If it is bound to 127.0.0.1 and you can't connect using "localhost", make sure it's not resolving to the IPv6 localhost address instead of IPv4. (or just use the IP address)

3) Double and triple-check the port that mysql is listening on.

4) Make sure you're using the right JDBC connector for your JDK.

5) Make sure you're not doing something really silly like starting mysql with --skip-networking.

I think my first suggestion has the most promise...in fact I think that's where I saw it recently...I was trying to connect to mysql remotely (also on Ubuntu 8.04).

Boden
  • 4,948
  • 12
  • 48
  • 70
  • 1
    Just to add my 2cent, the 4th worked for me. Thanks! – Janis Peisenieks May 02 '12 at 15:15
  • 2
    regarding "You'll have to comment out the bind-address parameter in my.cnf to bind to all available addresses", the default is now to listen only on localhost, so you may need to use line "bind-address = 0.0.0.0" within [mysqld]. – Palo May 09 '15 at 10:46
15

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 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:

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:

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.

Soheil
  • 251
  • 2
  • 3
  • I was having this problem too! But i had initially changed mysql to listen to port 8888 instead of 3306. SO, @sohail's answer helped, i just added port 8888 to my uri and it worked! Thanks a bunch –  Jul 02 '13 at 06:18
  • This solved the problem for me! With our Vagrant Homestead setup it for some reason was binding to the address given to the VM (10.0.2.15 in my case) instead of localhost. – Lander Jan 02 '15 at 21:38
1

There is also this huge bug in version 5.1.9 of mysql-jdbc driver :

http://bugs.mysql.com/bug.php?id=47494

1

This can also be caused by wrong proxy settings. I had this problem trying to connect via jdbc to a MySQL instance running in a Parallels virtual appliance on my Mac. The jdbc connection uses the system-level network settings and since I was behind a SOCKS proxy I had to set the MySql host as a non-proxy host (e.g., on a Mac you can configure that in Settings->Network->Advanced->Proxies, and finally add the hostname or IP address in the "Bypass proxy settings for these Hosts and Domains").

1

MySQL Connector/J supports only TCP/IP Java does not support Unix domain sockets connectivity

If the MYSQL is started with skip-networking flag or if the MySQL is running behind the firewall, then TCP/IP option is disabled. So that Java cannot communicate with MySQL.

petrus
  • 5,287
  • 25
  • 42
1

If you are running a Linux installation, you probably have lokkit blocking incoming communications except via SSH.

Log in as root, and run the command lokkit from the prompt, disable firewall and SElinux and see if you have the same problem.

Also check your permissions have been set correctly, so everything can write to the correct locations.

Stephen Thompson
  • 1,482
  • 9
  • 10
  • I'm running Ubuntu 8.04. I probably should have mentioned that. Do you have specific instructions for that distro? I'll google in a moment, but I thought I'd ask first. – Stefan Kendall Dec 01 '09 at 22:19
  • sudo ufw disable will disable the Ubuntu Firewall – Stephen Thompson Dec 01 '09 at 22:35
  • I installed ufw and tried to enable the port, but no dice. – Stefan Kendall Dec 01 '09 at 22:35
  • ufw and iptables were already uninstalled. I thought ufw was a management tool that had to be installed separately. Sure enough, "iptables" yields no such command, and no iptables service seems to be running. – Stefan Kendall Dec 01 '09 at 22:36
  • Ok finally thing that I've often found is you don't enable mysql to listen on the eth0 interface, and often its only on the localhost interface. Try the following. Change jdbc:mysql://myserver.com:myport/mydb to jdbc:mysql://localhost:myport/mydb if that works, you need to do the following http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html – Stephen Thompson Dec 02 '09 at 02:22
  • Permissions wound up being the final problem. You should probably add that to your answer, as I can't edit on sf yet. – Stefan Kendall Dec 02 '09 at 16:32
0

i had a very similar problem for almost a day, and it drove me crazy! but i managed to found the solution, and it was very, very simple, you just need to /etc/init.d/tomcat6 to change TOMCAT6_SECURITY=yes to TOMCAT6_SECURITY=no. it's not my solution, i found it here, as you can see, i am running ubuntu, hope this works.

0

I had the same problem. Changed the "bind-address" property in /etc/mysql/my.cnf file to 0.0.0.0, and it works. Corresponding line in my.cnf looks like this:

bind-address = 0.0.0.0

Before it was set to the outside ip address of the server, so it looked something like:

bind-address = 196.152.4.145

I think when it's set to the outside ip address and not the localhost loop, mysql server is just connected to the network card and does not listen to the connections from the local loop.

0

try your local address to bind address in my.cnf file

Connection con = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://x62.xx8.x4x.x5:3306/mydb", "root", "root");
        try {
            System.out.println(con.getMetaData());
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (ClassNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }