How to create user in Shard server

Hi Team,

I was created User on MongoS my shard server after the user name and pwd does not move into shard servers , actually we have 2 shards one mongos and one config server.

please briefly explaining me where i was missed
Why user name does not replicate into shard server what is the process of create user in MongoDB Shard .

MongoDB Enterprise mongos> use admin
switched to db admin
MongoDB Enterprise mongos> db.createUser({user:"test",pwd:"123",roles:[{role:"root",db:"admin"}]})
Successfully added user: {
        "user" : "test",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}
MongoDB Enterprise mongos>

After created User in Mongos and connecting to one of the my shard server same user name but gettinf below error.

C:\mongodb_enterprise\mongodb\bin>mongo --port 27017  -u test  -p 123 --authenticationDatabase admin
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27017/
Implicit session: session { "id" : UUID("05388006-86a3-4d28-b88b-b06599e2c18b") }
MongoDB server version: 4.0.4
2021-07-30T23:11:50.870+0530 E QUERY    [js] Error: Authentication failed. :
DB.prototype._authOrThrow@src/mongo/shell/db.js:1685:20
@(auth):6:1
@(auth):1:2
exception: login failed

C:\mongodb_enterprise\mongodb\bin>

Please notice my current shards above created same username connected as MongoS server

MongoDB Enterprise mongos> sh.status()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("60b4fdbfee39817673277920")
  }
  shards:
        {  "_id" : "rps",  "host" : "rps/localhost:28001,localhost:28002",  "state" : 1 }
        {  "_id" : "rps1",  "host" : "rps1/localhost:27017,localhost:27018",  "state" : 1 }
  active mongoses:
        "4.0.4" : 1
  autosplit:
        Currently enabled: yes

Hi @sree_hari1,

There are two scopes of user authentication:

  • Sharded cluster users that you create via mongos and use for all data access. This user information is saved in the sharded cluster config servers.

  • Shard Local Users that can be used to connect directly to a shard mongod for maintenance operations.

Per the Sharded Cluster Users documentation:

These shard local users are completely independent from the users added to the sharded cluster via mongos . Shard local users are local to the shard and are inaccessible by mongos .

Direct connections to a shard should only be for shard-specific maintenance and configuration. In general, clients should connect to the sharded cluster through the mongos .

Regards,
Stennie