在Java中通过SSH隧道连接到mongo数据库

Dav*_*d V 14 java ssh tunnel mongodb

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

我正在尝试使用java通过ssh隧道连接到mongo数据库.

我正在使用mongo驱动程序3.0.2和jcraft(Jsch)来创建一个ssh隧道.我的想法是:

  • 通过ssh连接到托管mongo db安装的机器
  • 设置从本地端口到远程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();
}
}
Run Code Online (Sandbox Code Playgroud)

此代码在运行时不会 编辑:确实有效.连接到ssh服务器工作正常,但连接到它后面的mongo数据库不起作用并返回此错误:

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)
Run Code Online (Sandbox Code Playgroud)

我已尝试通过命令行执行此操作,如下所示:

$ 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
Run Code Online (Sandbox Code Playgroud)

所以这似乎有效.我不知道为什么java代码(应该做大致相同的事情)不起作用.

Dav*_*d V 0

我设法使其工作(尝试将端口转发到“localhost”而不是“127.0.0.1”,更改它修复了它)编辑:我猜服务器正在专门监听 localhost 而不是 127.0.0.1