我可以通过SSH隧道通过Navicat连接到mysql,但不能使用shell或java连接
在Navicat常规选项卡中我可以通过SSH隧道通过Navicat连接到mysql,但不能使用shell或java连接,mysql,ssh,Mysql,Ssh,在Navicat常规选项卡中 服务器类型:MySQL 服务器名/IP地址:ABC 端口:3306 Encoding=“65001” 用户名:user1 密码:password1 SSH选项卡 SSH服务器名/IP地址:xx.xx.xx.xx 端口:22 用户名:user2 密码:password2 使用命令行ssh客户端,我从本地计算机键入以下命令: ssh-L 1234:localhost:3306user2@xx.xx.xx.xx 此帐户当前不可用。 与xx.xx.xx.xx的连接已关闭。
服务器类型:MySQL
服务器名/IP地址:ABC
端口:3306
Encoding=“65001”
用户名:user1
密码:password1
SSH选项卡
SSH服务器名/IP地址:xx.xx.xx.xx
端口:22
用户名:user2
密码:password2
使用命令行ssh客户端,我从本地计算机键入以下命令:
ssh-L 1234:localhost:3306user2@xx.xx.xx.xx
此帐户当前不可用。
与xx.xx.xx.xx的连接已关闭。
此外,我不能ping ABC(MySQL服务器名称)
void initDB() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
//
int assigned_port = 0;
try {
JSch jsch = new JSch();
// Create SSH session. Port 22 is your SSH port which is open in your firewall
// setup.
// ssh user:SSH loging username
// ssh password:SSH login password
// ssh host: hostname or ip of SSH server
// ssh port:remote SSH host port number
Session session = jsch.getSession("sshuser", "xxx.xxx.xxx.xxx", 22);
session.setPassword("sshpass");
// Additional SSH options.
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
config.put("Compression", "yes");
config.put("ConnectionAttempts", "2");
session.setConfig(config);
System.out.println("step 1");
// Connect
session.connect();
System.out.println("step 2");
// Create the tunnel through port forwarding.
// This is basically instructing jsch session to send data received from
// local_port in the local machine to remote_port of the remote_host
// assigned_port is the port assigned by jsch for use,
// it may not always be the same as local_port.
// local port:local port number use to bind SSH tunnel,any free port can be used
// database host:hostname or ip of database server
// remote port:remote port number of your database server
assigned_port = session.setPortForwardingL(1117, "databaseserver", 3306);
System.out.println("assigned_port I :" + assigned_port);
} catch (JSchException e) {
System.out.println("JSchException**:\n" + e.getMessage());
// LOGGER.log(Level.SEVERE, e.getMessage());
}
if (assigned_port == 0) {
// LOGGER.log(Level.SEVERE, "Port forwarding failed !");
System.out.println("Port forwarding failed !");
}
// Database access credentials.
final String database_user = "dbuser";
final String database_password = "dbpasswd";
final String database = "dbname";
// Build the database connection URL.
// use assigned_port to establish database connection
System.out.println("assigned_port II :" + assigned_port);
String url = "jdbc:mysql://localhost:" + assigned_port + "/" + database;
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
connect = DriverManager.getConnection(url, database_user, database_password);
}