我无法连接到AWS托管的mongo db。我能够ssh成功,但无法与mongo db连接。有人可以指导我谢谢吗。
JSch jsch =新的JSch();
Session session = null;
String username = "name";
String host = "ec2.amazonaws.com";
int port = 22;
String privateKeyPath = "./src/";
jsch.addIdentity(privateKeyPath);
session = jsch.getSession(username, host, port);
session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
System.out.println("session connected.....");
System.setProperty("jdk.tls.trustNameService","true");
String connectionString = "mongodb://{userName}:{dbpassword}@dbcluster-***************.docdb.amazonaws.com:27018/?&retryWrites=false";
MongoClientURI clientURI = new MongoClientURI(connectionString);
MongoClient mongoClient = new MongoClient(clientURI);
MongoDatabase testDB = mongoClient.getDatabase("database-name");
MongoCollection<Document> numbersCollection = testDB.getCollection("collection-name");