Mongodb – Unable to perform queries through mongos

mongodbmongodb-3.6

I have setup a minimal replicated configuration with mongodb 3.6.3,it seems to be everything fine but when I connect to the mongos (which as far as I understand acts like a kind of load balancer in front of sharded clusters) and try to perform a query it raises this error: Cannot accept sharding commands if not started with –shardsvr (code: 193, codename: NoShardingEnabled)

This is how I've setup the servers:

3 servers: mongoserver1, mongoserver2 and mongoserver3

Launch the config server:

mongod --configsvr --replSet data --bind_ip localhost,${HOSTNAME}\
    --port 25000 --logpath /var/lib/mongodb/shardedcluster/log.cfg\
    --logappend --dbpath /var/lib/mongodb/shardedcluster/cfg --fork

Log in the config server and initiate the replica:

rs.initiate(
  {
    _id: "data",
    configsvr: true,
    members: [
      { _id : 0, host : "mongoserver1:25000" },
      { _id : 1, host : "mongoserver2:25000" },
      { _id : 2, host : "mongoserver3:25000" }
    ]
  }
)

Launch the sharded replica:

mongod --shardsvr  --replSet data --bind_ip localhost,${HOSTNAME}\
    --port 26000 --logpath /var/lib/mongodb/shardedcluster/data.log\
    --logappend --dbpath /var/lib/mongodb/shardedcluster/data --fork

Log in the sharde replica and initiate the replica:

rs.initiate(
  {
    _id : "data",
    members: [
      { _id : 0, host : "mongoserver1:26000" },
      { _id : 1, host : "mongoserver2:26000" },
      { _id : 2, host : "mongoserver3:26000" }
    ]
  }
)

And finally launched the mongos:

mongos --configdb data/mongoserver1:25000,mongoserver2:25000,mongoserver3:25000\
   --bind_ip localhost,${HOSTNAME}\
   --logpath /var/lib/mongodb/shardedcluster/mongos.log --fork

And inside the mongos:

sh.addShard("data/mongoserver1:26000,mongoserver2:26000,mongoserver3:26000")
sh.enableSharding("cmdb")

After adding some data on the cmdb database through the mongod running or port 26000 I've checked after stopping primary node I can continue working with date on the new primary node but I'm not able to do it connecting to mongos even it seems the cmdb database is replicated as shown by sh.status():

databases:
    {  "_id" : "cmdb",  "primary" : "data",  "partitioned" : true }

Best Answer

Well, finally I've solved the problem. It was my fault when configuring everything to give the same name data to the replicas of the config server and the sharded server.

I replaced data by rs0 when launching the config servers:

mongod --configsvr --replSet rs0 --bind_ip localhost,${HOSTNAME}\
    --port 25000 --logpath /var/lib/mongodb/shardedcluster/log.cfg\
    --logappend --dbpath /var/lib/mongodb/shardedcluster/cfg --fork

Of course in the first rs.initiate changed _id to rs0 and finally when launching the mongos replaced data with rs0:

mongos --configdb data/mongoserver1:25000,mongoserver2:25000,mongoserver3:25000 \
    --bind_ip localhost,${HOSTNAME} \
    --logpath /var/lib/mongodb/shardedcluster/mongos.log --fork

Now it works like charm :)