java ssh连接mongodb,通过Java中的SSH隧道连接到Mongo数据库

FIXED (edited code to reflect changes I made)

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

I'm using the Mongo driver 3.0.2 and jcraft (JSch) to create an SSH tunnel.

The idea is that I:

connect to the machine hosting the MongoDB installation through SSH

set up port forwarding from a local port to the remote MongoDB port

connect to MongoDB remotely

My code looks like this:

// 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 = "";

private static final String SSH_PASSWORD = "";

private static final String SSH_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 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();

}

}

This code, when run, doesn't EDIT: 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 @ -p 22 -X -C

$

@$ mongo

@$ MongoDB shell version: 2.6.10

@$ connecting to: test

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.

解决方案

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

if it is not running

sudo systemctl start mongod

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值