Setting up MongoDB with Authentication on Windows

Posted By Akash Sharma | 02-Dec-2013

In this blog I am going to share the details of how to authenticate mongodb while logging to database in windows environment.

While demonstrating I will also show steps for mongodb setup that will be saving data in custom directory (e.g D:\ drive ) other than C:\data folder.If you already have setup of mongodb in custom directory structure then you can skip step 1 and step 2.


Step 1: Download mongodb

Download the latest production release of MongoDB from here




Note : Always download the correct version of MongoDB for your Windows system. The 64-bit versions of MongoDB will not work with 32-bit Windows.

Extract the archive to D:\ by right clicking on the archive and selecting Extract All and browsing to D:\ .

The folder name will be either:




In both examples, replace [version] with the version of MongoDB downloaded.


Step 2: Set up the Environment

Open a command prompt and issue the following command

move D:\mongodb-win32-* D:\mongodb


This will move the mongodb folder for bin directory in D:\ drive.

Add D:\mongodb\bin path to PATH environment variable.


Now issue following commands for creating necessary directory structure for mongodb:

md D:\mongodb\data
md D:\mongodb\data\db
md D:\mongodb\log


Issue following command for making configuration file:

echo logpath=D:\mongodb\log\mongo.log > D:\mongodb\mongod.cfg


Step 3: Mongodb custom configuration

In this step we will try to setup mongodb by our custom configurations.

By default data directory for mongodb is c:\data .Now we will be saving this data to d:\mongodb\data folder.



To make mongodb as authenticated login, we have to pass --auth parameter while installing its service.

Before installing a service with authentication, we have to create a user with required role that can easily login in authenticated development environment and do required operations.

For getting information about all the roles of a user follow this link

The two important roles for a user are userAdmin and userAdminAnyDatabase .

The userAdmin role is a database-specific privilege, and only grants a user the ability to administer users on a single database.

userAdminAnyDatabase provides users with the same access to user administration operations as userAdmin, except it applies to all logical databases in the MongoDB environment.


In this blog I have taken

database name = myDB

username = myUsername

password = myPassword


To add a user with a role of userAdminAnyDatabase , we will start mongodb instance as command tool.

Run following command with Administrator privilege:

D:\mongodb\bin\mongod.exe --journal --config D:\mongodb\mongod.cfg --dbpath D:\mongodb\data


Open another instance of command prompt and hit following commands:

>db = db.getSiblingDB('admin')
>db.addUser( { user: "myUsername",pwd: "myPassword",roles: [ "userAdminAnyDatabase" ] } )
>use myDB
switched to db myDB
       "_id" : ObjectId("51e39ba329e0772d7de869be"),
       "user" : "myUsername",
       "readOnly" : false,
       "pwd" : "60c55b23692da72cf223712b8bfd614f"


If everything goes well, close both instance of command prompt.


Step 4: Install mongodb service

Now we have to start mongodb service.For this we have to install mongodb service with --auth option.

Run following commands in cmd prompt with Administrator privileges:

D:\mongodb\bin\mongod.exe --journal --config D:\mongodb\mongod.cfg --dbpath D:\mongodb\data --auth --install
net start MongoDB


Close this cmd prompt and open a new one.Type following command to login in database:

mongo -u myUsername -p myPassword localhost/myDB


For more information on authenticated login follow this link



Akash Sharma

Request for Proposal

Recaptcha is required.

Sending message..