【问题标题】:I can connect to mysql with Navicat through SSH tunnel but can not connet using shell or java我可以通过 SSH 隧道使用 Navicat 连接到 mysql 但无法使用 shell 或 java 连接
【发布时间】:2020-03-13 14:42:29
【问题描述】:

在 Navicat 通用选项卡
服务器类型:MySQL
服务器名称/IP 地址:ABC
端口:3306
编码="65001"
用户名:user1
密码:密码1

SSH 选项卡
SSH 服务器名称/IP 地址:xx.xx.xx.xx
端口:22
用户名:user2
密码:密码2

使用命令行 ssh 客户端,我从本地计算机键入以下命令:
ssh -L 1234:localhost:3306 user2@xx.xx.xx.xx
此帐户目前不可用。
与 xx.xx.xx.xx 的连接已关闭。

此外,我无法 ping ABC(MySQL 服务器名称)

【问题讨论】:

    标签: mysql ssh


    【解决方案1】:
    void initDB() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
            //
            int assigned_port = 0;
    
            try {
                JSch jsch = new JSch();
    
                // Create SSH session. Port 22 is your SSH port which is open in your firewall
                // setup.
                // ssh user:SSH loging username
                // ssh password:SSH login password
                // ssh host: hostname or ip of SSH server
                // ssh port:remote SSH host port number
                Session session = jsch.getSession("sshuser", "xxx.xxx.xxx.xxx", 22);
                session.setPassword("sshpass");
    
                // Additional SSH options.
                java.util.Properties config = new java.util.Properties();
                config.put("StrictHostKeyChecking", "no");
                config.put("Compression", "yes");
                config.put("ConnectionAttempts", "2");
    
                session.setConfig(config);
                System.out.println("step 1");
                // Connect
                session.connect();
                System.out.println("step 2");
                // Create the tunnel through port forwarding.
                // This is basically instructing jsch session to send data received from
                // local_port in the local machine to remote_port of the remote_host
                // assigned_port is the port assigned by jsch for use,
                // it may not always be the same as local_port.
    
                // local port:local port number use to bind SSH tunnel,any free port can be used
                // database host:hostname or ip of database server
                // remote port:remote port number of your database server
                assigned_port = session.setPortForwardingL(1117, "databaseserver", 3306);
                System.out.println("assigned_port I :" + assigned_port);
    
            } catch (JSchException e) {
                System.out.println("JSchException**:\n" + e.getMessage());
    //            LOGGER.log(Level.SEVERE, e.getMessage()); 
            }
    
            if (assigned_port == 0) {
    //            LOGGER.log(Level.SEVERE, "Port forwarding failed !"); 
                System.out.println("Port forwarding failed !");
            }
    
            // Database access credentials.
            final String database_user = "dbuser";
            final String database_password = "dbpasswd";
            final String database = "dbname";
    
            // Build the database connection URL.
            // use assigned_port to establish database connection
            System.out.println("assigned_port II :" + assigned_port);
    
            String url = "jdbc:mysql://localhost:" + assigned_port + "/" + database;
            Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
            connect = DriverManager.getConnection(url, database_user, database_password);
        }
    

    【讨论】:

      猜你喜欢
      • 2014-03-21
      • 2023-04-01
      • 2016-08-07
      • 2018-10-06
      • 2019-03-30
      • 2018-05-05
      • 1970-01-01
      相关资源
      最近更新 更多