user1134181
user1134181

Reputation:

Communications link failure due to underlying exception: Connection Time Out

My application runs on Linux Mandriva and can't get a connection to the MySQL database.

Part of my.cnf:

bind-address="127.0.0.1"
# skip- networking

I setted wait_timeout as follows:

SET GLOBAL wait_timeout = 28800;

I trying to get a connection to the database:

public class TestJdbc {
    public static void main(String[] args) {
        Connection conn = null;

        String url = "jdbc:mysql://localhost:3306/";        
        String driver = "com.mysql.jdbc.Driver";

        String dbName = "gibrid", userName = "java", 
            password = "java";      

        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url+dbName, userName, password);
            System.out.println("Connected to the database");
            conn.close();
            System.out.println("Disconnected from database");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I packed this class in jar and send to the server. When I try to execute it, I get the following:

    com.mysql.jdbc.CommunicationsException: 
Communications link failure due to underlying exception:

    ** BEGIN MESSAGE **

    java.net.ConnectException
    MESSAGE: Connection timed out

    STACKTRACE: java.net.ConnectException: Connection timed out
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:333)
      ...

But when I run this code from my localhost, everything is OK:

Connected to the database
Disconnected from database

What could be the problem?

Upvotes: 0

Views: 8009

Answers (1)

Brian Agnew
Brian Agnew

Reputation: 272297

Can you telnet to port 3306 on that Linux server ? That will tell you if something is listening on that port or not.

Note that if you run your code from your Windows server, the following line:

jdbc:mysql://localhost:3306/";

means you're connecting to a service on your Windows machine, not your Linux machine.

Upvotes: 2

Related Questions