Com.mongod.MongoTimeoutException when using MongoClient with ServerAddress list

I am trying to deploy a replica database to one server with a different port and connect to it. It's okay if I just use a single server address and connect directly to the primary database like this:

mongoClient =new MongoClient(new ServerAddress("104.236.106.53", 27000));
morphia = new Morphia();
ds = morphia.createDatastore(mongoClient, "morphiaDB");

      

Everything works fine. But when I try 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");

      

As a result, 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 someone tell me what the error is, or give me some hint on how to resolve it?

+4


source to share


2 answers


After searching on google. I found out that Java cannot 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 my default mongodb config. And Java can't understand G-Server-1 So I'll fix it by changing 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"
            }
    ]
}

      

It works fine now. I know this is a pretty idiotic way to fix it, but honestly, I don't know how to get Java to recognize my domain name or how to set it up on an ubuntu server (I deploy mongodb on a digitalocean host and this is the first time I use ubuntu and configure the server yourself) -_-

0


source


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

Try the following:



  • Revert to using domain names in your replica configuration settings.
  • Update the mongo client configuration with the domain names set on the replica.

     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);
    
          

This should fix it. Spring Mongo actually uses the hostname and port number to determine the primary replica configured on the replica set.

0


source







All Articles