【问题标题】:Connecting to Mongo database through SSH tunnel in Java在 Java 中通过 SSH 隧道连接到 Mongo 数据库
【发布时间】:2015-09-18 14:29:34
【问题描述】:

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

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

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

  • 通过 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 sshSession;

public static void main(String[] args) {
try {
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    JSch jsch = new JSch();
    sshSession = null;
    sshSession = jsch.getSession(SSH_USER, SSH_HOST, SSH_PORT);
    sshSession.setPassword(SSH_PASSWORD);
    sshSession.setConfig(config);
    sshSession.connect();
    sshSession.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 {
    sshSession.delPortForwardingL(LOCAL_PORT);
    sshSession.disconnect();
}
}

这段代码在运行时, EDIT: does 工作。 连接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)

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

$ 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 代码(应该做大致相同的事情)不起作用。

【问题讨论】:

  • mongo 是否在 SSH 服务器主机上运行?您能否在 SSH 服务器主机上运行“netstat -an | grep 27071”并编辑您的问题以包含输出?我想看看 mongo 是如何监听连接的。
  • 我设法让它工作(试图将端口转发到“localhost”而不是“127.0.0.1”,更改它修复它)编辑:我猜服务器专门在 localhost 而不是 127.0 上监听.0.1,所以我认为这也回答了您的问题 Kenster。
  • 您通常不应将所有大写字母用于非常量变量。 SSH_SESSION 不是一个常数,你改变它。不过很高兴你让它工作了
  • @DavidV 您应该将问题恢复到以前的状态,并将解决方案作为答案发布。
  • @DavidV 回答你自己的问题并接受它,这样这个问题就不会出现在我未回答的搜索中:)

标签: java mongodb ssh jsch tunnel


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    sudo systemctl status mongod
    

    如果它没有运行

    sudo systemctl start mongod
    

    【讨论】:

      猜你喜欢
      • 2014-07-20
      • 1970-01-01
      • 2013-04-05
      • 2014-09-17
      • 2016-04-30
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      相关资源
      最近更新 更多