Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB atlas, connection not working

Tags:

java

mongodb

I just created a free MongoDB atlas cluster instance. But somehow I am failing to connect it from my application as well as from the MongoDB Compas.

I get below error when I try to run my application.

Caused by: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=experimental-1-epdri.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: experimental-1-epdri.mongodb.net}, caused by {java.net.UnknownHostException: experimental-1-epdri.mongodb.net}}]
    at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:377) ~[mongodb-driver-core-3.4.2.jar:na]
    at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:104) ~[mongodb-driver-core-3.4.2.jar:na]
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75) ~[mongodb-driver-core-3.4.2.jar:na]
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71) ~[mongodb-driver-core-3.4.2.jar:na]
    at com.mongodb.binding.ClusterBinding.getWriteConnectionSource(ClusterBinding.java:68) ~[mongodb-driver-core-3.4.2.jar:na]
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:411) ~[mongodb-driver-core-3.4.2.jar:na]
    at com.mongodb.operation.CreateIndexesOperation.execute(CreateIndexesOperation.java:144) ~[mongodb-driver-core-3.4.2.jar:na]
    at com.mongodb.operation.CreateIndexesOperation.execute(CreateIndexesOperation.java:71) ~[mongodb-driver-core-3.4.2.jar:na]
    at com.mongodb.Mongo.execute(Mongo.java:845) ~[mongodb-driver-3.4.2.jar:na]
    at com.mongodb.Mongo$2.execute(Mongo.java:828) ~[mongodb-driver-3.4.2.jar:na]
    at com.mongodb.MongoCollectionImpl.createIndexes(MongoCollectionImpl.java:491) ~[mongodb-driver-3.4.2.jar:na]
    at com.mongodb.MongoCollectionImpl.createIndex(MongoCollectionImpl.java:458) ~[mongodb-driver-3.4.2.jar:na]
    at org.axonframework.mongo.eventsourcing.eventstore.AbstractMongoEventStorageStrategy.ensureIndexes(AbstractMongoEventStorageStrategy.java:201) ~[axon-mongo-3.0.5.jar:3.0.5]
    at org.axonframework.mongo.eventsourcing.eventstore.MongoEventStorageEngine.ensureIndexes(MongoEventStorageEngine.java:123) ~[axon-mongo-3.0.5.jar:3.0.5]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    ... 76 common frames omitted


Process finished with exit code 1

On Compas it simply says MongoDB instance isn't running at this place. I have checked my cluster and I see below.

Seems like it is up..

But still I can't connect to mongo cluster. Further more I tried with `Mongo CLI as well where I found error as shown below. Error while connecting using CLI.

Below is the connection string that I get from the MongoDB atlas page.

mongodb+srv://admin_eventdb:<PASSWORD>@experimental-1-epdri.mongodb.net/test?retryWrites=true

Please help solve this.

like image 850
Bilbo Baggins Avatar asked Sep 20 '25 11:09

Bilbo Baggins


2 Answers

Please try with:

Hostname: experimental-1-epdri.mongodb.net
SRV Record: checked
Authentication: Username / Password
Username: admin_eventdb
Password: <PASSWORD>
Authentication Database: admin
Replica Set Name:
Read Preference: Primary
SSL: System CA / Atlas Deployment
SSL Tunnel: None
like image 59
Carlo Espino Avatar answered Sep 22 '25 07:09

Carlo Espino


I had the same issue. Via connection string using Java Driver 3.4 it would be:

mongodb://user:<PASSWORD>@cluster0-shard-00-00-ox90k.mongodb.net:27017,cluster0-shard-00-01-ox90k.mongodb.net:27017,cluster0-shard-00-02-ox90k.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true

![enter image description here

Instead of <PASSWORD> I wrote own password. admin1is my admin-user. enter image description here


Updated: if you want to use drivers 3.7+, you need to write instead of format connection (and to avoid my issues above)

MongoClientURI uri = new MongoClientURI("mongodb+srv://admin:[email protected]/test?retryWrites=true");
MongoClient mongoClient = new MongoClient(uri);

another variant using MongoClients.create() (as of the 3.7 release),:

   MongoClient mongoClient = MongoClients.create("mongodb+srv://admin:[email protected]/test?retryWrites=true");

Note: the password need to write not like mongodb://user:<mypassword>@..., just in format mongodb://user:mypassword@... without braces <>.

like image 24
invzbl3 Avatar answered Sep 22 '25 07:09

invzbl3