Springboot connects to the mysql database through the ssh channel, springbootssh
Navicat can connect to the mysql database through the ssh channel. How can this problem be achieved in java and how can spring boot be integrated?
1. Add a package
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.53</version> </dependency>
2. Create an ssh connection tool to connect to the ssh server
import com.jcraft.jsch.JSch;import com.jcraft.jsch.Session;import java.util.Properties;public class SSHConnection {private final static String S_PATH_FILE_PRIVATE_KEY = "/Users/hdwang/.ssh/id_rsa";private final static String S_PATH_FILE_KNOWN_HOSTS = "/Users/hdwang/.ssh/known_hosts";private final static String S_PASS_PHRASE = "";private final static int LOCAl_PORT = 3307;private final static int REMOTE_PORT = 3306; private final static int SSH_REMOTE_PORT = 1022;private final static String SSH_USER = "zhangsan";private final static String SSH_PASSWORD = "123456";private final static String SSH_REMOTE_SERVER = "192.168.0.2";private final static String MYSQL_REMOTE_SERVER = "123.mysql.com"; private Session sesion; //represents each ssh sessionpublic void closeSSH (){ sesion.disconnect();}public SSHConnection () throws Throwable{ JSch jsch = null; jsch = new JSch(); jsch.setKnownHosts(S_PATH_FILE_KNOWN_HOSTS); //jsch.addIdentity(S_PATH_FILE_PRIVATE_KEY); sesion = jsch.getSession(SSH_USER, SSH_REMOTE_SERVER, SSH_REMOTE_PORT); sesion.setPassword(SSH_PASSWORD); Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); sesion.setConfig(config); sesion.connect(); //ssh connection established! //by security policy, you must connect through a fowarded port sesion.setPortForwardingL(LOCAl_PORT, MYSQL_REMOTE_SERVER, REMOTE_PORT);}}
3. Manage ssh connections
import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.annotation.WebListener;@WebListenerpublic class MyContextListener implements ServletContextListener { private SSHConnection conexionssh; public MyContextListener() { super(); } /** * @see ServletContextListener#contextInitialized(ServletContextEvent) */ public void contextInitialized(ServletContextEvent arg0) { System.out.println("Context initialized ... !"); try { conexionssh = new SSHConnection(); } catch (Throwable e) { e.printStackTrace(); // error connecting SSH server } } /** * @see ServletContextListener#contextDestroyed(ServletContextEvent) */ public void contextDestroyed(ServletContextEvent arg0) { System.out.println("Context destroyed ... !"); conexionssh.closeSSH(); // disconnect }}
4. Configure the database, which is the same as what you previously configured, but you need to modify the connection address.
spring.datasource.url=jdbc:mysql://127.0.0.1:3307/test?characterEncoding=utf8spring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Note:
The principle is that the program creates an ssh connection on the local machine, connects to the ssh server, and then sends database operation commands. The commands are forwarded to the target database server and the operation results are returned.
Note: The database connection address is changed from 123.mysql.com: 3306 to 127.0.0.1: 3307. In this way, the ssh connection forwards the operations on 127.0.0.1: 3307 to 123.mysql.com: 3306, then you can operate the database normally. You can create an ssh connection by using a private key or a user name and password.
Reference address:
1. https://stackoverflow.com/questions/33526427/spring-boot-ssh-mysql
2. http://blog.weiqinxue.cn/blogs/index.php/User/articleview/ArticleID/U2A213