Java通过ssh连接访问数据库

前言:

在最近的工作中,需要通过ssh的方式远程连接数据库,jdbc默认的连接方式是tcp/ip。查阅资料,发现java可通过JSch实现ssh的端口转发, 从而实现数据库的连接,这里记录一下具体实现过程。

思路:

1.本地和远程建立ssh连接通道;
2.设置ssh本地端口转发,本地转发到远程;
3.通过访问本地的转发端口,实现和远程的数据库建立连接

建立ssh连接通道核心代码
JSch jsch = new JSch();
Session session = null;
try {
    session = jsch.getSession(user, host, port);
    session.setPassword(password);
    session.setConfig("StrictHostKeyChecking", "no");
    // step1:建立ssh连接
    session.connect();
} catch (Exception e) {
    if (null != session) {
        //关闭ssh连接
        session.disconnect();
    }
    e.printStackTrace();
}
本地端口转发核心代码:
    //step2: 设置SSH本地端口转发,本地转发到远程
    int assinged_port = session.setPortForwardingL(lport, rhost, rport);
完整代码如下:
  • SSH.properties配置文件
#本地端口
lport=
#SSH服务器ip
host=
#SSH访问端口
port=
#SSH连接用户名
user=
#SSH连接密码
password=
# 远程数据库服务器
rhost=
#远程数据库服务端口
rport=
  • SSHService.java文件
package com.tools.linkmode;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * SSH端口转发
 */
public class SSHService {
    static Integer lport;//本地端口
    static String rhost;//远程数据库服务器
    static int rport;//远程数据库服务端口

    static String user;//SSH连接用户名
    static String password;//SSH连接密码
    static String host;//SSH服务器
    static int port;//SSH访问端口

    static {
        //读取配置文件
        try {
            // 获取hive.properties文件的路径
            InputStream is = SSHService.class.getClassLoader().getResourceAsStream("SSH.properties");
            Properties prop = new Properties();
            prop.load(is);
            // 读取配置文件的值
            lport = Integer.valueOf(prop.getProperty("lport"));
            rhost = prop.getProperty("rhost");
            rport = Integer.valueOf(prop.getProperty("rport"));
            user = prop.getProperty("user");
            password = prop.getProperty("password");
            host = prop.getProperty("host");
            port = Integer.valueOf(prop.getProperty("port"));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void sshRun() {
        JSch jsch = new JSch();
        Session session = null;
        try {
            session = jsch.getSession(user, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            // step1:建立ssh连接
            session.connect();
            System.out.println(session.getServerVersion());//这里打印SSH服务器版本信息
            //step2: 设置SSH本地端口转发,本地转发到远程
            int assinged_port = session.setPortForwardingL(lport, rhost, rport);
            System.out.println("localhost:" + assinged_port + " -> " + rhost + ":" + rport);
        } catch (Exception e) {
            if (null != session) {
                //关闭ssh连接
                session.disconnect();
            }
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        sshRun();
    }

}

通过ssh连接访问数据库

1.配置好SSH.properties文件
2.运行SSHService.java的sshRun方法,开启转发连接
3.配置数据库的url:如:url=jdbc:postgresql://localhost:9005/test
  这里将localhost:9005作为ssh转发IP:端口
4.操作数据库可以参见前一篇博文:java通过jdbc查询数据

喜欢关注点个赞!

你可能感兴趣的:(Java通过ssh连接访问数据库)