如何设置MongoDB,使其可以作为Windows服务运行?
当前回答
我使用的是2.4.9版本,并使用配置文件。直到我在配置文件中用空格包围了等号,服务才会启动:
dbpath = D:\Mongo data
logpath = C:\mongodb\logs\mongo.log
logappend = true
原来我有:
logpath=C:\mongodb\logs\mongo.log
我还发现,当安装服务时,你必须使用配置文件的绝对路径,例如:
c:\mongodb\bin\>mongodb.exe C:\mongodb\bin\mongod.conf --install
不要尝试在dbpath周围使用带有空格的倒逗号。当您执行net start MongoDB时,服务将显示启动,但它将终止。检查日志文件以确认服务已经真正启动。
其他回答
目前(直到2.4.4版本),如果任何路径(dbpath/logpath/config)包含空格,那么服务将不会启动,并显示错误:“服务没有响应控制功能”。
这对我来说很管用:
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"
如果您使用从管理员命令提示符下载的MSI安装MongoDB 2.6.1或更新版本,则会自动为您创建服务定义。
MongoDB文档也有一个教程,如果需要,可以帮助您手动创建Windows服务定义。
推荐的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与pymongo
- 如何在mongodb上导入。bson文件格式
- JSON文件的蒙古导入
- 如何删除mongodb中的数组元素?
- 修改MongoDB数据存储目录
- 在MongoDB中查找重复的记录
- 为什么MongoDB Java驱动在条件中使用随机数生成器?
- 在猫鼬,我如何排序的日期?(node . js)
- 将映像存储在MongoDB数据库中
- 重复Mongo ObjectId的可能性在两个不同的集合中生成?
- Redis比mongoDB快多少?
- 亚马逊SimpleDB vs亚马逊DynamoDB
- 无法连接到服务器127.0.0.1:27017
- 如何创建数据库的MongoDB转储?
- 如何将MongoDB作为Windows服务运行?