How to install MongoDB on Windows as a service

I’m showing you a quick procedure to install MongoDB as a service on Windows platforms (I simply gathered the hints explained in the official documentation page and adapted the whole stuff to my specific case).

In my example, I’m installing MongoDB version 2.2.0 on a Windows Server 2003 R2 machine and my goal is to have it available as a service.

The procedure is pretty straightforward: all you need is to setup the path in which MongoDB will physically store the data collections, to provide a logfile for the service we’re going to create and finally to tell the mongod daemon to run as a service.

Here we go (be sure to enclose all the paths in double quotes if they contain spaces):


:: 1. Download the installation package (a .zip archive) and decompress it into folder C:\mongodb-2.2.0\

:: 2. Create the data path folders (in my example, C:\mongodb-2.2.0\data\db):
md C:\mongodb-2.2.0\data
md C:\mongodb-2.2.0\data\db
set datapath = C:\mongodb-2.2.0\data\db

:: 3. Setup mongod configuration file path (my file is C:\mongodb-2.2.0\mongod.cfg):
set configpath = C:\mongodb-2.2.0\mongod.cfg

:: 4. Create the folder that will host the service's log file (my folder is C:\mongodb-2.2.0\log) and write its path into the config file:
md C:\mongodb-2.2.0\log
echo logpath = C:\mongodb-2.2.0\log\mongod.log > C:\mongodb-2.2.0\mongod.cfg

:: 5. install mongod as a service:
C:\mongodb-2.2.0\bin\mongod.exe --config %configpath% --dbpath %dbpath% --install

Now you can start/stop/remove the MongoDB service via the services administration graphical interface, or via the command line using the following commands:


:: Start service
net start MongoDB

:: Stop service
net stop MongoDB

:: Uninstall the service
C:\mongodb-2.2.0\bin\mongod.exe --remove

Wanna test out your installation? Just call the MongoDB Javascript shell:


C:\mongodb-2.2.0\bin\mongo.exe

and if no error message appears – have fun!

Advertisements