Information Security, Web, Networks and Systems

Thursday, May 19, 2016

Setting up MongoDB Client Authentication in Ubuntu

5:29 AM Posted by Deepal Jayasekara , , No comments

This article is a slight variant of the official MongoDB guide for enabling client authentication focused more on setting up client authentication in ubuntu. If you are using any other operating system, please follow the official guide instead.

MongoDB by default does not enable client authentication when you install it via apt-get. To enable client authentication, follow the given steps.

Open /etc/mongod.conf file in your favorite editor.

Add the following two lines to the file if it does not exist
If above lines are there in the file, make sure you have authorization value as disabled. File will look similar to follows:
Then restart MongoDB by running:

sudo service mongod restart

By adding above changes to the file, we make sure we can easily enable authorization in MongoDB by simply changing the value for authorization from disabled to enabled.

Then open a separate terminal and connect to MongoDB without authentication.

mongo --port 27017

This will summon a mongo shell and run following commands to add authentication to your database. Replace mydatabase with your database name which you want to add authentication to, and use your own username and password. The following example was shamelessly copied from the official guide.
use mydatabase
db.createUser(
  {
    user: "myUserAdmin",
    pwd: "abc123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)
Role can be any of the roles of the given specification by mongodb, or any user created role.

Then open /etc/mongod.conf file again and enable client authentication by changing authorization:disabled to authorization:enabled.

Then restart MongoDB with authentication enabled.

sudo service mongod restart

Then you should be able to connect to your database only by providing your username and password given.

Go and check whether authentication works.
I hope this guide will help who is trying to setup mongodb on ubuntu and want to run it using sudo service mongod... 

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.