MongoDB Replication – Could Not Find Host Matching Read Preference {mode: ‘primary’}

mongodbreplicationsharding

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

dev41:27017,dev42:27017,dev193:27017

and configsvr in three machine:

dev41:27019,dev42:27019,dev193:27019

and also a mongos in machine:

dev41:28000

at last I try to add my replica into mongos:

mongo dev41:28000/admin
sh.addShard('rs0/dev41,dev42,dev193')

then I got errors:

{
    "ok" : 0,
    "errmsg" : "could not find host matching read preference { mode: \"primary\" } for set rs0",
    "code" : 133
}

Best Answer

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.