我正在尝试连接到由strato托管的MySQL服务器。
我按照this page上的说明使用PuTTy连接到数据库。现在,如果我正在使用终端,连接到我的数据库是没有问题的:

但是,一旦我尝试使用NaviCat或MySQL Workbench连接到MySQL服务器,它就会给我这个错误:

。
我做错了什么?我如何使用NaviCat连接到数据库?我也想通过Java连接到这个数据库,但是我应该使用哪一行来连接,我应该在主机上填写什么?只是本地主机?那么我是不是应该像这样使用一个函数来连接:
public static void connectToSQL() {
try {
@SuppressWarnings("unused")
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/DB3262523", "U3262523", "....");
System.out.println("Connection success");
} catch (Exception e) {
//TODO: handle exception
System.err.println(e);
}
}发布于 2018-08-13 17:06:37
最后,我明白了,你想只使用SSH隧道连接mysql数据库服务,你不想打开防火墙,只想通过JumpBox访问数据库。我已经使用JSch liberary进行了SSH调优。
这是你的代码。
import java.sql.DriverManager;
import java.sql.SQLException;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.sql.Connection;
public class MySqlConnOverSSH {
public static void main(String[] args) throws SQLException {
int local_proxy_port=4567;
String database_host="rdbms.strato.de";
String ssh_host="ssh.strato.de";
int database_port=3306;
String user="ssh_username";
String privateKey = "In case you want to use private key to do SSH";
String password="sshpassword";
String dbuserName = "yourDBName";
String dbpassword = "YourDBPass";
String url = "jdbc:mysql://localhost:"+local_proxy_port+"/your-database-name";
String driverName="com.mysql.jdbc.Driver";
Connection conn = null;
Session session= null;
try{
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
//Only in case you need to access SSH using key.
//jsch.addIdentity(privateKey);
session=jsch.getSession(user, ssh_host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected");
int assinged_port=session.setPortForwardingL(local_proxy_port, database_host, database_port);
System.out.println("localhost:"+assinged_port+" -> "+database_host+":"+database_port);
System.out.println("Port Forwarded");
Class.forName(driverName).newInstance();
conn = DriverManager.getConnection (url, dbuserName, dbpassword);
System.out.println ("Connected!!!");
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn != null && !conn.isClosed()){
System.out.println("Hurry Done!!");
conn.close();
}
if(session !=null && session.isConnected()){
System.out.println("Hurry Done!! Closing SSH");
session.disconnect();
}
}
}
}确保在您的classpath中包含mysql-connector-java-xxx-bin.jar;。其中xxx是mysql jdbc jar的版本,例如5.0.8
如果此代码不起作用,请在运行此代码后发布您看到的进一步错误。我可以重新尝试回答。
发布于 2018-08-14 18:03:32
由于您可以使用终端连接到数据库,因此实际上可以检查数据库本身,以了解从服务器的角度来看,连接失败的原因。
尝试此查询:
SELECT * FROM performance_schema.host_cache每个连接错误都在不同的列中进行说明,因此这有助于查明连接被拒绝的根本原因。
参考手册:
https://dev.mysql.com/doc/refman/5.6/en/host-cache-table.html
https://stackoverflow.com/questions/48720217
复制相似问题