我已经尝试了一切来创建与我的远程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 -v user @ host连接到远程MySQL数据库。但是,在我的代码中我收到此错误:
com.jcraft.jsch.JSchException:PortForwardingL:本地端口127.0.0.1:2022无法绑定。
我使用的是mac,是macs中的本地端口不同吗?
答案 0 :(得分:0)
这里有几个步骤来隔离问题的根源。因为它可能与您的Java代码无关。
ssh -v user@host
ssh -v user@host -L 2022:127.0.0.1:3306
您现在应该可以通过本地端口2022
连接到MySql SSH隧道和MySql的一些有用链接:MySQL Remote Connectivity: SSH-Tunnel, HTTP-Tunnel