很简单的东西,在这里——我只是对mysql不够好,无法理解它对我的要求。
我有一个简短的 java 测试用例,它在我的开发系统上的 mysql 上打开一个连接,但是当我尝试将它放到我的服务器上时,它失败了。
任何有助于追踪这一点的帮助将不胜感激。
谢谢!
测试代码
import java.util.*;
import java.sql.*;
public class mysqltest {
static public void getDBConnection() {
System.out.println ("Start of getDBConnection.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "magnets_development";
String driver = "com.mysql.jdbc.Driver";
String userName = "*****"; // blanked for publication
String password = "*****";
try {
Class.forName (driver).newInstance();
System.out.println ("driver.newInstance gotten.");
conn = DriverManager.getConnection (url+dbName, userName, password);
System.out.println ("Connection gotten: " + conn + ".");
Statement sql = conn.createStatement ();
ResultSet results = sql.executeQuery ("use " + dbName + ";");
}
catch (Exception ex) {
System.out.println ("*** Got exception.");
ex.printStackTrace();
}
}
public static void main(String args[]) {
System.out.println ("Main started.");
mysqltest.getDBConnection();
}
}
开发系统输出 (预期/正确响应)
olie$ java mysqltest
Main started.
Start of getDBConnection.
Properties set.
driver.newInstance gotten.
Connection gotten: com.mysql.jdbc.Connection@c980c9.
olie$
服务器输出 (我正在尝试追踪的错误)(删除了一些空白行。)
mini$ java mysqltest
Main started.
Start of getDBConnection.
Properties set.
driver.newInstance gotten.
*** Got exception.
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.ConnectException
MESSAGE: Connection refused
STACKTRACE:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
at java.net.Socket.connect(Socket.java:520)
at java.net.Socket.connect(Socket.java:470)
at java.net.Socket.<init>(Socket.java:367)
at java.net.Socket.<init>(Socket.java:209)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at mysqltest.getDBConnection(mysqltest.java:34)
at mysqltest.main(mysqltest.java:49)
** END NESTED EXCEPTION **
Last packet sent to the server was 3 ms ago.
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2847)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at mysqltest.getDBConnection(mysqltest.java:34)
at mysqltest.main(mysqltest.java:49)
mini$