'com.mongodb.MongoTimeoutException when using MongoClient with list ServerAddress
I'm trying to deploy a replica database onto one server with different port and connect with it. Everything is ok if I just use single ServerAddress and connect directly to Primary DB like this:
mongoClient =new MongoClient(new ServerAddress("104.236.106.53", 27000));
morphia = new Morphia();
ds = morphia.createDatastore(mongoClient, "morphiaDB");
Everything work fine. But when I'm trying to use List<ServerAddress>
like this:
List<ServerAddress> lstServer = new ArrayList<ServerAddress>();
lstServer.add(new ServerAddress("104.236.106.53", 27000));
lstServer.add(new ServerAddress("104.236.106.53", 27002));
lstServer.add(new ServerAddress("104.236.106.53", 27001));
mongoClient = new MongoClient(lstServer);
morphia = new Morphia();
ds = morphia.createDatastore(mongoClient, "morphiaDB");
It will end up with this error:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting for a server that matches {serverSelectors=[ReadPreferenceServerSelector{readPreference=primary}, LatencyMinimizingServerSelector{acceptableLatencyDifference=15 ms}]}. Client view of cluster state is {type=ReplicaSet, servers=[{address=G-Server-1:27000, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.UnknownHostException: G-Server-1}}, {address=G-Server-1:27001, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.UnknownHostException: G-Server-1}}, {address=G-Server-1:27002, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.UnknownHostException: G-Server-1}}]
at com.mongodb.BaseCluster.getServer(BaseCluster.java:82)
at com.mongodb.DBTCPConnector.getServer(DBTCPConnector.java:654)
at com.mongodb.DBTCPConnector.access$300(DBTCPConnector.java:39)
at com.mongodb.DBTCPConnector$MyPort.getConnection(DBTCPConnector.java:503)
at com.mongodb.DBTCPConnector$MyPort.get(DBTCPConnector.java:451)
at com.mongodb.DBTCPConnector.getPrimaryPort(DBTCPConnector.java:409)
at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:182)
at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:165)
at com.mongodb.DBCollection.insert(DBCollection.java:161)
at com.mongodb.DBCollection.insert(DBCollection.java:107)
at com.mongodb.DBCollection.save(DBCollection.java:965)
at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:949)
at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:1013)
at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:1000)
at com.learn.DAO.AuthorBookDAO.main(AuthorBookDAO.java:18)
Can anybody tell me what this error is or provide me some hint how to solve it?
Solution 1:[1]
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) -_-
Solution 2:[2]
Changing your replica set to ip address is actually the wrong way to go about it.
Try this:
- Revert back to using the domain names in your replica set configuration.
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |