user2777473
user2777473

Reputation: 3826

MongoDB URL without replicaSet optional parameter

I have a mongoDB cluster

   server1:27017
   server2:27017
   server3:27017

For historical reason, IT team could not provide the replicaSet name for this cluster.

My question is: without knowing the replicaSet name, is the following mongoDB url legal and will missing the optional replicaSet optional parameter cause any possible problems in future?

mongodb://username:password@server1:27017,server2:27017,server3:27017

I am using Java to setup MongoDB connection using the following

String MONGO_REPLICA_SET = "mongodb://username:password@server1:27017,server2:27017,server3:27017";
MongoClientURI mongoClientURI = new MongoClientURI(MONGODB_REPLICA_SET);
mongoClient = new MongoClient(mongoClientURI);

Upvotes: 2

Views: 4090

Answers (2)

Wan B.
Wan B.

Reputation: 18845

To clarify, although it may be functional to connect to the replica set it would be preferable to specify the replicaSet option.

Depending on the MongoDB Drivers that you're using it may behaves slightly differently. For example quoting the Server Discovery and Monitoring Spec for Initial Topology Type:

In the Java driver a single seed means Single, but a list containing one seed means Unknown, so it can transition to replica-set monitoring if the seed is discovered to be a replica set member. In contrast, PyMongo requires a non-null setName in order to begin replica-set monitoring, regardless of the number of seeds.

There are variations, and it's best to check whether the connection can still handle topology discovery and failover.

For historical reason, IT team could not provide the replicaSet name for this cluster.

If you have access to the admin database, you could execute rs.status() on mongo shell to find out the name of the replica set. See also replSetGetStatus for more information.

Upvotes: 2

Vince Bowdren
Vince Bowdren

Reputation: 9208

It should be possible to find out the name of the replica set, to avoid this worry. Open a connection to any one of your nodes (e.g. direct to server1:27017), and run rs.status(); that will tell you the name of your replica set as well as lots of other useful data such as the complete set of configured nodes and their individual statuses.

In this example of the output, "rsInternalTest" is the replica set name:

{
    "set" : "rsInternalTest",
    "date" : ISODate("2018-05-01T11:38:32.608Z"),
    "myState" : 1,
    "term" : NumberLong(123),
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        ...
    },
    "members" : [ 
        {
            "_id" : 1,
            "name" : "server1:27017",
            "health" : 1.0,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 1652592,
            "optime" : {
                "ts" : Timestamp(1525174711, 1),
                "t" : NumberLong(123)
            },
            "optimeDate" : ISODate("2018-05-01T11:38:31.000Z"),
            "electionTime" : Timestamp(1524371004, 1),
            "electionDate" : ISODate("2018-04-22T04:23:24.000Z"),
            "configVersion" : 26140,
            "self" : true
        }
        ...
    ],
    "ok" : 1.0
}

Note that you will need the login of a high-level user account, otherwise you won't have permission to run rs.status().

Upvotes: 1

Related Questions