0

私のアプリケーションはLinuxMandrivaで実行されており、MySQLデータベースに接続できません。

my.cnfの一部:

bind-address="127.0.0.1"
# skip- networking

wait_timeoutは次のように設定しました:

SET GLOBAL wait_timeout = 28800;

データベースへの接続を取得しようとしています:

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();
        }
    }
}

このクラスをjarにパックして、サーバーに送信しました。実行しようとすると、次のようになります。

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

しかし、ローカルホストからこのコードを実行すると、すべて問題ありません。

Connected to the database
Disconnected from database

何が問題なのですか?

4

1 に答える 1

2

telnetその Linux サーバーで 3306 をポートできますか? そのポートで何かがリッスンしているかどうかがわかります。

Windows サーバーからコードを実行する場合は、次の行に注意してください。

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

Linux マシンではなく、Windows マシンのサービスに接続していることを意味します。

于 2012-10-18T11:29:22.667 に答える