0

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 } }
PedroG
  • 33
  • 6

2 Answers2

0

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}'`);
   }
}
Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
  • Yeah those commands worked from any VM to another. As for the script I saved it as a .sh and ran it but it threw me some "cons not found" errors, not sure if I'm running it as supposed though. – PedroG Mar 31 '22 at 14:38
  • You need to run it from `mongo` or `mongosh` shell. Connect your mongo shell to `mongos` service. – Wernfried Domscheit Mar 31 '22 at 15:12
  • It throws a "uncaught exception: TypeError: db.runCommand(...).authInfo.authenticatedUsers.shift(...) is undefined" on the "const user = ..." line – PedroG Mar 31 '22 at 17:09
  • You run your cluster without authentication??? Use `let connectionString = \`mongodb://@${uri[1]}/admin?replicaSet=${uri[0]}\`;` – Wernfried Domscheit Mar 31 '22 at 18:42
  • Yeah for now since I'm only testing.. should work on authentication now probably. It ran fine btw, "health: 1" in both config servers and shards/replicas. – PedroG Apr 01 '22 at 13:35
0

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.

PedroG
  • 33
  • 6