Cluster description not yet available. Waiting for 30000 ms before timing out com.mongodb.diagnostics.logging.JULLogger log

Viewed 12070

I am trying to connect MongoDB Atlas with Java. This is my code

MongoClientSettings.Builder mongoClientSettingsBuilder = MongoClientSettings.builder()
        .credential(mongoCredential)
        .applyToClusterSettings(builder -> builder.hosts(new ServerAddress("dev-20937-liwy3.gcp.mongodb.net", 27017)));
MongoClientSettings settings = mongoClientSettingsBuilder.build();      
MongoClient mongoClient = MongoClients.create(settings);

By doing so I am getting the following error

INFO: Cluster description not yet available. Waiting for 30000 ms before timing out
Mar 26, 2020 11:33:41 AM com.mongodb.diagnostics.logging.JULLogger log
Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=dev-20937-liwy3.gcp.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: dev-20937-liwy3.gcp.mongodb.net}, caused by {java.net.UnknownHostException: dev-20937-liwy3.gcp.mongodb.net}}]

I do not know the cause of the error. By my guess I have given my host name wrong.

Can someone help me out with this

1 Answers

go to your MongoDB Atlas -> NetworkAccess-> Edit-> and add Current IP address it works!!

// Create a Mongo client 
  MongoClient mongo = new MongoClient( "localhost" , 27017 ); 

  // Creating Credentials 
  MongoCredential credential; 
  credential = MongoCredential.createCredential("User", "DB", 
     "pass".toCharArray()); 
  System.out.println("Connected to the database successfully");  
Related