連接到MySQL服務器我得到的錯誤"Communications link failure"
在這行代碼:無法從Java
mySqlCon = DriverManager.getConnection("jdbc:mysql://**server ip address**:3306/db-name", "mu-user-name", "my-password");
我this post檢查一切:
- 我在增加最大允許的數據包。 cnf in etc/mysql:max_allowed_packet = 5073741824 ------ [mysqldump] max_allowed_packet = 1G
- 綁定地址是:127.0.0.1
- 所有超時值等於一些尚未安裝在服務器上
- 的Tomcat(新服務器)
- 存在my.cnf中沒有跳躍網絡
- 我可以ping服務器
- 我連接到MySQL數據庫通過ssh
當我查詢字符串改成這樣:
mySqlCon = DriverManager.getConnection("jdbc:mysql://**server ip address**:22/127.0.0.1:3306/db-name", "mu-user-name", "my-password");
我得到的錯誤Packet for query is too large (4739923 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.
雖然我改變了my.cnf上的數據包大小,然後重新啓動了mysql服務。
有什麼建議嗎?
注:
我可以通過這個代碼的ssh連接,但這種方式似乎並不理性!我可以在main中連接一次,然後我應該將連接傳遞給所有類。
public my-class-constructor() {
try {
go();
} catch (Exception e) {
e.printStackTrace();
}
mySqlCon = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://" + rhost + ":" + lport + "/";
String db = "my-db-name";
String dbUser = "dbuser";
String dbPasswd = "pass";
try {
Class.forName(driver);
mySqlCon = DriverManager.getConnection(url + db, dbUser, dbPasswd);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void go() {
String user = "ssh-user";
String password = "ssh-pass";
String host = "ips-address";
int port = 22;
try {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
lport = 4321;
rhost = "localhost";
rport = 3306;
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection...");
session.connect();
int assinged_port = session.setPortForwardingL(lport, rhost, rport);
System.out.println("localhost:" + assinged_port + " -> " + rhost
+ ":" + rport);
} catch (Exception e) {
System.err.print(e);
e.printStackTrace();
}
}
你能ping通服務器嗎? –
看起來您的查詢太大,請嘗試一個較小的查詢,例如「select * from table limit 10;」 – nafas
如果MySQL數據庫是遠程的,您是否正確配置了權限? – John