Installing MongoDB as a windows service

After using MongoDB for quite a while, I realised that I had been repeating the starting up of the MongoDB database daemon whenever I reboot my PC. Manually starting the MongoDB database daemon via my command prompt whenever I want to work on my projects is inefficient.

Hence making MongoDB run as a windows service automatically when my computer starts up is one way to avoid repeating myself. In this post, I document how I install my MongoDB database as a windows service.

Getting the MongoDB package

  • I chose the MongoDB package meant for Windows 64-bit from MongoDB's official download page.
  • I unzipped the package to my D: drive. By doing so, I had the folder: D:\mongodb-<version-identifier>.
  • I renamed the folder as mongodb to keep the folder naming simpler.

Configuring the database

MongoDB could be installed with default configurations. However, some of the default configurations are not to my liking. I would prefer my MongoDB database server to listen on port 31234 instead of the default 27017. I also would want my database to use the D: drive instead of the C: drive for data storage.

I could configure my MongoDB database daemon via command-line parameters. However, to cater for future configuration changes, I had instead created a mongod.conf text file in the D:\mongodb folder. And within mongod.conf file, I defined the configuration settings that I want my MongoDB database daemon to take:

# Folder for data storage 
dbpath = D:\mongodb\data 

# Indicate that log records are appended to existing log file
logappend = true 

# Log file that will contain the log records
logpath = D:\mongodb\log\mongod.log

# Database server listens on port 31234 
port = 31234 

# The log records should be detailed
verbose = true 

To conclude the configuration phase, I created the D:\mongodb\data and D:\mongodb\log folders as MongoDB will fail to install as a windows service if the specified folders does not exist.

The magical command to install MongoDB as a windows service

Within the D:\mongodb, I looked for the bin\mongod.exe binary, which is the MongoDB database process. Apart from serving as the MongoDB database process, mongod.exe also came with the feature for installing itself as a windows service.

With that, I navigate to the bin folder with my command prompt and execute the following command-line string:

mongod.exe --install -f d:\mongodb\mongod.conf 

The process ended with the message: "all output going to: D:\mongodb\log\mongod.log".

And inside D:\mongodb\log\mongod.log, I saw the following lines:

Sat Jul 21 17:24:20 BackgroundJob starting: DataFileSync
Creating service MongoDB.
Service creation successful.
Service can be started from the command line via 'net start "MongoDB"'.
Sat Jul 21 17:24:21 dbexit: 
Sat Jul 21 17:24:21 shutdown: going to close listening sockets...
Sat Jul 21 17:24:21 shutdown: going to flush diaglog...
Sat Jul 21 17:24:21 shutdown: going to close sockets...
Sat Jul 21 17:24:21 shutdown: waiting for fs preallocator...
Sat Jul 21 17:24:21 shutdown: lock for final commit...
Sat Jul 21 17:24:21 shutdown: final commit...
Sat Jul 21 17:24:21 shutdown: closing all files...
Sat Jul 21 17:24:21 closeAllFiles() finished
Sat Jul 21 17:24:21 dbexit: really exiting now

With that, the MongoDB windows service was installed successfully and I was able to see it with services.msc.

MongoDB installed as a windows service in services.msc

Some other posts which you may interest you

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.