Setup Mongodb as windows service step by step

Setup Mongodb as windows service step by step

  1. Download Mongodb from https://www.mongodb.org/downloads
  2. Install the setup.
  3. 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.

  1. As per above path, create a folder named logs in E:\MongoDBDemo.
  1. Create a folder named data in E:\MongoDBDemo.
  1. 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

  1. 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.

image001

Else simply go to run window and type firewall.cpl.

image002

Click Allow a promgram or feature through Windows Firewall in left pane.

image003

Click ‘Allow another program’ button.

image004

Click Browse button and browse to C:\Program Files\MongoDB\Server\3.0\bin. Select mongod.exe and click open.

image005

Run services.msc from admin command prompt and you can see myMongodbService running.