Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to MySql database with SSH tunneling on remote host with specific mysql host

I am using Jsch to connect to the remote mysql database where the ssh host is not the same as the mysql host as shown by the Upper half of the picture below: enter image description here

Below is the code I am using for SSH Connection:

private static void connectSSH() throws SQLException {
    try {
        java.util.Properties config = new java.util.Properties();
        JSch jsch = new JSch();
        jsch.setLogger(new MyLogger());
        session = jsch.getSession(sshUser, sshHost, 22);
        jsch.addIdentity(SshKeyFilepath, sshPassword);
        config.put("StrictHostKeyChecking", "no");
        config.put("ConnectionAttempts", "3");
        session.setConfig(config);
        session.connect();

        System.out.println("SSH Connected");

        Class.forName(driverName).newInstance();

        int assinged_port = session.setPortForwardingL(localPort, remoteHost, remotePort);

        System.out.println("localhost:" + assinged_port + " -> " + sshHost + ":" + remotePort);
        System.out.println("Port Forwarded");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

and eventually connecting to the database using the code below:

 Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:" + localPort, dbUser, dbPassword);

I am able to do an SSH connection successfully but the execution is getting hung up when at the line below:

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:" + localPort, dbUser, dbPassword);

I have double checked my SSH and database credentials by connecting on commandline I am able to connect to the database using

mysql -h host1 -u "myusername" -p"mypasswordhere"

I suppose the problem might be due to the fact that on remote host the mysql host is not localhost but host1 which I don't know where to specify in the jdbc url.

like image 231
Anirudh Avatar asked Jan 12 '16 12:01

Anirudh


People also ask

How to connect to a MySQL database using SSH tunnel?

Dbeaver - Database connection using SSH Tunnel 1 Open dbeaver 2 Click on " New Database Connection ", in the following " main " window enter the MySQL server host relative to the SSH... 3 In the " SSH " part, specify the SSH host, port, user also the authentication method used, like SSH private key Test... More ...

How to set up a port 3306 SSH tunnel for MySQL?

This doesn't just have to work with MySQL, it can be used for any TCP based service, such as HTTP on port 80. Open PuTTY and enter the server hostname or IP address. Next we need to setup the tunnel. On the Category page, choose Connection -> SSH -> Tunnels. Input Source port 3306. Destination 127.0.0.1:3306. Click Add.

How do I SSH into a MySQL server from another server?

This doesn't just have to work with MySQL, it can be used for any TCP based service, such as HTTP on port 80. Open PuTTY and enter the server hostname or IP address. Next we need to setup the tunnel. On the Category page, choose Connection -> SSH -> Tunnels. Input Source port 3306.

How to connect to a remote server using SSH tunnel?

The forwarded port is listed. Click Open. It will start SSH connection to remote server. You will need to enter username and password. After you're successfully connected do not close the PuTTY window because it has the SSH Tunnel to the remote server.


1 Answers

I know this is old, but the localSSHUrl is really the host that you would use when you are logged in to SSH.

In short, this is usually “localhost” when you are trying to connect to databases as a local user.

int assigned_port = session.setPortForwardingL(localPort, “localhost”, remotePort);

Many examples use remote_host to connect both to the SSH and to the database via port forwarding, but if you only have local access to the database, it will fail to connect.

like image 130
Otmar Avatar answered Oct 27 '22 07:10

Otmar