How can I run MongoDB as a Windows service? How can I run MongoDB as a Windows service? mongodb mongodb

How can I run MongoDB as a Windows service?


After trying for several hours, I finally did it.

Make sure that you added the <MONGODB_PATH>\bin directory to the system variable PATH

First I executed this command:

D:\mongodb\bin>mongod --remove

Then I executed this command after opening command prompt as administrator:

D:\mongodb\bin>mongod --dbpath=D:\mongodb --logpath=D:\mongodb\log.txt --install

After that right there in the command prompt execute:

services.msc

And look for MongoDB service and click start.


NOTE: Make sure to run command prompt as administrator.

If you don't do this, your log file (D:\mongodb\log.txt in the above example) will contain lines like these:

2016-11-11T15:24:54.618-0800 I CONTROL  [main] Trying to install Windows service 'MongoDB'2016-11-11T15:24:54.618-0800 I CONTROL  [main] Error connecting to the Service Control Manager: Access is denied. (5)

and if you try to start the service from a non-admin console, (i.e. net start MongoDB or Start-Service MongoDB in PowerShell), you'll get a response like this:

System error 5 has occurred.Access is denied.

or this:

Start-Service : Service 'MongoDB (MongoDB)' cannot be started due to the following error: Cannot open MongoDB serviceon computer '.'.At line:1 char:1+ Start-Service MongoDB+ ~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service],   ServiceCommandException    + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceComman


I think if you run it with the --install command line switch, it installs it as a Windows Service.

mongod --install

It might be worth reading this thread first though. There seems to be some problems with relative/absolute paths when the relevant registry key gets written.


not only --install,

also need --dbpath and --logpath

and after reboot OS you need to delete "mongod.lock" manually