I need to connect to Mysql RDS DB through ssh connection, what i have tried is to open an ssh tunnel and then connect to that DB, but it did not work.
Below is my code:
int lport = 3306;
String host = "10.254.64.135";
String rhost = "localhost";
int rport = 8000;
String user = "ubuntu";
String dbuserName = "someusername";
String dbpassword = "somepassword";
String dburl = "jdbc:mysql://somedbhost.us-east-1.rds.amazonaws.com:3306";
String driverName = "com.mysql.jdbc.Driver";
String driverName2 = "org.gjt.mm.mysql.Driver";
Session session = null;
try {
final java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
final JSch jsch = new JSch();
session = jsch.getSession(user, host, 22);
jsch.addIdentity("C:\\Users\\rop\\workspace\\awskey.pem");
session.setConfig(config);
session.connect();
System.out.println("Connected through SSH");
final int assinged_port = session.setPortForwardingL(rport, host,lport );
System.out.println("localhost:" + assinged_port + " -> " + rhost + ":" + rport);
System.out.println("Port Forwarded");
//mysql database connectivity
Class.forName("com.mysql.jdbc.Driver").newInstance();
//String url = "jdbc:mysql://localhost:" + rport + "/" + db;
con = DriverManager.getConnection(dburl, dbuserName, dbpassword);
System.out.println("Mysql Database connection established");
System.out.println("DONE");
The above code crash with below exception, and crashs in this line "con = DriverManager.getConnection(dburl, dbuserName, dbpassword);":
com.mysql.jdbc.exceptions.jdbc4.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.
mean while when we connect from mysql workbench it does connect normally, below a snapshot:
Please note that the ssh host ip is different than the db host ip
Thanks.
