After MongoDB installation by default Authentication was disabled. So we nned to create users with different role permission to enable Authentication . In this topic I am going to explain all steps to add users to enable Authentication in MongoDB.
When you first time create a user in mongodb you need to create a user with previlage to create/delete all other users in all database.
Step1. "admin" is default database in mongoDB, so select "admin" database use admin Step2. create a Super admin user with following role permission db.createUser( { user: "username", pwd: "password" roles: [ { role: "dbOwner", db: "admin" }, { role: "clusterAdmin", db: "admin" }, { role: "userAdminAnyDatabase", db: "admin" }, { role: "dbAdminAnyDatabase", db: "admin" }, { role: "readWriteAnyDatabase", db: "admin" }, { role: "readWrite", db: "admin" } ] } ) Step3. Now create a new database use testdb Step4. create a user with following role permission for "testdb" db.createUser( { user: "username", pwd: "password" roles: [ { role: "dbAdmin", db: "testdb" }, { role: "userAdmin", db: "testdb" }, { role: "readWrite", db: "testdb" }, { role: "readAnyDatabase", db: "admin" }, { role: "readWrite", db: "testdb" } ] } ) Step5. Enable security from mongodb.config /etc/mongodb.conf. Open mongod.conf file and uncomment “#security to security”.It’s normally located in /etc folder.Open the file as an administrator and modifie this file. Next line give two space and add “ authorization: enabled”.This file will look like below: # network interfaces net: port: 27017 bindIp: 172.31.4.59 #processManagement: #processManagement: security: authorization: enabled #operationProfiling: Step6. Restart Mongodb service sudo service mongod restart Step7. Go to mongochef and try to connect your old connection. If your config modification was successfully done your old connection will fail to connect. Now edit your connection setting ,add authentication with newly added user. Connect your connection, You have add authenticated user successfully