MongoDB can be downloaded from their official website. You can either download msi file which provides step by step guide to install or choose zip file which doesn’t need any installation.
Installation
If using .msi file, just follow the instructions in the setup window. Optionally you can specify the installation directory. Another way is to use .zip file where you just extract the zip file to any directory as you wish.
In this example I’m choosing “C:\MongoDB\” to install it.
After installation it is recommended to add mongo bin directory(“C:\MongoDB\3.6\bin” directory in my case) to system “path” environment variable. This will allow you to use mongodb commands through DOS command prompt.
You can verify this by executing the command “mongo -version” from a new command prompt.
Configuring the mongodb server
Create following file structure where ever you wish to store the database and logs.
data\db logs
I’m using following file structure:
C:\MongoDB\Replicas\replica-1\data\db C:\MongoDB\Replicas\replica-1\logs
Here I’m using above structure since I’ve plans to have multiple replicas in my system to play with it.
Now use the following command to start the mongodb server.
mongod --dbpath C:\MongoDB\Replicas\replica-1\data\db --logpath C:\MongoDB\Replicas\replica-1\log --port=27017
Here port number is optional. If it is not specified, mongodb server will start with port 27017 which is the default port.
Even though we can specify the dbpath, port number, logpath etc as command line arguments, I always prefer to use mongodb config files. This will provide the luxury of updating it any time easily after setting the mongodb as windows service.
For this you can use .config file or .yml file(supporting since mongodb-2.6).
Examples:
1. .config file
dbpath=C:\MongoDB\Replicas\replica-1\data\db logpath C:\MongoDB\Replicas\replica-1\log port=27017
2. .yml file
systemLog: destination: file path: C:\MongoDB\Replicas\replica-1\log storage: dbpath: C:\MongoDB\Replicas\replica-1\data\db net: bindIp: localhost port: 27017
Setting Mongo as Windows Service
Use below command to create MongoDB windows service if not using a config file.
mongod --dbpath C:\MongoDB\Replicas\replica-1\data\db --logpath C:\MongoDB\Replicas\replica-1\log --port=27017 --install
Use following format if you are using a config file.
mongod --config C:\MongoDB\Replicas\replica-1\config.yml --install
If you want to specify the windows service name use –serviceName and –serviceDisplayName as below. This will be handy when you are trying to have multiple replica running from same system.
mongod --config C:\MongoDB\Replicas\replica-1\config.yml --install --serviceName "Mongo Replica-1" --serviceDisplayName "Mongo Replica-1"