我的应用程序在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可能的问题是什么?
发布于 2012-10-18 19:29:23
你能通过telnet连接到Linux服务器上的3306端口吗?它将告诉您该端口上是否有侦听的东西。
请注意,如果您从Windows服务器运行代码,则以下行:
jdbc:mysql://localhost:3306/";意味着您正在连接到Windows计算机上的服务,而不是Linux计算机上的服务。
https://stackoverflow.com/questions/12953494
复制相似问题