Creating normal users and assigning built-in roles
In this recipe, we will look at how to use built-in roles provided by MongoDB and assign them to users.
Getting ready
You should have a MongoDB instance with authentication enabled and an administrator account created. Refer to the first recipe of this chapter for more details.
How to do it...
- Connect to the mongod instance using the mongo shell and authenticate asÂ
superadmin
:
use admin db.auth('superadmin', 'supasecret')
- Create a new user and assign it a built-in role:
use mydb db.createUser( { user: "mydb_user", pwd: "secret", roles: [{role: "read", db: "mydb"}] } )
- You should see that an output similar to this:
Successfully added user: { "user" : "mydb_user", "roles" : [ { "role" : "read", "db" : "mydb" } ] }
- Connect to the mongod instance and authenticate.
use mydb db.auth('mydb_user', 'secret')
- Execute a
count()
command:
db...