SSH Tunneling Example for MySQL

31 Jan 2023

This was done as a one-time troubleshooting experiment. May never need it again, but just in case…

Windows PowerShell (as admin), install OpenSSH client:

1
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'

If needed, use PuttyGen to export the Putty-format key to OpenSSH format.

Windows command line (-N means “no remote command”):

1
2
REM ssh -N -L SOURCE-PORT:127.0.0.1:DESTINATION-PORT -i path/to/SSH_KEYFILE user@SERVER
ssh -N -L 3307:127.0.0.1:3306 -i openssh-keyfile ec2-user@ec2-9-999-999-999.compute-1.amazonaws.com

The console will not show any messages or prompts.

Java:

Java
1
2
// local port 3307 tunnels to remote server port 3306, where MySQL is running:
DriverManager.getConnection("jdbc:mysql://localhost:3307/my_db", "user", "pass");

Ctrl-C to close the tunnel.