I am deploy a monodb sharding, I have deploy a replica set in three machines:


and configsvr in three machine:


and also a mongos in machine:


at last I try to add my replica into mongos:

mongo dev41:28000/admin

then I got errors:

    "ok" : 0,
    "errmsg" : "could not find host matching read preference { mode: \"primary\" } for set rs0",
    "code" : 133
  • Please note that using the same three servers for both a shard server replica set and config server is not a recommended production configuration. During operation, there will be extensive resource contention between multiple mongod processes within each machine, which may result in sub-optimal performance or worse.
    – kevinadi
    Commented Nov 24, 2016 at 22:09

11 Answers 11


I faced the same issue then looked into Mongos log file and it was saying

can't authenticate to hostname as internal user, error: Authentication failed.

Please see here: internal user authentication and happens with SecKey file and auth- Enabled.


I opened mongod.conf file and

security: authorization: enabled    
keyFile: /var/run/mongodb/secKey.key

was missing so I added that and restart the mongod. Then I created shard again from Mongos and it is working now.


I had this same issue, and found out that I simply had the name of the replica set wrong in the addShard command.


I had the same issue, but my problem was that before calling sh.addShard() I needed to login to the new shard server (not mongos), and call the following command:

rs.initiate({ _id : "REPLICA_SET_NAME", members: [ { _id : 0, host : "HOSTNAME:27017" } ] })

After that, I was able to call sh.addShard() without problems.

Note that this works without security: authorization: enabled in my mongo.conf


  • i did ``` rs.initiate( { _id: "rs1", configsvr: true, members: [ { _id : 0, host : "mongo-ubuntu16-001:27019" }, { _id : 1, host : "mongo-ubuntu16-002:27019" }, { _id : 2, host : "mongo-ubuntu16-003:27019" } ] } ) ``` and then mongos --configdb rs1/mongo-ubuntu16-001:27019,mongo-ubuntu16-002:27019 --bind_ip localhost, still i am getting "code" : 133 error while adding shard sh.addShard() please help
    – maswadkar
    Commented May 15, 2018 at 21:44

I had a same issue. Problem in my configuration was that hosts where I set config servers to run did not have in /etc/hosts each shard member that I was supposing to add as a new shard. Problem fixed when I added missing shard members in /etc/hosts.

  • Good one, it fixed it for me - Adding the shards to the hosts files of the CONFIG servers - Commented Dec 18, 2018 at 21:57

I had the same symptom, however, my issue was the keyfiles were not consistent bewteen mongod and config nodes.


This adds to @Miroslav's answer except that the hostname found in /etc/hostname had to match the hostname found in /etc/hosts (dont forget to reboot when changed).

I was using a common /etc/hosts file for all my servers but wasn't updating the /etc/hostname file to match.



This error will also occur if the shard host that is being specified in addShard() command is not reachable. Exit out of mongos and try to telnet it for e.g.

telnet dev42 27017 

If telnet did not work then that needs to be fixed first.


I had what appears to me to be the same problem.

In my case, the issue was that 2 of my shard servers went down. I simply started them up again and it took a while for the commands to propagate (I did a remove and some docs were still showing up in a collection), but after a minute or so everything was correct.


Check if mongod service running on Arbiter node, this solved my problem

  1. Need to be sure that your Replicaset name is the correct.
  2. Ensure to at least add the ip or hostname of the master in the replicaset.
  3. Ensure to add the correct port of the master node in the replicaset.
  4. Check that you are using the correct keyfile defined in mongos and rs.

After that you just need to use:


and it's all, mongos will take all the members in the rs you just have added. Good luck


I accidently assumed that shards running on 27017, but they actually run on 27018 by default, so make sure to specify port after host name, for example:

> sh.addShard(mongo-shard3/shard3-svr1.mongo:27018)

and not sh.addShard(mongo-shard3/shard3-svr1.mongo)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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