Running MongoDB as a network service means that your service will have permission to access the network with the same credentials as the computer you are using. Running MongoDB locally will run the service without network connectivity.(Refer Source here)
Starting in version 4.0, you can install and configure MongoDB as a Windows Service during installation. The MongoDB service starts upon successful installation.
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
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
Unlike other answers this will ..
START THE SERVICE AUTOMATICALLY ON SYSTEM REBOOT / RESTART
(1) Install MongoDB
(2) Add bin to path
(3) Create c:\data\db
(4) Create c:\data\log
(5) Create c:\data\mongod.cfg with contents ..
systemLog:
destination: file
path: c:\data\log\mongod.log
storage:
dbPath: c:\data\db
(6) To create service that will auto start on reboot .. RUN AS ADMIN ..
sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe\" --service --config=\"C:\data\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"
(7) Start the service .. RUN AS ADMIN ..
net start MongoDB
IMPORTANT: Even if this says 'The MongoDB service was started successfully' it can fail
To double check open Control Panel > Services, ensure the status of the MongoDB service is 'Running'
If not, check your log file at C:\data\log\mongod.log for the reason for failure and fix it
(Do not start MongoDB via Control Panel > Services, use .. net start MongoDB)
(8) Finally, restart your machine with MongoDB running and it will still be running on restart
If you ever want to kill it ..
net stop MongoDB
sc.exe delete MongoDB
The below steps apply to Windows.
Run below in an administrative cmd
mongod --remove
This will remove the existing MongoDB service (if any).
mongod --dbpath "C:\data\db" --logpath "C:\Program Files\MongoDB\Server\3.4\bin\mongod.log" --install --serviceName "MongoDB"
Make sure that C:\data\db
folder exists
Open services with:
services.msc
Find MongoDB -> Right click -> Start
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With