Setting up MongoDB with Authentication on Windows
Posted By : Akash Sharma | 02-Dec-2013
In this
While demonstrating I will also show steps for
Step 1: Download
Download the latest production release of MongoDB from here
.
Extract the archive to D:\ by
The folder name will be either:
D:\mongodb-win32-i386-[version]
or
D:\mongodb-win32-x86_64-[version]
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
Add D:\mongodb\bin path to PATH environment variable.
Now issue following commands for creating
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:
In this
By default data directory for
NOTE:
To make
Before installing a service with authentication, we have to create a user with
For getting information about all the roles of a user follow this link
The two important roles for a user are userAdmin and
The
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
Run
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:
mongo
>db = db.getSiblingDB('admin')
>db.addUser( { user: "myUsername",pwd: "myPassword",roles: [ "userAdminAnyDatabase" ] } )
>use myDB
switched to db myDB
>db.addUser("myUsername","myPassword")
{
"_id" : ObjectId("51e39ba329e0772d7de869be"),
"user" : "myUsername",
"readOnly" : false,
"pwd" : "60c55b23692da72cf223712b8bfd614f"
}
>
If everything goes well, close both
Step 4: Install
Now we have to start
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
mongo -u myUsername -p myPassword localhost/myDB
For more information on authenticated login follow this link
Thanks
Akash Sharma
About Author
Akash Sharma
Akash is a bright Groovy and Grails developer and have worked on development of various SaaS applications using Grails technologies. Akash loves playing Cricket and Tennis