Running MongoDB as a Windows Service

Following on from my opening “Getting started with MongoDB and .NET” post, one of the next logical steps was to get mongodb running as a Windows service.

From a command prompt, in the mongodb bin directory run:

mongod --install --serviceName "MongoDB" --dbpath C:\mongodb\data\db --logpath C:\mongodb\logs\mongolog.txt --logappend  

Obviously you can tweak the arguments for your environment - I opted to use a different dbpath to the default \data\db

Starting the service is then just a case of running:

net start mongodb

or starting via Control Panel - Administrative Tools - Services. You can of course set it to start up automatically on Windows startup.

Word of caution

Make sure the arguments you give to mongod are valid. I initially forgot to specify the –dbpath argument and because I wasn’t using the default location which didn’t exist on my machine, the service got stuck in a constant start/stop cycle when I tried to start it. The mongo log file was growing and growing each time. In the end, renaming the logpath directory on my machine was enough to kill the service completely and let me correct the mistake.

There is an open issue logged on for this: Error during service startup causes server to start over and over and over

See also