10

I have a MySQL database behind a firewall which can only be accessed via an SSH connection. Does anyone know of an implementation of java.sql.Connection which would allow me to make an SSH connection to this database?

Jonas
  • 121,568
  • 97
  • 310
  • 388
Elie
  • 13,693
  • 23
  • 74
  • 128
  • For better answers, see [Connect to remote MySQL database through SSH using Java](https://stackoverflow.com/q/1968293/850848) – Martin Prikryl Apr 18 '19 at 07:05

2 Answers2

15

You can use SSH's port forwarding to do this. While not a pure java.sql.Connection, it will allow you to tunnel the connection through ssh.

ssh -L 3306:localhost:3306 remote.mysql.host.com

This will forward port 3306 on your local machine to port 3306 on remote.mysql.host.com. This will allow you to connect to port 3306 on your local machine, and it will be tunnelled to remote.mysql.host.com.

If you're looking to do it all in Java, create the ssh connection with JSch.

Kevin Wright
  • 49,540
  • 9
  • 105
  • 155
Steve K
  • 19,408
  • 6
  • 52
  • 50
0

Here is an example using the library sshj.

James Newman
  • 161
  • 5