mongodb could not find host matching read preference { mode: \"primary\" } for set?

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.

Resolution

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

Cheers,