我的应用程序在 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
可能是什么问题?
最佳答案
你可以 telnet
到那个 Linux 服务器上的端口 3306 吗?这会告诉您是否有东西正在监听该端口。
请注意,如果您从 Windows 服务器运行代码,则以下行:
jdbc:mysql://localhost:3306/";
表示您正在连接到 Windows 机器上的服务,而不是 Linux 机器上的服务。
关于java - 由于基础异常 : Connection Time Out 导致通信链接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12953494/