{
"set" : "rs0",
"date" : ISODate("2018-02-28T15:58:37.228Z"),
"myState" : 7,
"term" : NumberLong(13),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1519833700, 2),
"t" : NumberLong(13)
},
"appliedOpTime" : {
"ts" : Timestamp(1519833700, 2),
"t" : NumberLong(13)
},
"durableOpTime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
}
},
"members" : [
{
"_id" : 1,
"name" : "mongo2:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1106202,
"optime" : {
"ts" : Timestamp(1519833700, 2),
"t" : NumberLong(13)
},
"optimeDurable" : {
"ts" : Timestamp(1519833700, 2),
"t" : NumberLong(13)
},
"optimeDate" : ISODate("2018-02-28T16:01:40Z"),
"optimeDurableDate" : ISODate("2018-02-28T16:01:40Z"),
"lastHeartbeat" : ISODate("2018-02-28T15:58:34.259Z"),
"lastHeartbeatRecv" : ISODate("2018-02-28T15:58:35.285Z"),
"pingMs" : NumberLong(8),
"syncingTo" : "mongo1:27017",
"configVersion" : 23
},
{
"_id" : 5,
"name" : "arbiter:27017",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 1114291,
"configVersion" : 23,
"self" : true
},
{
"_id" : 6,
"name" : "mongo1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 21350,
"optime" : {
"ts" : Timestamp(1519833700, 2),
"t" : NumberLong(13)
},
"optimeDurable" : {
"ts" : Timestamp(1519833700, 2),
"t" : NumberLong(13)
},
"optimeDate" : ISODate("2018-02-28T16:01:40Z"),
"optimeDurableDate" : ISODate("2018-02-28T16:01:40Z"),
"lastHeartbeat" : ISODate("2018-02-28T15:58:34.305Z"),
"lastHeartbeatRecv" : ISODate("2018-02-28T15:58:36.223Z"),
"pingMs" : NumberLong(7),
"electionTime" : Timestamp(1518724645, 1),
"electionDate" : ISODate("2018-02-15T19:57:25Z"),
"configVersion" : 23
}
],
"ok" : 1
}
mongodb://myuser:mypass@mongo1:27017,mongo2:27017/mydb?replicaSet=rs0
ServerSelectionTimeoutError: No replica set members match selector "Primary()"
NetworkTimeout: mongo1:27017: timed out
Hi
ServerSelectionTimeoutError: No replica set members match selector “Primary()”
This error saying that the client (Pymongo, in this case) is trying to connect to the primary node of the replica set, but couldn’t.
A replica set can have no primary if the primary node cannot see the majority of the replica set nodes. This is possible if:
Thus for example if you have a 3-node replica set and the two secondaries are offline, the primary node will immediately step down (becoming a secondary) and no writes are allowed in the set. This is to prevent a situation where a replica set was affected by a network partition, and thus could potentially have two primary nodes that cannot see each other (a “split brain” scenario). If a split brain occur and writes are occurring to two different primaries, it is very difficult to tell which primary has the most recent and the correct data.
What happened in your case could be such a scenario, where there is a temporary network partition and the primary node steps down. The intermittent nature of network partitions could be causing your intermittent connection issues.
Please see Automatic Failover for more details.
Best regards
Kevin
Hi David
If your replica set is having connection issues between the nodes, you should see a message in the mongod logs that is similar to this:
2018-03-06T16:48:56.503+1100 I REPL_HB [replexec-1] Error in heartbeat (requestId: 68) to localhost:27018, response status: HostUnreachable: Connection refused
2018-03-06T16:48:56.958+1100 I REPL [replexec-2] Not starting an election, since we are not electable due to: Not standing for election because I cannot see a majority (mask 0x1)
Note that the messages above were printed by MongoDB 3.6.3. It could be slightly different on older MongoDB versions.
If you’re certain that your replica set does not have an election or similar message during the connection timeout issue you’re seeing, then there could be intermittent network issues between the replica set and the client application.
Best regards
Kevin
Hi David
If your app can connect to the replica set and the replica set status is as you posted, I don’t believe that a MongoDB configuration issue is the source of the connection problems. If there is a replica set configuration issue, typically your replica set 1) would not be able to have a primary and a secondary, and 2) apps cannot connect to it all the time, not just some of the time. I suspect it’s a networking issue (reliability, config, etc.) instead.
Having said that, if you’re still having doubt that there could be a configuration issue, please post:
rs.conf()mongod running on it, is it cloud hosted or physical machines, etc.mongod log shows during this connection issueBest regards
Kevin
{
"_id" : "rs0",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "mongo1:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : 60000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("59b0707a92de04d12999374f")
}
}