连接到 MongoDB URI 时出错,即使我可以使用 compassdb 或 robot t3 工具

Error when connecting to MongoDB URI even though i can do with compassdb or robot t3 tools

当我尝试连接到 MongoDB 时出现此错误:

exception={com.mongodb.MongoSocketException: xxxxxxx}, caused by {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();

但是在工具罗盘中mongodb我可以连接到它:

我必须禁用 ssl(在 java 代码中)