如何设置MongoDB,使其可以作为Windows服务运行?


当前回答

以下是安装MongoDB as Windows Service的步骤:

Create a log directory, e.g. C:\MongoDB\log Create a db directory, e.g. C:\MongoDB\db Prepare a configuration file with following lines dbpath=C:\MongoDB\db logpath=C:\MongoDB\log Place the configuration file with name mongod.cfg in folder "C:\MongoDB\" Following command will install the Windows Service on your sc.exe create MongoDB binPath= "\"C:\MongoDB\Server\3.4\bin\mongod.exe\" --service --config=\"C:\MongoDB\mongod.cfg\" DisplayName= "MongoDB 3.4" start= "auto" Once you run this command, you will get the [SC] CreateService SUCCESS Run following command on Command Prompt net start MongoDB

其他回答

以下步骤适用于Windows操作系统。

在下面的管理cmd中运行

mongod --remove

这将删除现有的MongoDB服务(如果有的话)。

mongod --dbpath "C:\data\db" --logpath "C:\Program Files\MongoDB\Server\3.4\bin\mongod.log" --install --serviceName "MongoDB"

确认C:\data\db文件夹存在

使用以下工具开放服务:

services.msc

Find MongoDB ->右键单击->开始

这对我来说很管用:

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

逃脱binPath是失败的我在Mongo文档中描述

失败:

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"

推荐的mongod——install方法会导致错误:

2015-12-03T18:18:28.896+0100 I CONTROL  --install has to be used with a log file for server output

在C:\mongodb中安装mongodb后,您需要简单地添加日志路径:

mongod --install --logpath C:\mongodb\logs\mongo.log

日志文件的路径必须存在,并且必须是Windows的绝对路径。然后你通过输入启动MongoDB服务:

net start MongoDB

在我的例子中,我在mongo .exe旁边创建了mongo .cfg,其中包含以下内容。

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: D:\apps\MongoDB\Server\4.0\data
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path:  D:\apps\MongoDB\Server\4.0\log\mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0


#processManagement:

#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

然后运行这两个命令中的一个来创建服务。

D:\apps\MongoDB\Server\4.0\bin>mongod --config D:\apps\MongoDB\Server\4.0\bin\mongod.cfg --install

D:\apps\MongoDB\Server\4.0\bin>net stop mongodb
The MongoDB service is stopping.
The MongoDB service was stopped successfully.


D:\apps\MongoDB\Server\4.0\bin>mongod --remove
2019-04-10T09:39:29.305+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-04-10T09:39:29.309+0800 I CONTROL  [main] Trying to remove Windows service 'MongoDB'
2019-04-10T09:39:29.310+0800 I CONTROL  [main] Service 'MongoDB' removed

D:\apps\MongoDB\Server\4.0\bin>
D:\apps\MongoDB\Server\4.0\bin>sc.exe create MongoDB binPath= "\"D:\apps\MongoDB\Server\4.0\bin\mongod.exe\" --service --config=\"D:\apps\MongoDB\Server\4.0\bin\mongod.cfg\""
[SC] CreateService SUCCESS

D:\apps\MongoDB\Server\4.0\bin>net start mongodb
The MongoDB service is starting..
The MongoDB service was started successfully.

D:\apps\MongoDB\Server\4.0\bin>

以下是不正确的,注意转义引号是必需的。

D:\apps\MongoDB\Server\4.0\bin>sc.exe create MongoDB binPath= "D:\apps\MongoDB\Server\4.0\bin\mongod --config D:\apps\MongoDB\Server\4.0\bin\mongod.cfg"
[SC] CreateService SUCCESS

D:\apps\MongoDB\Server\4.0\bin>net start mongodb
The service is not responding to the control function.

More help is available by typing NET HELPMSG 2186.


D:\apps\MongoDB\Server\4.0\bin>

经过几个小时的努力,我终于做到了。

确保:

您将<MONGODB_PATH>\bin目录添加到系统变量PATH中 以管理员身份运行命令提示符


步骤:

第一步:执行以下命令:

D:\mongodb\bin>mongod --remove

第二步:以管理员身份打开命令提示符后执行该命令:

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

注意:你也可以在上面的命令后面附加——serviceName MongoDB。

这是所有!


After that right there in the command prompt execute:
services.msc

// OR

net start MongoDB

寻找MongoDB service并单击start。


注意:确保以管理员身份运行命令提示符。

如果你不这样做,你的日志文件(在上面的例子中是D:\mongodb\log.txt)将包含这样的行:

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)

如果你试图从非管理控制台启动服务(即net start MongoDB或start - service MongoDB在PowerShell中),你会得到这样的响应:

System error 5 has occurred.
Access is denied.

或:

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