【问题标题】:Java SSH MySQL connectionJava SSH MySQL 连接
【发布时间】:2016-06-27 06:47:46
【问题描述】:

如何在 Java 代码中实现这一点?

ssh -L 1234:localhost:3306 mysql.server.remote

我有一个 Java 桌面应用程序,数据库位于共享服务器(互联网)中,可以从任何地方访问。会用软件的人不会打开终端执行前后命令的人会用软件...

所以我需要一个解决方案来在应用程序启动时将我的应用程序与数据库连接起来,并在它们将关闭应用程序时终止连接(conn.close() 和 ssh)。为此,所有这些(上面的代码)都应该在我的脚本中......这是我无法实现的......

我使用了 JSch,并且成功创建了 SSH 隧道(在端口 22 上),但无法连接 MySQL...欢迎任何帮助。

整个代码在这里:

public static void main(String[] args) throws SQLException {

    String host="ssh.Mysite.com";
    String rhost="localhost";
    int rport=3306;
    int lport=4321;

    String user="userMysite.com";
    String password="passMysite.com";

    String dbuserName = "UserDB.com";
    String dbpassword = "PassDB.com";
    String url = "jdbc:mysql://localhost:3306/DB.com";
    String driverName="com.mysql.jdbc.Driver";
    Session session= null;

    try{
  //ssh tunnel creation          
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        JSch jsch = new JSch();
        session=jsch.getSession(user, host, 22);
        session.setPassword(password);
        session.setConfig(config);
        session.connect();
        int assinged_port=session.setPortForwardingL(lport, rhost, rport);

  //mysql database connect                  
        Class.forName(driverName).newInstance();
        dbConn  = DriverManager.getConnection (url, dbuserName, dbpassword);

 String sql = "SELECT * FROM client ";      
     PreparedStatement ps = dbConn.prepareStatement(sql);
     ResultSet rs = ps.executeQuery();
            while (rs.next()){
                  System.out.println(rs.getString("name"));
            }

    }catch(JSchException e){
        e.printStackTrace();            
    }finally{
        if(dbConn != null && !dbConn.isClosed()){
            dbConn.close();
        }
        if(session !=null && session.isConnected()){
            session.disconnect();
        }
    }
}

出于测试目的,我编写了以下代码:

String url = "jdbc:mysql://localhost:3306";
String driverName="com.mysql.jdbc.Driver";
Class.forName(driverName);
Connection dbConn = DriverManager.getConnection (url, dbuserName, dbpassword); 

        try {
           ResultSet rs = dbConn.getMetaData().getCatalogs();
                        while (rs.next()) {
                            System.out.println("TABLE_CAT = " + rs.getString("TABLE_CAT") );
                        }           
         }catch(SQLException e){
                         e.printStackTrace(); 
         }

结果:

localhost:3306 => 打印我电脑 localhost 中的所有数据库;

remote.server.mysql:3306remote.server.mysql:4321localhost:4321 => 错误

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.

谢谢!

【问题讨论】:

  • 如何在代码中打开与 mysql 的连接?
  • 改成jdbc:mysql://mysql.server.remote:3306/remoteDB
  • dbConn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/remoteDB, dbuserName, dbpassword"); - 创建隧道后
  • Connected localhost:4321 -> localhost:3306 Port Forwarded com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 最后一个成功发送到服务器的数据包是 0 毫秒前。驱动程序没有收到来自服务器的任何数据包。在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 关闭 SSH 连接
  • 如果我在连接中使用 远程服务器 给我Error : Communications link failure;如果我使用 localhost Error :Unknown database 'remoteDB'

标签: java mysql ssh


【解决方案1】:

您可以尝试从 Java 运行 ssh 二进制文件吗?例如:

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("ssh -L 1234:localhost:3306 mysql.server.remote");

显然,这需要系统在 PATH 中安装 ssh。

【讨论】:

    猜你喜欢
    • 2016-08-13
    • 2014-10-08
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    • 2021-11-01
    • 1970-01-01
    • 2011-03-05
    • 2021-08-07
    相关资源
    最近更新 更多