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:
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.
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 ...
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With