com.mongodb.MongoTimeoutException when using MongoClient with list ServerAddress com.mongodb.MongoTimeoutException when using MongoClient with list ServerAddress mongodb mongodb

com.mongodb.MongoTimeoutException when using MongoClient with list ServerAddress


After searching on google. I'm find out that java can't recognize which is the primary database

GWReplication:PRIMARY> cfg=rs.conf();{    "_id" : "GWReplication",    "version" : 1,    "members" : [            {                    "_id" : 0,                    "host" : "G-Server-1:27000"            },            {                    "_id" : 1,                    "host" : "G-Server-1:27001"            },            {                    "_id" : 2,                    "host" : "G-Server-1:27002"            }    ]}

This is default config of my mongodb. And java can't understand G-Server-1 So I fix it by change it to my above ip

GWReplication:PRIMARY> rs.config();{    "_id" : "GWReplication",    "version" : 2,    "members" : [            {                    "_id" : 0,                    "host" : "104.236.106.53:27000"            },            {                    "_id" : 1,                    "host" : "104.236.106.53:27001"            },            {                    "_id" : 2,                    "host" : "104.236.106.53:27002"            }    ]}

Now it work fine. I know that it's a quite idiot way to fix but honestly I don't know how to make java to recognize my domain name or how to config it on ubuntu server(I deploy mongodb on host of digitalocean and this is first time I use ubuntu and self config a server) -_-


Changing your replica set to ip address is actually the wrong way to go about it.

Try this:

  1. Revert back to using the domain names in your replica set configuration.
  2. Update your mongo client configuration with the replica set domain names.

     List<ServerAddress> lstServer = new ArrayList<ServerAddress>(); lstServer.add(new ServerAddress("G-Server-1", 27000)); lstServer.add(new ServerAddress("G-Server-1", 27002)); lstServer.add(new ServerAddress("G-Server-1", 27001)); mongoClient = new MongoClient(lstServer);

That should fix it. Spring Mongo actually uses the host name and port number to determine the primary replica as configured in the replica set.