Create db user in mongodb

February 08, 201969
Share Tweet Pin it

To create DB admin in mongodb follow the below command

1. Go to the admin database

use admin

2. Now we will have to use the createUser function to create db user and assign to the required db

db.createUser(
  {
    user: "myUserAdmin",
    pwd: "abc123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
)

The funnctions and keywords in the above query are case sensitive. Please follow the syntax and you can change the user : and pwd : as per your requirement.

If you would like to create user for particular table you could use the following code

use <your-db-name>

db.createUser({user: "bikram", pwd: "Bikram@123", roles: [{role: "clusterAdmin",db: "admin"},{role: "readAnyDatabase", db: "admin"},"readWrite"]})

Hope this helps you. Thanks for reading this.