use admin
db.createUser({user:"adminUser",pwd:"adminPassword", roles:["userAdminAnyDatabase"]})
or
db.createUser({
user:"adminUser",
pwd:"adminPassword",
passwordDigestor:"server",
roles:["dbOwner"]
})
Add the following to /etc/mongod.conf
or /usr/local/etc/mongod.conf
security:
authorization: enabled
Restart mongodb server
To apply user with read write access to a database:
Switch to database you want to add user
use kunal
Then apply role
db.createUser( { user: "kunal", pwd: "kunal", roles:[ { role: "readWrite", db:"kunal" }] })