1

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.

Mikhail Vega
  • 491
  • 1
  • 6
  • 6
  • are you specifying an absolute vs a relative path? –  Jan 16 '15 at 19:02
  • relative path because im going to have to create a jar file for others to use – Mikhail Vega Jan 16 '15 at 19:06
  • i think i got the same failure of directory not found when i specified the absolute path as well. – Mikhail Vega Jan 16 '15 at 19:07
  • If the path begins with a `/`, then that isn't a relative path Try beginning it with `./`. Also, check where the working directory of the program is. It might not be where you think it is –  Jan 16 '15 at 19:10
  • so i right clicked my file and copied the path file, which is the package directory of project; i tried what you suggested and this is what I get -- Error: com.jcraft.jsch.JSchException: java.io.FileNotFoundException: ./NewCustomer/src/qa-cloud.pem (No such file or directory) – Mikhail Vega Jan 16 '15 at 19:13
  • check the working directory of your project. http://stackoverflow.com/questions/4871051/getting-the-current-working-directory-in-java –  Jan 16 '15 at 19:17
  • its saying null, I placed the file I needed directly into the package and when I use this code:System.out.println("Working Directory = "+System.getProperty("qa-cloud.pem")); i get null – Mikhail Vega Jan 16 '15 at 19:31
  • I think you have to send the string "user.dir", not the filename of the file you want to find. –  Jan 16 '15 at 20:28
  • Are you trying to get Jsch to use a key file stored in a jar? `Jsch.addIdentity(String)` reads a key from a file in the filesystem. If you want to use a key from a resource, you'll have to read the key resource yourself and then call one of the other `addIdentity()` functions. – Kenster Jan 16 '15 at 20:56

0 Answers0