UnknownHostException error when connecting to Mongo cluster Java

Today, a timeout error was reported when connecting to the Mongo cluster in Java, but it was able to connect when the local client was connected to a single node. The specific error reported is as follows:

Caused by: com.mongodb.MongoTimeoutException:
Timed out after 60000 ms while waiting for a server that matches
{serverSelectors=[ReadPreferenceServerSelector{readPreference=primaryPreferred}, LatencyMinimizingServerSelector{acceptableLatencyDifference=15 ms}]}.
Client view of cluster state is {type=ReplicaSet, servers=[{address=mongo.50:27017, type=Unknown,state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket},
caused by {java.net.UnknownHostException: mongo.50}},
{address=mongo.66:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket},
caused by {java.net.UnknownHostException: mongo.66}},
{address=mongo.67:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused
by {java.net.UnknownHostException: mongo.67}}]

 

I checked the IP address in the configuration file and there is no problem, but why does the error that mongo.50, mongo.66 and mongo.67 can’t resolve the three addresses appear in the error report? How did these three addresses come from? The Mongo cluster consists of 3 machines, and the last IPs are 50, 66, and 67 respectively. I guess it might be a problem with the cluster configuration.

There is a similar question on StackOverflow: (https://stackoverflow.com/questions/27607773/com-mongodb-mongotimeoutexception-when-using-mongoclient-with-list-serveraddress)

In fact, because of Mongo’s replica set method, the client decides which Mongo server to connect to. Each Mongo service listens to each other. If the main service is found to be down, it will elect a new master. The client will be notified when the new master is confirmed. The domain name is used when creating the replica set, so the primary address obtained by the client is also the domain name.
Therefore, the address configured in the mongo.50 cluster configured by the Java client cannot be resolved.
Then you only need to do one more step below to configure the hosts file of the machine. Corresponding to the corresponding IP and mongo.50 address can be resolved.

Or remove?replicaSet=rs0,The following is a summary of the main node, i.e., only the master node is connected, not the replica.

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *