Mongodb as windows service

mongodbwindows

I am using mongo db with replica sets. I am starting the mongodb using command prompt.

Python Script:

os.system('start mongod --configsvr --dbpath C:\mongoConfigData/configdb1 --replSet conf --port 27019')
os.system('start mongod --configsvr --dbpath C:\mongoConfigData/configdb2 --replSet conf --port 27020')
os.system('start mongod --configsvr --dbpath C:\mongoConfigData/configdb3 --replSet conf --port 27021')

os.system('start mongos --configdb "conf/localhost:27019,localhost:27020,localhost:27021" --logpath E:\mongoSLogs\mongoLog.log  --port 26060')

os.system('start mongod --replSet conf --shardsvr   --dbpath E:\data --storageEngine wiredTiger --wiredTigerJournalCompressor zlib --wiredTigerCollectionBlockCompressor zlib --wiredTigerCacheSizeGB 7 --port 27010 ')

os.system('start mongod --replSet conf --shardsvr  --dbpath F:\data --storageEngine wiredTiger --wiredTigerJournalCompressor zlib --wiredTigerCollectionBlockCompressor zlib --wiredTigerCacheSizeGB 7 --port 27011 ')

os.system('start mongod --replSet conf --shardsvr  --dbpath G:\data --storageEngine wiredTiger --wiredTigerJournalCompressor zlib --wiredTigerCollectionBlockCompressor zlib --wiredTigerCacheSizeGB 7 --port 27012 ')

I want to start the mongodb as a windows service.
Let me know how to convert the above in to windows service

Best Answer

I would recommend a Configuration File instead a bunch of command line options.

There have a closer look at Windows Service Options

Once you created the configuration file you can install the service like this:

mongod.exe --config c:\MongoDB\config\mongo.cfg --install

Note, the default service name is MongoDB. Unless you specify processManagement.windowsService.serviceName you have to use os.system('net start MongoDB') in order to start the service.

However, if you like to run more than one MongoDB service, the service names have to be distinct.

Running start mongod ... starts the service in foreground and the terminal remains open.