Skip to main content

ENABLING USER AUTHENTICATION IN MONGODB


         
                                  In my previous article i have explained installation of mongodb in Centos.Follow the guidelines in http://www.goserverspace.com/2017/09/mongodb-is-nosql-database-which.html
In this article Iam explaining about enabling user authentication in mongodb.we will enable authentication for users to prevent that another user without sufficient privileges is able to see the data on the database.

On our CentOS 7 server, MongoDB is running under systemd with an init script in the '/etc/init.d/' dirctory. We will edit that script to force the mongodb service to run with the '--auth' option.

Go to the '/etc/init.d/' directory and edit the "mongod" file:

#cd /etc/init.d/
#vim mongod

On line 15 you will find the "OPTION" variable, there we will add the "mongod" option.

OPTIONS=" --auth -f $CONFIGFILE"

Save the file.

Reload the systemd service and restart MongoDB.

#systemctl daemon-reload
#systemctl restart mongod


Next, we have to test the configuration by logging into the mongo shell and switch to the admin database, then try to see the admin users

#mongo

use admin
show users


You will see an error about the unauthorized execution of the command in the database admin. Now we need to use the command 'db.auth()' for the authentication.

db.auth('admin', 'admin123')

Now you can see the users with their roles and the privileges.