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

102 Views Asked by At

When i try to connect to MongoDB i get this error:

exception={com.mongodb.MongoSocketException: xxxxxxx}, caused by {java.net.UnknownHostException: xxxxxxx}}]"

The java code:

    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();

However in the tool compass mongodb I can connect to it:

enter image description here

2

There are 2 best solutions below

0
Myriam On BEST ANSWER

I had to disable ssl (in java code)

0
kaung htet naing On

for me i remove "replicaSet=rs0&connectTimeoutMS=60000&maxIdleTimeMS=60000&readPreference=primary&" these from my uri connection string