How to run MongoDB as Windows service?

2019-01-03 11:53发布

How to setup MongoDB so it can run as Windows service?

26条回答
Luminary・发光体
2楼-- · 2019-01-03 12:14

Plz try the steps in Mongo DB: 3.6 & Windows 10

  1. mongod --remove
  2. mongod --dbpath=C:/data/db --port 27017 --logpath C:/data/log/log.txt --service
  3. mongod --dbpath=C:/data/db --port 27017 --logpath C:/data/log/log.txt --install
  4. net start MongoDB
查看更多
男人必须洒脱
3楼-- · 2019-01-03 12:15

Unlike other answers this will ..

START THE SERVICE AUTOMATICALLY ON SYSTEM REBOOT / RESTART

MongoDB Install

Windows

(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
查看更多
forever°为你锁心
4楼-- · 2019-01-03 12:18

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 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
查看更多
老娘就宠你
5楼-- · 2019-01-03 12:18

You can use the command below for running mongodb as a windows service

"C:\mongodb\bin\mongod" --bind_ip  yourIPadress  --logpath  "C:\data\dbConf\mongodb.log"  --logappend  --dbpath  "C:\data\db"  --port yourPortNumber --serviceName "YourServiceName" --serviceDisplayName "YourServiceName" --install 

If you use mongodb with default parameters, you can use these values :

  • yourIPadress : 127.0.0.1 or localhost
  • yourPortNumber : 27017 (default port) or dont put --port
  • serviceDisplayName : only if you run more than one service (since mongodb 1.8)

There's more information on this command here

http://www.mongodb.org/display/DOCS/Windows+Service

查看更多
孤傲高冷的网名
6楼-- · 2019-01-03 12:18

Consider using

mongod --install --rest --master
查看更多
贪生不怕死
7楼-- · 2019-01-03 12:20

This was the only thing that worked for me. As everything had to be an absolute path:

C:\Program Files\MongoDB\Server\3.2\bin>mongod --install --dbpath=c:/data/db --logpath=c:/data/logs/log.txt

I also had to run it from admin cmd

查看更多
登录 后发表回答