0

我的应用程序在 Linux Mandriva 上运行,无法连接到 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/";

意味着您正在连接到 Windows 机器上的服务,而不是 Linux 机器。

于 2012-10-18T11:29:22.667 回答