当我尝试连接到 MongoDB 时,出现此错误:
异常={com.mongodb.MongoSocketException: xxxxxxx},由 {java.net.UnknownHostException: xxxxxxx}} 引起的"
的java代码:
MongoClientURI mangoUri = new MongoClientURI("mongodb://user:pwd@xxxxxxx:27017/?authSource=admin&authMechanism=SCRAM-SHA-1&allowConnectionsWithoutCertificates=true&sslInvalidHostnameAllowed=true");
MongoClientOptions options = MongoClientOptions.builder().sslInvalidHostNameAllowed(true).build();
MongoClient mongoClient = new MongoClient("user:27017",options) ;
mongoDatabase =mongoClient.getDatabase("test");
MongoIterable<String> documents = mongoDatabase.listCollectionNames();
System.out.println(documents.first());
MongoCollection<Document> collection = mongoDatabase.getCollection("event_cidb");
Document searchedfields=collection.find(eq("customer_id","4545454545454")).first();
System.out.println(searchedfields);
mongoClient.close();
但是在工具 compass mongodb 中我可以连接到它: