FIXED (edited code to reflect changes I made)

我正在try 使用Java通过SSH tunel 连接到Mongo数据库.

我正在使用Mongo驱动程序3.0.2和jcraft(JSch)创建一个SSH tunel .

  • 通过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();
}
}

此代码在运行时,不起作用连接到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代码(应该做大致相同的事情)不起作用.

推荐答案

我设法让它工作(try 将端口转发到"localhost"而不是"127.0.0.1",并对其进行了修改)编辑:我猜服务器是专门在localhost而不是127.0.0.1上监听的

Mongodb相关问答推荐

如何从MongoDB集合中获取第一个和最后一个元素?

如何填充Mongoose中的嵌套引用

使用 multer 在我的 MERN 前端显示 MongoDB 图像的正确语法是什么?

通过insertId MongoDB获取文档

使用golang的MongoDB错误无法访问服务器

Mongoose 更新不同类型的记录

使用 Node.js 通过 SSL 连接到 MongoDB

增加嵌套对象中的值?

使用绝对类型在 Typescript 中编写 Mongoose 的类型化模型和模式的类和接口

删除一对一和一对多引用 - Mongoose

MongoDB - 如果新值更大,则更新字段

使用 Flask-pymongo 扩展通过 _id 在 MongoDB 中搜索文档

MongoDB 的 BinData(0, "e8MEnzZoFyMmD7WSHdNrFJyEk8M=") 中的0是什么意思?

使用 ObjectId.GenerateNewId() 还是离开 MongoDB 创建一个?

使用 Node.js 和 mongodb 处理超时

Spring Mongodb @DBREF

MEAN 堆栈文件上传

Cannot connect to MongoDB errno:61

在 MongoDB 上分片 GridFS

mongoose 使用 $cond 中的 $exists 聚合