博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java SSH连接mysql数据库
阅读量:2386 次
发布时间:2019-05-10

本文共 3514 字,大约阅读时间需要 11 分钟。

当目标数据库不能直连的,需要一个服务器作为中间跳板的时候,我们需要通过SSH通道连接数据库,其使用方式如下:

引入依赖

com.jcraft
jsch
0.1.55

编写SSHConnection连接类

import com.jcraft.jsch.JSch;import com.jcraft.jsch.Session;import java.util.Properties;public class SSHConnection {
private final static String S_PATH_FILE_KNOWN_HOSTS = "localhost"; private final static int LOCAl_PORT = 3307; private final static int REMOTE_PORT = 3306; private final static int SSH_REMOTE_PORT = 22; private final static String SSH_USER = "root"; private final static String SSH_PASSWORD = "123456"; private final static String SSH_REMOTE_SERVER = "39.100.4.8"; private final static String MYSQL_REMOTE_SERVER = "39.22.22.1"; private Session sesion; //represents each ssh session public 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); }}

编写MyContextListener监听

import org.springframework.stereotype.Component;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.annotation.WebListener;@WebListener@Componentpublic 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 }}

更改数据库配置文件

# 服务端口和服务名称spring:    datasource:        url: jdbc:mysql://localhost:3307/cs?useUnicode=true&characterEncoding=UTF8&allowMultiQueries=true&useSSL=false&serverTimezone=UTC        username: cs        password: 987654321        driver-class-name: com.mysql.jdbc.Driver        type: com.alibaba.druid.pool.DruidDataSource        connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=15000        initialSize: 5        minIdle: 5        maxActive: 20        maxWait: 60000        timeBetweenEvictionRunsMillis: 60000        minEvictableIdleTimeMillis: 300000        validationQuery: SELECT 1 FROM DUAL        testWhileIdle: true        testOnBorrow: false        testOnReturn: false        poolPreparedStatements: true        maxPoolPreparedStatementPerConnectionSize: 20        filters: stat,wall,log4j        useGlobalDataSourceStat: true

这样使得使用本地指定的3307端口的时候会通过跳板服务器连接目标数据库,而其它端口则不会例如3306

至此完毕

转载地址:http://rcnab.baihongyu.com/

你可能感兴趣的文章
通过修改kong属性解决不能获取外网域名的问题
查看>>
Eclipse带命令行参数调试
查看>>
php smtp发送邮件
查看>>
yii框架的404、500等异常处理
查看>>
yii框架在layout模式下,模版和layout文件的渲染顺序
查看>>
php5对象复制、clone、浅复制与深复制
查看>>
php设计模式
查看>>
git与github在ubuntu下的使用
查看>>
css pie.htc使用总结
查看>>
python包含中文字符串长度
查看>>
sysbench 0.5 性能测试工具使用手册
查看>>
通过telnet连接查看memcache服务器
查看>>
django不用在数据库中创建新的user表而使用它的后台管理功能
查看>>
php array_unshift()修改数组key
查看>>
mysql性能优化-查询(Query)优化-2
查看>>
MySQL分区表的使用
查看>>
MongoDB 地理位置索引的实现原理
查看>>
MongoDB与MySQL的插入、查询性能测试
查看>>
深入理解OAuth2.0协议
查看>>
https原理:证书传递、验证和数据加密、解密过程解析
查看>>