Setup Mongodb as windows service step by step
- Download Mongodb from https://www.mongodb.org/downloads
- Install the setup.
- Create a directory for example E:\MongoDBDemo. And create a file here named as mongod.cfg. Provide following content in mongod.cfg.
systemLog:
destination: file
path: E:\MongoDBDemo\logs\mongod.log
storage:
dbPath: E:\MongoDBDemo\data
net:
bindIp: 127.0.0.1,xxx.xxx.xxx.xxx
port: 27021
Provide your IP instead of xxx.xxx.xxx.xxx. So multiple IP can be given comma separated.
Update the path and dbPath as per your log file path and db data folder path respectively. Click here for more configurations options.
- As per above path, create a folder named logs in E:\MongoDBDemo.
- Create a folder named data in E:\MongoDBDemo.
- Install windows service: If you find your mongodb.exe here – C:\Program Files\MongoDB\Server\3.0\bin\mongod.exe or please use the directory where you have installed the mongodb.
Start windows cmd with Run as Administrator and execute below command.
sc.exe create myMongodbService binPath= "C:\Program Files\MongoDB\Server\3.0\bin\mongod.exe --service --config=\"E:\MongoDBDemo\mongod.cfg\"" DisplayName= "myMongodbService" start= "auto" | net start myMongodbService
In future if you wish to delete this service then you can run following command
sc stop myMongodbService | sc delete myMongodbService
- Allow mongod in firewall as below. If you try to run mongod –dbpath <db path> from cmd, then you will get below dialog prompting to allow mongod against firewall.
Else simply go to run window and type firewall.cpl.
Click Allow a promgram or feature through Windows Firewall in left pane.
Click ‘Allow another program’ button.
Click Browse button and browse to C:\Program Files\MongoDB\Server\3.0\bin. Select mongod.exe and click open.
Run services.msc from admin command prompt and you can see myMongodbService running.