我在網上找到的解決方案都不起作用。 我不停的按當我嘗試使用JDBC連接到MySQL時,出現「通信鏈接失敗」錯誤
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信鏈路故障
我想MySQL數據庫從另一臺服務器連接。 我可以向你保證,當我嘗試使用PgAdmin
軟件連接數據庫時,url,用戶和密碼信息是正確的,因爲它工作正常。但是,當我嘗試使用Java代碼執行時(通過NetBean 8.1),它失敗了。 好了,下面是我的代碼:如下圖所示
import oracle.jdbc.pool.OracleDataSource;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Client9{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://MySQLUrl:3306/";
String dbName = "MySQL_dbName";
String driver = "com.mysql.jdbc.Driver";
String userName = "user";
String password = "password";
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();
}
}
}
錯誤消息:
run:
MySQL Connect Example.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:673)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1084)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2465)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at netiqcustomprov.Client9.main(Client9.java:35)
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3052)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:597)
... 16 more
BUILD SUCCESSFUL (total time: 1 minute 0 seconds)
解決方案到目前爲止,我曾嘗試: 我有進口ojdb14.jar
和mysql-connector-java-5.1.23-bin.jar
到庫中。 我已經嘗試了所有3種不同的方法getConnection()
getConnection(String url)
(我的字符串URL = 「的jdbc:mysql的:// MySQLUrl:3306/MySQL_dbName用戶=用戶&密碼=密碼」)getConnection(String url, Properties info)
getConnection(String url, String user, String password)
我也曾嘗試使用下面的命令來註冊驅動程序:
Driver myDriver = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(myDriver);
我發現的解決方案之一是評論my.cnf文件中的「skip-networking」規則。但是,我無法在服務器中找到這樣的文件。 (服務器基於Linux操作系統運行)
非常感謝幫助解決此問題。 在此先感謝
更新:查看我的回答。我再一次爲我愚蠢的錯誤道歉。
oracle事情在這裏沒有任何關係。我們可以看到在這個linux服務器上運行了'sudo lsof -i:3306',你是否也在命令行嘗試了一個'mysql -u user -p'(同樣在這臺服務器上) –
'從機器ping MySQLUrl'運行java代碼也可能有助於診斷 –
MySQL服務器是否正在運行?你能夠從任何MySQL客戶端連接嗎? – Gangaraju