通过Java中的SSH隧道连接到Mongo数据库 [英] Connecting to Mongo database through SSH tunnel in Java

查看:476
本文介绍了通过Java中的SSH隧道连接到Mongo数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已修复(已编辑代码以反映我所做的更改)

我正在尝试使用Java通过SSH隧道连接到Mongo数据库.

I'm trying to connect to a Mongo database through an SSH tunnel using Java.

我正在使用Mongo驱动程序3.0.2和jcraft(JSch)创建SSH隧道. 我的想法是:

I'm using the Mongo driver 3.0.2 and jcraft (JSch) to create an SSH tunnel. The idea is that I:

  • 通过SSH连接到托管MongoDB安装的计算机
  • 设置从本地端口到远程MongoDB端口的端口转发
  • 远程连接到MongoDB

我的代码如下:

// forwarding ports
private static final String LOCAL_HOST = "localhost";
private static final String REMOTE_HOST = "127.0.0.1";
private static final Integer LOCAL_PORT = 8988;
private static final Integer REMOTE_PORT = 27017; // Default mongodb port

// ssh connection info
private static final String SSH_USER = "<username>";
private static final String SSH_PASSWORD = "<password>";
private static final String SSH_HOST = "<remote host>";
private static final Integer SSH_PORT = 22;

private static Session SSH_SESSION;

public static void main(String[] args) {
try {
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    JSch jsch = new JSch();
    SSH_SESSION = null;
    SSH_SESSION = jsch.getSession(SSH_USER, SSH_HOST, SSH_PORT);
    SSH_SESSION.setPassword(SSH_PASSWORD);
    SSH_SESSION.setConfig(config);
    SSH_SESSION.connect();
    SSH_SESSION.setPortForwardingL(LOCAL_PORT, REMOTE_HOST, REMOTE_PORT);

    MongoClient mongoClient = new MongoClient(LOCAL_HOST, LOCAL_PORT);
    mongoClient.setReadPreference(ReadPreference.nearest());
    MongoCursor<String> dbNames = mongoClient.listDatabaseNames().iterator();
    while (dbNames.hasNext()) {
    System.out.println(dbNames.next());
    }
} catch (Exception e) {
    e.printStackTrace();
} finally {
    SSH_SESSION.delPortForwardingL(LOCAL_PORT);
    SSH_SESSION.disconnect();
}
}

此代码在运行时无效 有效. 连接到SSH服务器可以很好地工作,但是连接到它后面的Mongo数据库不起作用并返回此错误:

This code, when run, doesn't does work. Connecting to the SSH server works just fine, but connecting to the Mongo database behind it doesn't work and returns this error:

INFO: Exception in monitor thread while connecting to server localhost:8988
com.mongodb.MongoSocketReadException: Prematurely reached end of stream
    at com.mongodb.connection.SocketStream.read(SocketStream.java:88)
    at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:491)
    at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:221)
    at com.mongodb.connection.CommandHelper.receiveReply(CommandHelper.java:134)
    at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:121)
    at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:83)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:43)
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
    at java.lang.Thread.run(Unknown Source)

我已经尝试过通过命令行执行以下操作:

I've tried doing this through command line as follows:

$ ssh <user>@<host> -p 22 -X -C
$ <enter requested password>
<user>@<host>$ mongo
<user>@<host>$ MongoDB shell version: 2.6.10
<user>@<host>$ connecting to: test

所以这似乎行得通.对于Java代码(应该做大致相同的事情)为什么不起作用,我感到茫然.

So this seems to work. I'm at a loss as to why the Java code (which should be doing roughly the same thing) doesn't work.

推荐答案

此代码已成功运行,但主要问题是您的mongo数据库已停止.请检查mongo实例是否正在运行.

This code is run successfully, but the main problem is your mongo db is stopped. Please check the instance of the mongo is running or not.

sudo systemctl status mongod

如果它没有运行

sudo systemctl start mongod

这篇关于通过Java中的SSH隧道连接到Mongo数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆