【问题标题】:Communications link failure due to underlying exception: Connection Time Out由于基础异常导致通信链路故障:连接超时
【发布时间】:2012-10-18 11:23:54
【问题描述】:

我的应用程序在 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

可能是什么问题?

【问题讨论】:

  • 在linux服务器上,“netstat -an | grep 3306”的输出是什么

标签: java mysql linux networking jdbc


【解决方案1】:

你能telnet 到那个 Linux 服务器上的 3306 端口吗?这会告诉你是否有东西在那个端口上监听。

请注意,如果您从 Windows 服务器运行代码,请使用以下行:

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

表示您正在连接到 Windows 机器上的服务,而不是 Linux 机器上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-21
    • 2012-09-20
    • 2021-03-27
    • 2020-01-08
    • 2018-12-24
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多