How can I set up MongoDB so it can run 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.
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 service
on computer '.'.
At line:1 char:1
+ Start-Service MongoDB
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service],
ServiceCommandException
+ FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceComman