com.jcraft.jsch.JSchException:Auth fail - ssh key

时间:2015-01-16 18:50:08

标签: java mysql ssh

尝试使用ssh密钥文件连接到mySQL数据库。这是我的设置:

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);
    }
}

我收到错误 - com.jcraft.jsch.JSchException:Auth失败 - 当我尝试这个时,当我发表评论时 - //jsch.addIdentity(privateKey) - 我得到错误 - java.io.FileNotFoundException:" /directory/inside/package/.pem-key"(没有这样的文件或目录)。我是否正确接近这个?我认为这是通过ssh连接到我的mysql数据库的正确方法,但我不知道如何包含我的.pem密钥文件来完成连接。

0 个答案:

没有答案