当我尝试从PC通过SSH连接在远程服务器中连接mysql时,出现以下错误消息。 我安装了所有必需的ssh,Java jar和信任关系,能够访问SSH,但无法访问mysql DB。我真的不知道我在哪里错了。下面的代码显示了端口和Ssh会话的变量:
以下错误消息:
com.jcraft.jsch.JSchException: PortForwardingL: local port 127.0.0.1:3306 cannot be bound.
at com.jcraft.jsch.PortWatcher.<init>(PortWatcher.java:158)
at com.jcraft.jsch.PortWatcher.addPort(PortWatcher.java:110)
at com.jcraft.jsch.Session.setPortForwardingL(Session.java:1847)
at com.jcraft.jsch.Session.setPortForwardingL(Session.java:1828)
at com.jcraft.jsch.Session.setPortForwardingL(Session.java:1809)
at com.jcraft.jsch.Session.setPortForwardingL(Session.java:1792)
at connectsshserver.CTestDriver.doSshTunnel(CTestDriver.java:29)
at connectsshserver.CTestDriver.main(CTestDriver.java:47)
Caused by: java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(DualStackPlainSocketImpl.java:106)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:190)
at java.net.ServerSocket.bind(ServerSocket.java:375)
at java.net.ServerSocket.<init>(ServerSocket.java:237)
at com.jcraft.jsch.PortWatcher.<init>(PortWatcher.java:150)
我的代码是这样的: 软件包connectsshserver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class CTestDriver {
private static void doSshTunnel(String strSshUser, String strSshPassword, String strSshHost, int nSshPort,
String strRemoteHost, int nLocalPort, int nRemotePort) throws JSchException {
final JSch jsch = new JSch();
Session session = jsch.getSession(strSshUser, strSshHost, 21098);
session.setPassword(strSshPassword);
final Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect(30000);
// String boundaddress ="0.0.0.0";
//t assinged_port=session.setPortForwardingL(boundaddress,lport, rhost, rport);
session.setPortForwardingL(
nLocalPort, strRemoteHost, nRemotePort);
}
public static void main(String[] args) {
try {
String strSshUser = "softeuab"; // SSH loging username
String strSshPassword = "SP1234a1234b"; // SSH login password
String strSshHost = "host47.registrar-servers.com";//me or ip or // SSH server
int nSshPort = 21098; // remote SSH host port number
String strRemoteHost = "host47.registrar-servers.com"; // hostname or
// ip of
// your // database server
int nLocalPort =3306;//cal port number use to bind SSH tunnel
int nRemotePort = 3306; // remote port number of your database
String strDbUser = "softeuab_mohamed"; // database loging username
String strDbPassword = "1234a1234b"; // database login password
CTestDriver.doSshTunnel(strSshUser, strSshPassword, strSshHost, nSshPort, strRemoteHost, nLocalPort,
nRemotePort);
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:" + nLocalPort, strDbUser,
strDbPassword);
con.close();
// Connection con=Connect.ConnectDB();
// con.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.exit(0);
}
}
}
此行显示错误
session.setPortForwardingL(
nLocalPort, strRemoteHost, nRemotePort);
答案 0 :(得分:0)
我只是尝试了您的代码(除了注释了端口转发后运行的mysql代码。)一切都对我有用-调用session.setPortForwardingL时没有错误。