Saturday, January 29, 2011

Unable to connect to mysql through JDBC connector through Tomcat or externally.

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.

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

    iftrue : 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.
    Stephen Thompson : sudo ufw disable will disable the Ubuntu Firewall
    iftrue : I installed ufw and tried to enable the port, but no dice.
    iftrue : 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.
    Stephen Thompson : 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
    iftrue : Permissions wound up being the final problem. You should probably add that to your answer, as I can't edit on sf yet.
  • 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).

    From Boden

  • hi guys, 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.

  • double check the port, some ninja changed the one on our server

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

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

    From Mathieu
  • 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 comments:

Post a Comment