How to run MongoDB as Windows Service

This Article is outdated!
Please refer to this latest guide to install MongoDB as Windows Service.

A guide to show you how to installed MongoDB on Windows.

1. mongod –help

Get to know all the Windows service related commands by typing “mongod --help“.

C:\MongoDB\bin>mongod --help
 
Windows Service Control Manager options:
  --install                install mongodb service
  --remove              remove mongodb service
  --reinstall             reinstall mongodb service (equivilant of mongod
                             --remove followed by mongod --install)
  --serviceName arg           windows service name
  --serviceDisplayName arg windows service display name
  --serviceDescription arg    windows service description
  --serviceUser arg              user name service executes as
  --servicePassword arg       password used to authenticate serviceUser

Two --install and --remove arguments are what you need.

2. Install as Windows Service

To install as a Windows service, issue “mongod --install“, for example :

#> mongod --dbpath "c:\mymongodb" --logpath "c:\mymongodb\logs.txt" --install --serviceName "MongoDB"
all output going to: c:\mymongodb\logs.txt
Creating service MongoDB.
Service creation successful.
Service can be started from the command line via 'net start "MongoDB"'.

It means, install a MongoDB, which point to “c:\mymongodb” data directory, log output to “c:\mymongodb\logs.txt“, and a Windows service named “MongoDB“.

Figure : MongoDB is installed as Windows Service

mongodb as windows service

3. Uninstall It

To uninstall above installed MongoDB service, issue “mongod --remove“, along with the installed service name.

#> mongod --remove --serviceName "MongoDB"
Deleting service MongoDB.
Service deleted successfully.
Fri Apr 29 18:39:06 dbexit:
Fri Apr 29 18:39:06 shutdown: going to close listening sockets...
Fri Apr 29 18:39:06 shutdown: going to flush diaglog...
Fri Apr 29 18:39:06 shutdown: going to close sockets...
Fri Apr 29 18:39:06 shutdown: waiting for fs preallocator...
Fri Apr 29 18:39:06 shutdown: closing all files...
Fri Apr 29 18:39:06 closeAllFiles() finished
Fri Apr 29 18:39:06 dbexit: really exiting now

Done.

Reference

  1. MongoDB as Windows Service
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: Java MongoDB Tutorial()

  • http://adg-pftwwr-41-9-1.herokuapp.com/ Douglas Allen

    Hi. After making some blunders here I figured out that I needed to give the full path for the daemon.

    C:\mongodb\bin\mongod --dbpath "c:\data\db" --logpath "c:\data\logs.txt" --install --serviceName "MongoDB"

    Hope this might help someone not go through the trial and error I did.
    Thanks!

    • http://www.mkyong.com mkyong

      Thanks for your input.

      It’s better to defined the MongoDB configuration values in a config file, and start it like this :

      mongod --config /config/path/mongo.cfg

      Refer to the latest guide to install MongoDB as Windows service.

  • Pingback: How to install MongoDB on Windows()