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


当前回答

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

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

我还必须从管理cmd运行它

其他回答

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

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

我还必须从管理cmd运行它

推荐的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

正在mongodb DB: 3.6上工作

(1)安装MongoDB

(2)将bin添加到环境路径变量中

(3)创建c:\data\db

(4)创建c:\data\mongo .log

(5)在bin文件夹上执行以下命令

.\mongo .exe——install——logpath c:\data\mongo .log——logappend ——bind_ip 12 7.0.0.1——dbpath c:\data\db

(6)启动mongodb作为服务

net start MongoDB

(7)最后在命令行中运行mongo,检查mongo shell是否打开。

我尝试了所有的答案,然后按照https://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/#configure-a-windows-service-for-mongodb-community-edition描述的方式来做。

使用配置文件…

“C:\mongodb\bin\mongo .exe”——config“C:\mongodb\mongo .cfg”——安装

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

确保:

您将<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