Unable to Connect to MySQL Through JDBC Connector via Tomcat

jdbcMySQLtomcat

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.

Best Answer

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