Dirk Bergmann

Developer, panographer. Does consultancy work from own company. Lives in Chiang Mai, Thailand.


Chiang Mai

Hua Hin

Thailand Hotels

ListVote - List. Vote. Learn.

Partnerseek - Find Partners

MongoDB Replica Set Nodes as Windows Server Services

The MongoDB replica set tutorial should be easy enough to implement on a Windows (server) machine, but how do we run our replica set members as MongoDB windows services, having them start up automatically or manage them with the Windows SCM (Service Control Manager)?

Suggestions to seed multiple hosts in one command didn't work for me and several other options did not bear much fruit.  Especially running mongod —install with the —serverName option did not work out as the new service instance will be named like the first, leading to a conflict and failure of the command.

So the best path might be to forego the 10gen suggested Windows service installation and use the sc.exe utility instead to create the service instance like so:

sc.exe create "Mongo DB 1" binPath= "c:\mongodb\bin\mongod.exe --service --dbpath c:\data\r0 --logpath c:\log\mongodb.log --replSet my_replica_set --port 27017 --rest"

sc.exe create "Mongo DB 2" binPath= "c:\mongodb\bin\mongod.exe --service --dbpath c:\data\r1 --logpath c:\log\mongodb.log --replSet my_replica_set --port 27018 --rest"

sc.exe create "Mongo DB 3" binPath= "c:\mongodb\bin\mongod.exe --service --dbpath c:\data\r2 --logpath c:\log\mongodb.log --replSet my_replica_set --port 27019 --rest"

Be sure to check adjusts your paths and urls, duh. To edit these commands you can check and edit the HKLM\System\CurrentControlSet\Services<Mongo Service Name> key, especially its ImagePath parameter. The —rest parameter in the commands above lets you check your replica set configuration by navigating to http://localhost:28017/_replSet

This technique will also be handy when implemeting sharding. Things may get better with 1.7+ versions of MongoDB.

Hope it helps.


dirk: Thanks Kristina, I wanted to implement the tutorial on a single machine and tried different paths, especially hacking the ImagePath parameter of the service definition in the Windows Registry, and the sc.exe was the best work around the limitations of Windows. I'm a MongoDB newbie and understood that replica sets should be run as the "new default" installation even on a single machine. Is this not correct?

Kristina: In "real life," you're probably not going to have a whole replica set (or sharded cluster) on a single machine. I don't think the way you install services will change much in the future, but your method looks like a good way to hack around the limitations for testing/development.