I can connect to mysql with Navicat through SSH tunnel but can not connet using shell or java

十年热恋 提交于 2020-01-25 06:42:11

问题


in Navicat General TAB
serverType:MySQL
serverName/IP Address:ABC
port:3306
Encoding="65001"
username:user1
password:password1

SSH TAB
SSH serverName/IP Address:xx.xx.xx.xx
port:22
username:user2
password:password2

using the command line ssh client, I type the following command from my local machine:
ssh -L 1234:localhost:3306 user2@xx.xx.xx.xx
This account is currently not available.
Connection to xx.xx.xx.xx closed.

besides, I can not ping ABC(MySQL server name)


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


来源:https://stackoverflow.com/questions/58911008/i-can-connect-to-mysql-with-navicat-through-ssh-tunnel-but-can-not-connet-using

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!