我的应用程序在 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
可能是什么问题呢?