当我试图通过ssh连接从我的pc连接到远程服务器上的mysql时,我收到了下面的错误消息。我安装了所有必需的ssh,java jar和trust可以访问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)
我的代码是:package 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);
1条答案
按热度按时间c8ib6hqw1#
我刚刚试过你的代码(除了注解掉端口转发后运行的mysql代码)一切都很好-call session.setportforwardingl上没有错误。