我使用JSch将本地计算机连接到具有mongoDB的服务器。使用JSch成功连接到服务器后,我无法访问MongoDB数据库。 这是代码-
System.out.println("Start...");
// forwarding ports
String LOCAL_HOST = "127.0.0.1";
String REMOTE_HOST = "xx.xx.xx.xx";
Integer LOCAL_PORT = 8988;
Integer REMOTE_PORT = 27017; // Default mongodb port
// ssh connection info
String SSH_USER = "xx";
String SSH_PASSWORD = "";
String SSH_HOST = "xx.xx.xx.xx";
Integer SSH_PORT = 22;
Session SSH_SESSION = null;
try {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Choose your privatekey(ex. ~/.ssh/id_dsa)");
chooser.setFileHidingEnabled(false);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose "+
chooser.getSelectedFile().getAbsolutePath()+".");
jsch.addIdentity(chooser.getSelectedFile().getAbsolutePath()
, "passphrase"
);
}
SSH_SESSION = null;
SSH_SESSION = jsch.getSession(SSH_USER, SSH_HOST, SSH_PORT);
SSH_SESSION.setConfig(config);
SSH_SESSION.connect();
System.out.println("Connection established...");
SSH_SESSION.setPortForwardingL(LOCAL_PORT, REMOTE_HOST, REMOTE_PORT);
System.out.println("Port forwarding done...");
MongoClient mongoClient = new MongoClient(LOCAL_HOST, LOCAL_PORT);
mongoClient.setReadPreference(ReadPreference.nearest());
System.out.println("Mongo client created...");
MongoCursor<String> dbNames = mongoClient.listDatabaseNames().iterator();
System.out.println("Mongo cursor fetched...");
while (dbNames.hasNext()) {
System.out.println(dbNames.next());
}
System.out.println("End...");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
SSH_SESSION.delPortForwardingL(LOCAL_PORT);
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SSH_SESSION.disconnect();
}
输出以及异常:
Start...
You chose C:\XXXXXXX\dsworker_rsa.
Connection established...
Port forwarding done...
Aug 06, 2018 12:37:55 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[127.0.0.1:8988], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Mongo client created...
Aug 06, 2018 12:37:55 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=127.0.0.1:8988, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
Aug 06, 2018 12:38:15 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server 127.0.0.1:8988
com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message
at com.mongodb.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:474)
at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:225)
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)
Caused by: java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at com.mongodb.connection.SocketStream.read(SocketStream.java:85)
at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:491)
at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:221)
... 8 more
代码中是否缺少任何内容?