使用 SSH 连接到 MySQL 数据库未通过代码成功但通过 MySQL Workbench 成功
Connecting to MySQL DB using SSH not successful through code but successful through MySQL Workbench
我正在尝试使用 Java 和 SSH 隧道连接到远程(AWS 服务器)MySQL 数据库。
当尝试使用 MySQL Workbench 连接到数据库时,它是成功的:
尝试使用 Java 代码连接到 MySQL 数据库时:
public class SQL {
private final String MYSQL_HOSTNAME_CONNECTION = EnvConf.getProperty("mysql.hostname");
private static final String sshUser = EnvConf.getProperty("ssh.username");
private static final String sshHost = EnvConf.getProperty("ssh.hostname");
public static void main(String[] args) throws JSchException {
establishSSHConnection();
getJDBCRemoteConnection();
}
private static void establishSSHConnection() {
JSch jsch = new JSch();
Session session = null;
try {
jsch.addIdentity(PATH_TO_PEM_FILE);
session = jsch.getSession(sshUser, sshHost);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("shell");
channel.connect();
System.out.println("Connected!!!");
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
private static Connection getJDBCRemoteConnection() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String dbName = "dbName"
String userName = "root";
String password = "password";
String hostname = "RDS_HOST_NAME";
String port = "3306";
String jdbcUrl = "jdbc:mysql://" + hostname + ":" + port + "/" + dbName + "?user=" + userName + "&password=" + password;
Connection con = DriverManager.getConnection(jdbcUrl);
return con;
} catch (ClassNotFoundException | SQLException e) {
System.out.println(e.toString());
}
return null;
}
}
我收到下一条错误消息:
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
==============================编辑============== =================
这是 SSH 隧道信息:
ubuntu@SSH_HOSTNAME_STRING:22
并使用 Bastion PEM 文件进行身份验证。
好的,我已经弄清楚我的问题是什么,现在我解决了它并且它按预期工作:
首先我没有使用我在建立ssh隧道连接时创建的转发端口:
public class SQL {
private final String MYSQL_HOSTNAME_CONNECTION = EnvConf.getProperty("mysql.hostname");
private static final String sshUser = EnvConf.getProperty("ssh.username");
private static final String sshHost = EnvConf.getProperty("ssh.hostname");
private static int forwardedPort;
public static void main(String[] args) throws JSchException {
establishSSHConnection();
getJDBCRemoteConnection();
}
private static void establishSSHConnection() {
JSch jsch = new JSch();
Session session = null;
try {
jsch.addIdentity(PATH_TO_PEM_FILE);
session = jsch.getSession(sshUser, sshHost);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
forwardedPort = session.setPortForwardingL( 0, MYSQL_HOSTNAME_CONNECTION , 3306);
Channel channel = session.openChannel("shell");
channel.connect();
System.out.println("Connected!!!");
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
private static Connection getJDBCRemoteConnection() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String dbName = "dbName"
String userName = "root";
String password = "password";
String port = "3306";
String jdbcUrl = "jdbc:mysql://localhost:" + forwardedPort + "/" + dbName + "?user=" + userName + "&password=" + password;
Connection con = DriverManager.getConnection(jdbcUrl);
return con;
} catch (ClassNotFoundException | SQLException e) {
System.out.println(e.toString());
}
return null;
}
}
我正在尝试使用 Java 和 SSH 隧道连接到远程(AWS 服务器)MySQL 数据库。
当尝试使用 MySQL Workbench 连接到数据库时,它是成功的:
尝试使用 Java 代码连接到 MySQL 数据库时:
public class SQL {
private final String MYSQL_HOSTNAME_CONNECTION = EnvConf.getProperty("mysql.hostname");
private static final String sshUser = EnvConf.getProperty("ssh.username");
private static final String sshHost = EnvConf.getProperty("ssh.hostname");
public static void main(String[] args) throws JSchException {
establishSSHConnection();
getJDBCRemoteConnection();
}
private static void establishSSHConnection() {
JSch jsch = new JSch();
Session session = null;
try {
jsch.addIdentity(PATH_TO_PEM_FILE);
session = jsch.getSession(sshUser, sshHost);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("shell");
channel.connect();
System.out.println("Connected!!!");
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
private static Connection getJDBCRemoteConnection() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String dbName = "dbName"
String userName = "root";
String password = "password";
String hostname = "RDS_HOST_NAME";
String port = "3306";
String jdbcUrl = "jdbc:mysql://" + hostname + ":" + port + "/" + dbName + "?user=" + userName + "&password=" + password;
Connection con = DriverManager.getConnection(jdbcUrl);
return con;
} catch (ClassNotFoundException | SQLException e) {
System.out.println(e.toString());
}
return null;
}
}
我收到下一条错误消息:
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
==============================编辑============== ================= 这是 SSH 隧道信息: ubuntu@SSH_HOSTNAME_STRING:22 并使用 Bastion PEM 文件进行身份验证。
好的,我已经弄清楚我的问题是什么,现在我解决了它并且它按预期工作:
首先我没有使用我在建立ssh隧道连接时创建的转发端口:
public class SQL {
private final String MYSQL_HOSTNAME_CONNECTION = EnvConf.getProperty("mysql.hostname");
private static final String sshUser = EnvConf.getProperty("ssh.username");
private static final String sshHost = EnvConf.getProperty("ssh.hostname");
private static int forwardedPort;
public static void main(String[] args) throws JSchException {
establishSSHConnection();
getJDBCRemoteConnection();
}
private static void establishSSHConnection() {
JSch jsch = new JSch();
Session session = null;
try {
jsch.addIdentity(PATH_TO_PEM_FILE);
session = jsch.getSession(sshUser, sshHost);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
forwardedPort = session.setPortForwardingL( 0, MYSQL_HOSTNAME_CONNECTION , 3306);
Channel channel = session.openChannel("shell");
channel.connect();
System.out.println("Connected!!!");
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
private static Connection getJDBCRemoteConnection() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String dbName = "dbName"
String userName = "root";
String password = "password";
String port = "3306";
String jdbcUrl = "jdbc:mysql://localhost:" + forwardedPort + "/" + dbName + "?user=" + userName + "&password=" + password;
Connection con = DriverManager.getConnection(jdbcUrl);
return con;
} catch (ClassNotFoundException | SQLException e) {
System.out.println(e.toString());
}
return null;
}
}