我已经尝试了所有创建到我的远程MySQL数据库的SSH连接。如何检查我的主机是否不允许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访问?
UPDATE:
我能够通过ssh -v用户@主机连接到远程MySQL数据库。但是,在我的代码中,我收到此错误:
com.jcraft.jsch.JSchException:PortForwardingL:本地端口127.0.0.1:2022无法绑定。
我正在使用mac,是mac中的本地端口不同?
或许你也应该指定SSH端口('INT远程端口= 22;');如果这不起作用,您需要询问您的提供商。 –
port'3306'用于mysql。你想用SSH做什么?只有通过SSH的远程SSH或MySQL连接? –
您可以请发布例外的statcktrace?如果您的手动连接正在工作,包括MySQL端口的转发。那么你的代码也应该工作。目前为止没有错。 – SubOptimal