Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a java application that runs on my work machine, but wont connect on the production server.

Heres the connection code:

    String db = "jdbc:mysql://localhost:3306/"+database+
    connect = DriverManager.getConnection(db, username, password);

Where the username, dbname and password are set correctly. When ran, I get:

com.mysql.jdbc.exceptions.jdbc4.CommunicationException: Communications link failure

I think it has to do with my iptables setup. When I telnet localhost 3306, My connection is refused; however, when I telnet the sever from another machine on port 3306 I can connect (the server accepts remote connections on the LAN). Is this normal behaviour?

I would have thought that localhost connections would work by default, but apparently not. The server is running debian btw. Any ideas whats going wrong here and how I can fix it?



After temporarily resetting the iptables setup I'm starting to doubt that is where the problem is and it may be to do with the mysql config.

The bind address in my.cnf is set to the server IP - not I can telnet from the local machine using the IP, but still can't connect with JDBC to it. From what I can tell mysql can only have one bind address. Perhaps theres something I need to tweak in the mysql setup...

share|improve this question

Your MySQL may be configured to specifically listen on only one IP address. Also note that localhost has special meaning in some if not most MySQL clients: it means to use a UNIX socket on the box. The address will use a real TCP connection, so try that.

If you still suspect iptables: iptables -L will allow you to see the current ruleset.

Edit: if your MySQL is configured to listen to a specific address, you can most likely get it to listen on all addresses, by making it listen to

Edit2: or simply removing the parameter from the config.

share|improve this answer
Thanks for you reply. I had tried connecting to, but no success. The reason I suspected iptables is because of the telnet thing. – DrDipshit Aug 26 '10 at 14:44

If it works on another server, then that server is the one hosting the mysql database. Your connect string needs to refer to it instead of localhost.

The command 'netstat -lnt | grep 3306' will tell you if mysql is running and listening on the port. If it is only listening on '', you will need to run your Java application on the same server, or tunnel the connection. If it is only listening on an address other than, try substituting localhost with that address.

Edit: From what I see your mysql configuration should be good. Check the port to see if it has been moved to a non-standard port. There are two locatiosn where port is defined, so check both. To see if it is listening run the netstat command above. If it isn't listening you won't be able to connect. Debian by default should listen on TCP address localhost:3306.

It is typicall to allow remote connections. To enable it, change the listen address to in your mysql configuration and restart.

I use DBVisualizer to browse databases. It is a Java application which uses JDBC to access the databases. It provides reasonably good output on connection failure, so you may want to try it. Use a basic connection string without options first. Then add your date option to see if that is the problem.

MySQL login security can include the originating hosts in addition to user and password, so logins may fail for that reason. Passwords can vary depending on origin.

share|improve this answer
What I meant is that there are other programs that access mysql on the server remotely. This program sits on the server and needs to access mysql via the loopback. I had tried substitution localhost with the server IP, but that did not work. Will update my question. – DrDipshit Aug 26 '10 at 15:16

Most Linux distributions by default when install MySQL default MySQL to not use a network connection, but by socket.

Generally there is a file call /etc/my.cnf. In there look for a line that has the following and comment it out: skip-networking

Once you comment it out, restart MySQL and you should be able to connect with no problems.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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