I have set up a MongoDB Replica Set and this is the structure:

  • One primary node.
  • One secondary node.
  • One arbiter node.

I have included the output of rs.status() at the end of this post.

The problem I am having now is that when I have my application connected using the MongoDB connection string: mongodb://192.168.17.52:27017,192.168.17.223:27017,192.168.17.49:27017/?replicaSet=rs-nnt&readPreference=primary it will work if the primary node is specified first in the comma separated list and in this instance 192.168.17.52:27017. If I was to stop the primary DB service, I would expect the application to use the secondary node but for some reason, it will not work and after some troubleshooting, I came to a conclusion that it will only take the first host specified in the connection string.

Please let me know if more information is needed, appreciate any assistance!

rs.status():

rs-nnt:PRIMARY> rs.status()
{
        "set" : "rs-nnt",
        "date" : ISODate("2018-12-04T14:09:38.835Z"),
        "myState" : 1,
        "term" : NumberLong(8),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1543932576, 1),
                        "t" : NumberLong(8)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1543932576, 1),
                        "t" : NumberLong(8)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1543932576, 1),
                        "t" : NumberLong(8)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1543932576, 1),
                        "t" : NumberLong(8)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "192.168.17.52:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 10565,
                        "optime" : {
                                "ts" : Timestamp(1543932576, 1),
                                "t" : NumberLong(8)
                        },
                        "optimeDate" : ISODate("2018-12-04T14:09:36Z"),
                        "electionTime" : Timestamp(1543923115, 1),
                        "electionDate" : ISODate("2018-12-04T11:31:55Z"),
                        "configVersion" : 3,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "192.168.17.223:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 8427,
                        "optime" : {
                                "ts" : Timestamp(1543932576, 1),
                                "t" : NumberLong(8)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1543932576, 1),
                                "t" : NumberLong(8)
                        },
                        "optimeDate" : ISODate("2018-12-04T14:09:36Z"),
                        "optimeDurableDate" : ISODate("2018-12-04T14:09:36Z"),
                        "lastHeartbeat" : ISODate("2018-12-04T14:09:38.167Z"),
                        "lastHeartbeatRecv" : ISODate("2018-12-04T14:09:38.736Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "192.168.17.52:27017",
                        "configVersion" : 3
                },
                {
                        "_id" : 2,
                        "name" : "192.168.17.49:27017",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 10518,
                        "lastHeartbeat" : ISODate("2018-12-04T14:09:38.167Z"),
                        "lastHeartbeatRecv" : ISODate("2018-12-04T14:09:36.770Z"),
                        "pingMs" : NumberLong(0),
                        "configVersion" : 3
                }
        ],
        "ok" : 1,
        "operationTime" : Timestamp(1543932576, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1543932576, 1),
                "signature" : {
                        "hash" : BinData(0,"Ydlo0SyUlFlnZUfimAmGvrowuRs="),
                        "keyId" : NumberLong("6630712219715764225")
                }
        }
}

Extra information: The firewall has been turned off on all hosts. The hostname and IP address has been added to the hosts file on all servers.

  • What specific driver or tool (and version) are you using to connect to your MongoDB deployment? Is there any specific error message returned when the current primary is not first in your connection string seedlist? The seedlist of hosts provided in the initial connection string should be used to discover the current primary, so as long as at least one of those hosts is reachable the deployment configuration should be discoverable. The order of hosts should not be overly significant, although many drivers will try connecting in the order specified. – Stennie Dec 6 at 1:01
  • Thank you for the feedback, I managed to solve the problem! I had to add the following parameter in my applications configuration file as well as the mongo DB connection string. "replicaSet" – Hasan Dec 7 at 14:17
  • Was your connection string different from that included in the original question (which does appear to have the replicaSet parameter? – Stennie Dec 7 at 21:03
  • Yes, I also had to edit the connection string and excluded the replicaset parameters at the end. So the new connection string looks like this: mongodb://192.168.17.52:27017,192.168.17.223:27017,192.168.17.49:27017 – Hasan Dec 8 at 13:23
  • To make your question a more useful reference to others,it would be very helpful to edit the question to reflect the original problematic connection string and post an answer on how you addressed the issue. The current story is a tad confusing as it looks like the connection string in the original description would have worked. However, I gather you were actually using a connection string without the replicaSet parameter (which most drivers require to perform replica set discovery). The example in your recent comment does not include replicaSet, so doesn't seem to be the desired format. – Stennie Dec 9 at 0:30

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.