Trying to connect to mySQL database with ssh key file. Here is my setup:
public class DBconnect {
private Connection con;
private Statement st;
private ResultSet rs;
String strSshUser = "XXXX"; // SSH loging username
String strSshPassword = "XXXX"; // SSH login password
String strSshHost = "XXXX"; // hostname or ip or SSH server
int nSshPort = 22; // remote SSH host port number
String strRemoteHost = "XXXX"; // hostname or ip of your database server
int nLocalPort = 3366; // local port number use to bind SSH tunnel
int nRemotePort = 3306; // remote port number of your database
String strDbUser = "XXXX"; // database loging username
String strDbPassword = "XXXX"; // database login password
String privateKey = "/directory/inside/package/.pem-key";
private static void doSshTunnel( String strSshUser, String strSshPassword, String strSshHost, int nSshPort, String strRemoteHost, int nLocalPort, int nRemotePort,String privateKey ) throws JSchException
{
JSch jsch = new JSch();
//jsch.addIdentity(privateKey);
Session session = jsch.getSession( strSshUser, strSshHost, 22 );
session.setPassword( strSshPassword );
final Properties config = new Properties();
config.put( "StrictHostKeyChecking", "no" );
session.setConfig( config );
session.connect();
session.setPortForwardingL(nLocalPort, strRemoteHost, nRemotePort);
}
public DBconnect() {
try{
doSshTunnel(strSshUser, strSshPassword, strSshHost, nSshPort, strRemoteHost, nLocalPort, nRemotePort, privateKey);
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(""jdbc:mysql:XXXX:3306/XXXX","XXXX","XXXX"");
st = con.createStatement();
}catch(Exception ex){
System.out.println("Error: " + ex);
}
}
I get the error -- com.jcraft.jsch.JSchException: Auth fail -- when I try this, and when I comment out -- //jsch.addIdentity(privateKey) -- I get the error of -- java.io.FileNotFoundException: "/directory/inside/package/.pem-key"(No such file or directory). Am I even approaching this correctly? I think this is the proper way to connect to my mysql db through ssh but I dont know how to include my .pem key file to complete the connection.