Facing issue in adding shard to mongos

Facing the error of “Could not find host matching read preference { mode: “primary” } for set rs0”.
Here I am not able to add the shard in mongos, even if the cluster is up and running.
Please find the output required of rs.status.
Clusteres here are aws EC2 instances.

rs0:PRIMARY> rs.status() { “set” : “rs0”, “date” : ISODate(“2021-10-26T07:09:23.722Z”), “myState” : 1, “term” : NumberLong(9), “syncingTo” : “”, “syncSourceHost” : “”, “syncSourceId” : -1, “heartbeatIntervalMillis” : NumberLong(2000), “optimes” : { “lastCommittedOpTime” : { “ts” : Timestamp(1635232156, 1), “t” : NumberLong(9) }, “readConcernMajorityOpTime” : { “ts” : Timestamp(1635232156, 1), “t” : NumberLong(9) }, “appliedOpTime” : { “ts” : Timestamp(1635232156, 1), “t” : NumberLong(9) }, “durableOpTime” : { “ts” : Timestamp(1635232156, 1), “t” : NumberLong(9) } }, “lastStableCheckpointTimestamp” : Timestamp(1635232146, 1), “electionCandidateMetrics” : { “lastElectionReason” : “priorityTakeover”, “lastElectionDate” : ISODate(“2021-10-25T12:52:13.234Z”), “electionTerm” : NumberLong(9), “lastCommittedOpTimeAtElection” : { “ts” : Timestamp(1635166330, 1), “t” : NumberLong(8) }, “lastSeenOpTimeAtElection” : { “ts” : Timestamp(1635166330, 1), “t” : NumberLong(8) }, “numVotesNeeded” : 2, “priorityAtElection” : 10, “electionTimeoutMillis” : NumberLong(10000), “priorPrimaryMemberId” : 1, “numCatchUpOps” : NumberLong(0), “newTermStartDate” : ISODate(“2021-10-25T12:52:13.322Z”), “wMajorityWriteAvailabilityDate” : ISODate(“2021-10-25T12:52:15.264Z”) }, “members” : [ { “_id” : 0, “name” : “mongodb01:27017”, “health” : 1, “state” : 1, “stateStr” : “PRIMARY”, “uptime” : 65843, “optime” : { “ts” : Timestamp(1635232156, 1), “t” : NumberLong(9) }, “optimeDate” : ISODate(“2021-10-26T07:09:16Z”), “syncingTo” : “”, “syncSourceHost” : “”, “syncSourceId” : -1, “infoMessage” : “”, “electionTime” : Timestamp(1635166333, 1), “electionDate” : ISODate(“2021-10-25T12:52:13Z”), “configVersion” : 1, “self” : true, “lastHeartbeatMessage” : “” }, { “_id” : 1, “name” : “mongodb01:27018”, “health” : 1, “state” : 2, “stateStr” : “SECONDARY”, “uptime” : 65841, “optime” : { “ts” : Timestamp(1635232156, 1), “t” : NumberLong(9) }, “optimeDurable” : { “ts” : Timestamp(1635232156, 1), “t” : NumberLong(9) }, “optimeDate” : ISODate(“2021-10-26T07:09:16Z”), “optimeDurableDate” : ISODate(“2021-10-26T07:09:16Z”), “lastHeartbeat” : ISODate(“2021-10-26T07:09:23.332Z”), “lastHeartbeatRecv” : ISODate(“2021-10-26T07:09:21.896Z”), “pingMs” : NumberLong(0), “lastHeartbeatMessage” : “”, “syncingTo” : “mongodb01:27017”, “syncSourceHost” : “mongodb01:27017”, “syncSourceId” : 0, “infoMessage” : “”, “configVersion” : 1 }, { “_id” : 2, “name” : “mongodb01:27019”, “health” : 1, “state” : 2, “stateStr” : “SECONDARY”, “uptime” : 65841, “optime” : { “ts” : Timestamp(1635232156, 1), “t” : NumberLong(9) }, “optimeDurable” : { “ts” : Timestamp(1635232156, 1), “t” : NumberLong(9) }, “optimeDate” : ISODate(“2021-10-26T07:09:16Z”), “optimeDurableDate” : ISODate(“2021-10-26T07:09:16Z”), “lastHeartbeat” : ISODate(“2021-10-26T07:09:21.735Z”), “lastHeartbeatRecv” : ISODate(“2021-10-26T07:09:22.571Z”), “pingMs” : NumberLong(0), “lastHeartbeatMessage” : “”, “syncingTo” : “mongodb01:27018”, “syncSourceHost” : “mongodb01:27018”, “syncSourceId” : 1, “infoMessage” : “”, “configVersion” : 1 } ], “ok” : 1, “operationTime” : Timestamp(1635232156, 1), “$clusterTime” : { “clusterTime” : Timestamp(1635232156, 1), “signature” : { “hash” : BinData(0,“AAAAAAAAAAAAAAAAAAAAAAAAAAA=”), “keyId” : NumberLong(0) } } }

command to add the cluster is:

sh.addShard( “rs0/mongodb01:27017”)
“ok” : 0, “errmsg” : “Could not find host matching read preference { mode: “primary” } for set rs0”, “code” : 133, “codeName” : “FailedToSatisfyReadPreference”, “operationTime” : Timestamp(1635166470, 1), “$clusterTime” : { “clusterTime” : Timestamp(1635166470, 1), “signature” : { “hash” : BinData(0,“AAAAAAAAAAAAAAAAAAAAAAAAAAA=”), “keyId” : NumberLong(0) } }

Note: All authentication option is commented so the keyfile mismatch issue is also not the case.

Can Anyone please help me out as I have checked all possible solutions for this one

I have faced this issue before and it was due to a Firewall issue blocking communication between the mongos and the shard servers. A simple test you can do is from your mongos servers run “telnet hostname mongod port” which based on your logs would probably look like telnet mongodb01 27017. You can also verify you can ping on the host name to make sure there isn’t a DNS issue between your AWS instances.

1 Like

Hi tapioca,
Thankyou so much for the solution. In my case hostname mapping was not done due to which I was not able to ping the server from mongos.
Now I am able to add the shard successfully.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.