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.

link|improve this question

feedback

7 Answers

up vote 8 down vote accepted

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).

link|improve this answer
Just to add my 2cent, the 4th worked for me. Thanks! – Janis Peisenieks May 2 at 15:15
feedback

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.

link|improve this answer
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 1 '09 at 22:19
sudo ufw disable will disable the Ubuntu Firewall – Stephen Thompson Dec 1 '09 at 22:35
I installed ufw and tried to enable the port, but no dice. – Stefan Kendall Dec 1 '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 1 '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 dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html – Stephen Thompson Dec 2 '09 at 2:22
show 1 more comment
feedback

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

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

link|improve this answer
feedback

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.

link|improve this answer
feedback

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.

link|improve this answer
feedback

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").

link|improve this answer
feedback

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();
    }
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.