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


当前回答

在我的例子中,我在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>

其他回答

这是唯一对我有用的方法。因为所有东西都必须是绝对路径:

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

我还必须从管理cmd运行它

这对我来说很管用:

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"

以下步骤适用于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 ->右键单击->开始

考虑使用

mongod --install --rest --master

在Windows系统上编辑配置文件后,我不得不重新启动MongoDB (v4.4)服务。以下是我所做的:

按Win+R打开Run面板 输入“服务”。msc”,按“Enter” 搜索“MongoDB”-你可以按“m”跳转到它。 右键单击-选择“重新启动”

就是这样!