如何设置 MongoDB 使其可以作为 Windows 服务运行?
最佳答案
尝试了几个小时,我终于做到了。
<MONGODB_PATH>\bin
系统变量的目录PATH
第 1 步: 执行此命令:
D:\mongodb\bin>mongod --remove
第2步:以管理员身份打开命令提示符后执行此命令:
D:\mongodb\bin>mongod --dbpath=D:\mongodb --logpath=D:\mongodb\log.txt --install
注意:您也可以附加 --serviceName MongoDB
在上面的命令之后。
services.msc
// OR
net start MongoDB
然后寻找MongoDB服务并点击开始。
注意:确保以管理员身份运行命令提示符。
如果您不这样做,您的日志文件(上例中为 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
或 PowerShell 中的 Start-Service MongoDB
),您将收到如下响应:
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
https://stackoverflow.com/questions/2438055/