After creating shards in replica set and also setting up config servers, when I try to add a collection to my shards then I get below error :

"ok" : 0.0,
"errmsg" : "ensureIndex failed to create index on primary shard: Cannot accept sharding commands if not started with --shardsvr"

I checked it and have started my shard server with --shardsvr. Still I face the same issue.

Current MongoDB Version : 3.4.6 (In which I am trying to create shards)

  • 1
    Can you post the exact command you are running? – SqlWorldWide Aug 22 '17 at 11:37
  • if mongod says that you have not started with --shardsvr command, you haven't!! Check mongodb.log file, there is (after start) listed all parameters what mongod used when it was started. – JJussi Aug 22 '17 at 17:06

I check on same version on windows server with mongodb 3.4.6. Please find below scripts.

1) run shard servers

"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --shardsvr --replSet rs0 --dbpath "C:\Mongodb_Databases\MongoDB_Shard\WT_mongod1" --logpath "C:\Mongodb_Databases\MongoDB_Shard\WT_mongod1\log.int1" --port 27000 --logappend "C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --shardsvr --replSet rs0 --dbpath "C:\Mongodb_Databases\MongoDB_Shard\WT_mongod2" --logpath "C:\Mongodb_Databases\MongoDB_Shard\WT_mongod2\log.int2" --port 27001 --logappend "C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --shardsvr --replSet rs0 --dbpath "C:\Mongodb_Databases\MongoDB_Shard\WT_mongod3" --logpath "C:\Mongodb_Databases\MongoDB_Shard\WT_mongod3\log.int3" --port 27002 --logappend "C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --shardsvr --replSet rs0 --dbpath "C:\Mongodb_Databases\MongoDB_Shard\WT_mongod4" --logpath "C:\Mongodb_Databases\MongoDB_Shard\WT_mongod4\log.int4" --port 27003 --logappend

2) config servers

"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --configsvr --replSet rsconfig --dbpath "C:\Mongodb_Databases\MongoDB_Shard\WT_Shardcfg0" --port 26050 --logpath "C:\Mongodb_Databases\MongoDB_Shard\WT_Shardcfg0\log.cfg00" --logappend "C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --configsvr --replSet rsconfig --dbpath "C:\Mongodb_Databases\MongoDB_Shard\WT_Shardcfg1" --port 26051 --logpath "C:\Mongodb_Databases\MongoDB_Shard\WT_Shardcfg0\log.cfg01" --logappend "C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --configsvr --replSet rsconfig --dbpath "C:\Mongodb_Databases\MongoDB_Shard\WT_Shardcfg2" --port 26052 --logpath "C:\Mongodb_Databases\MongoDB_Shard\WT_Shardcfg0\log.cfg02" --logappend

3) mongos mongo router

"C:\Program Files\MongoDB\Server\3.4\bin\mongos.exe" --configdb rsconfig/BSS-BC4-Benchmark2:26050,BSS-BC4-Benchmark2:26051,BSS-BC4-Benchmark2:26052 --logappend --logpath "C:\Mongodb_Databases\MongoDB_Shard\WT_mongos\log.mongos0" --bind_ip,

4) Connect any shard instance in replica and initiate replica set using rs.initiate()

rs.initiate( { _id : "rs0", members: [ { _id : 0, host : "Mongo-Test-PC:27000" }, { _id : 1, host : "Mongo-Test-PC:27001" }, { _id : 2, host : "Mongo-Test-PC:27002" }, { _id : 3, host : "Mongo-Test-PC:27003" } ] } );

5) Connect any config server and initiate replica set for config server using rs.initiate() - name must be different from shard replica

rs.initiate(); rs.status(); cfg = rs.conf(); cfg.members[0].priority = 3; rs.reconfig(cfg); rs.add({ host:"Mongo-Test-PC:26051", priority: 2}); rs.add({ host:"Mongo-Test-PC:26052", priority: 1}); rs.status();

6) Connect mongos and ser read pref and add shard.

db.getMongo().getReadPref(); db.getMongo().setReadPref('secondary'); db.getMongo().setReadPref('primaryPreferred'); db.getMongo().getReadPref();



create database shardtest sh.enableSharding('shardtest');

use shardtest


Hope this will help you.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.