How can I connect to remote MySQL database through SSH from java application? Small code example is helpful for me and I’d appreciate this.
How can I connect to remote MySQL database through SSH from java application? Small
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
My understanding is that you want to access a mysql server running on a remote machine and listening on let’s say port 3306 through a SSH tunnel.
To create such a tunnel from port 1234 on your local machine to port 3306 on a remote machine using the command line ssh client, you would type the following command from your local machine:
To do the same thing from Java, you could use JSch, a Java implementation of SSH2. From its website:
For an example, have a look at
PortForwardingL.java. Once the session connected, create your JDBC connection to MySQL using something likejdbc:mysql://localhost:1234/[database]as connection URL.