How to run MongoDB as Windows service?

2019-01-03 11:53发布

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

26条回答
贪生不怕死
2楼-- · 2019-01-03 12:03

This is what worked for me:

sc.exe create MongoDB binPath= "d:\MongoDB\bin\mongod.exe --service --config=d:\MongoDB\bin\mongod.config" displayname= "MongoDB 2.6 Standard" start= "auto"

escaping the binPath was failing for me as described in the Mongo documentation

Failed:

sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB 2.6 Standard\bin\mongod.exe\" --service --config=\"C:\Program Files\MongoDB 2.6 Standard\mongod.cfg\"" DisplayName= "MongoDB 2.6 Standard" start= "auto"
查看更多
戒情不戒烟
3楼-- · 2019-01-03 12:06

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.

查看更多
倾城 Initia
4楼-- · 2019-01-03 12:06

Working on MongoDB 3.4 [Windows]

  • Create dir C:/mongodb/data
  • Create a file in C:/mongodb/mongodb.config using this configuration:

    storage:
        engine: wiredTiger
        dbPath: "C:/mongodb/data"
        directoryPerDB: true
        journal:
            enabled: true
    systemLog:
        destination: file
        path: "C:/mongodb/data/mongod.log"
        logAppend: true
        timeStampFormat: iso8601-utc
    net:
        bindIp: 127.0.0.1
        port: 27017
        wireObjectCheck : false
    
  • To install MongoDb as a service, run this command in powershell with admin power

    mongod --config="C:\mongodb\mongodb.config" --install --service

  • Open Services.msc and look for MongoDb, then start it

查看更多
仙女界的扛把子
5楼-- · 2019-01-03 12:06

I found that you should pass : dbpath , config and logfile to mongod with the install flag

example :

mongod --dbpath=c:\data\db --config=c:\data\db\config.cfg --logpath=c:\data\db\log.txt --install 

note : I have mongod path in my path variable .

You can control the service with :

net start mongodb
net stop  mongodb 
查看更多
Animai°情兽
6楼-- · 2019-01-03 12:08

If you install MongoDB 2.6.1 or newer using the MSI download from an Administrator Command Prompt, a service definition should automatically be created for you.

The MongoDB documentation also has a tutorial to help you Manually Create a Windows Service definition if needed.

查看更多
唯我独甜
7楼-- · 2019-01-03 12:09
1) echo logpath=F:\mongodb\log\mongo.log > F:\mongodb\mongod.cfg

2) dbpath=F:\mongodb\data\db [add this to the next line in mongod.cfg]

C:\>F:\mongodb\bin\mongod.exe –config F:\mongodb\mongod.cfg –install

Reference

查看更多
登录 后发表回答