'Mongos instance can't communicate with the database

So I have a sharded cluster with 2 config servers, 2 shards each with 2 replicas and 2 mongos instances, everything running on different VMs.

However, after configuring all of it, I finally tried to interact with the database which is empty with a simple show dbs query from the mongos instance, but it threw me the following error (after thinking for like 1 min):

uncaught exception: Error: listDatabases failed:{
        "ok" : 0,
        "errmsg" : "Could not find host matching read preference { mode: \"primary\" } for set rep",
        "code" : 133,
        "codeName" : "FailedToSatisfyReadPreference",
        "operationTime" : Timestamp(1648722327, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1648722327, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

Everything seems to be well configured and when I do sh.status() from the mongos instance it identifies the shards and replicas as such:

sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("62421dd6b5f9640f309faca0")
  }
  shards:
        {  "_id" : "rep",  "host" : "rep/192.168.86.136:26000,192.168.86.141:26001",  "state" : 1 }
        {  "_id" : "repb",  "host" : "repb/192.168.86.142:26002,192.168.86.143:26003",  "state" : 1 }
  active mongoses:
        "4.4.8" : 2
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  5
        Last reported error:  Empty host component parsing HostAndPort from ""
        Time of Reported error:  Thu Mar 31 2022 11:06:39 GMT+0100 (WEST)
        Migration Results for the last 24 hours:
                No recent migrations
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                rep     919
                                repb    105
                        too many chunks to print, use verbose if you want to force print
        {  "_id" : "testdb",  "primary" : "rep",  "partitioned" : false,  "version" : {  "uuid" : UUID("2e584dcd-25ea-4ba4-805c-b40928e26511"),  "lastMod" : 1 } }


Solution 1:[1]

Maybe a firewall issue.

Every node in your cluster must be able to reach any other node via according port. See Simple HTTP/TCP health check for MongoDB

Try this script to check each member of each replica set:

const MONGO_PASSWROD = '*******'
const AUTH_SOURCE = 'admin'

const user = db.runCommand({ connectionStatus: 1 }).authInfo.authenticatedUsers.shift().user;
const map = db.adminCommand("getShardMap").map;

for (let rs of Object.keys(map)) {
   let uri = map[rs].split("/");
   let connectionString = `mongodb://${user}:${MONGO_PASSWROD}@${uri[1]}/admin?replicaSet=${uri[0]}&authSource=${AUTH_SOURCE}`;
   let replicaSet = Mongo(connectionString).getDB("admin");
   for (let member of replicaSet.adminCommand({ replSetGetStatus: 1 }).members) {
      if (!replicaSet.hello().hosts.includes(member.name)) continue;
      printjsononeline({ replicaSet: rs, host: member.name, stateStr: member.stateStr, health: member.health });

      if (member.health != 1 || !Array("PRIMARY", "SECONDARY").includes(member.stateStr))
         print(`Member state of ${member.name} is '${member.stateStr}'`);
   }
}

Solution 2:[2]

Turns out I configured the replica set wrongly, so all I had to do was recreate the volumes of all VMs and configure it all again from scratch. Now it works as it should.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wernfried Domscheit
Solution 2 PedroG