1. Start MongoDB without authentication

2. Create new role which we will use to login into database

> use admin
 > db.createUser(
 user: "sachin",
 pwd: "sachin",
 roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]

3. Edit mongod.conf  file and enable authentication

under security: tag enable/disable authorization
authorization: enabled/disabled

vi /etc/mongod.conf

authorization: enabled

save it and restart mongod instance

4. Restart Mongo

5. Check now after login it will not allow

6. Pass username and password to authenticate

> use admin
switched to db admin
> db.auth('sachin','sachin')
> show dbs;
admin 0.000GB
local 0.000GB


Leave a Reply