我尝试了各种方法来创建到我的远程MySQL数据库的SSH连接。
String user = "user";
String password = "pass";
String remoteHost = "host";
int localPort = 2022;
int remotePort = 3306;
Session session = null;
//Try to forward ports
try
{
//Create the JSCH object
JSch jsch = new JSch();
//Get the session
session = jsch.getSession(user, remoteHost);
//Set the password
session.setPassword(password);
//To be able to connect to any host (this is just for testing!)
session.setConfig("StrictHostKeyChecking", "no");
//Connect the session
session.connect();
//Set port forward
session.setPortForwardingL(localPort, "127.0.0.1", remotePort);
//Show message
System.out.println("Waiting for connections…");
//Exit on return
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
in.readLine();
}
//Failed
catch(Exception ex)
{
//Message
System.out.println("Failed to setup tunnel");
//Stack trace
ex.printStackTrace();
}
//Clean up
finally
{
try{session.disconnect();} catch(Throwable t) {t.printStackTrace();}
}然而,我一直收到这样的错误: com.jcraft.jsch.JSchException: java.net.ConnectException:操作超时
我不明白为什么它给了我这个错误,因为我相信我使用的是正确的信息。因此,我的问题是,如何检查主机的远程ip,以及如何确保它们允许SSH访问?
更新:
我能够通过ssh MySQL -v user@host连接到远程-v数据库。但是,在我的代码中,我得到了以下错误:
com.jcraft.jsch.JSchException: PortForwardingL:本地端口127.0.0.1:2022不能绑定。
我使用的是mac,在mac中本地端口不同吗?
发布于 2016-05-02 06:06:12
这里有几个步骤来隔离问题的根源。因为它可能与您的Java代码无关。
ssh -v user@hostssh -v user@host -L 2022:127.0.0.1:3306
您现在应该能够通过本地端口2022连接到2022。SSH隧道和MySql:MySQL远程连接:SSH-隧道,HTTP-隧道的一些有用的链接
https://stackoverflow.com/questions/36974361
复制相似问题