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


我认为如果你用——install命令行开关运行它,它会把它作为Windows服务安装。

mongod --install

也许值得先读一下这篇文章。当写入相关的注册表项时,相对/绝对路径似乎存在一些问题。


考虑使用

mongod --install --rest --master

不仅——安装,

还需要——dbpath和——logpath

重启操作系统后,你需要删除“mongod”。手动锁”


您可以使用下面的命令将mongodb作为windows服务运行

"C:\mongodb\bin\mongod" --bind_ip  yourIPadress  --logpath  "C:\data\dbConf\mongodb.log"  --logappend  --dbpath  "C:\data\db"  --port yourPortNumber --serviceName "YourServiceName" --serviceDisplayName "YourServiceName" --install 

如果你使用mongodb默认参数,你可以使用这些值:

youripaddress: 127.0.0.1或localhost 你的portnumber: 27017(默认端口)或不放——port serviceDisplayName:仅当你运行多个服务时(从mongodb 1.8开始)

这里有关于这个命令的更多信息

http://www.mongodb.org/display/DOCS/Windows+Service


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

确保:

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

下载并安装MongoDB作为Windows服务:

Set-ExecutionPolicy RemoteSigned

$mongoDbPath = "C:\MongoDB" 
$mongoDbConfigPath = "$mongoDbPath\mongod.cfg"
$url = "http://downloads.mongodb.org/win32/mongodb-win32-x86_64-2008plus-2.4.9.zip" 
$zipFile = "$mongoDbPath\mongo.zip" 
$unzippedFolderContent ="$mongoDbPath\mongodb-win32-x86_64-2008plus-2.4.9"

if ((Test-Path -path $mongoDbPath) -eq $True) 
{ 
  write-host "Seems you already installed MongoDB"
    exit 
}

md $mongoDbPath 
md "$mongoDbPath\log" 
md "$mongoDbPath\data" 
md "$mongoDbPath\data\db"

[System.IO.File]::AppendAllText("$mongoDbConfigPath", "dbpath=C:\MongoDB\data\db`r`n")
[System.IO.File]::AppendAllText("$mongoDbConfigPath", "logpath=C:\MongoDB\log\mongo.log`r`n")
[System.IO.File]::AppendAllText("$mongoDbConfigPath", "smallfiles=true`r`n")
[System.IO.File]::AppendAllText("$mongoDbConfigPath", "noprealloc=true`r`n")

$webClient = New-Object System.Net.WebClient 
$webClient.DownloadFile($url,$zipFile)

$shellApp = New-Object -com shell.application 
$destination = $shellApp.namespace($mongoDbPath) 
$destination.Copyhere($shellApp.namespace($zipFile).items())

Copy-Item "$unzippedFolderContent\*" $mongoDbPath -recurse

Remove-Item $unzippedFolderContent -recurse -force 
Remove-Item $zipFile -recurse -force

& $mongoDBPath\bin\mongod.exe --config $mongoDbConfigPath --install

& net start mongodb

对于2.4.3版本(发布日期的当前版本),创建一个配置文件,然后执行以下命令:

C:\MongoDB\bin\mongo .exe——config C:\MongoDB\mongo .cfg——service


目前(直到2.4.4版本),如果任何路径(dbpath/logpath/config)包含空格,那么服务将不会启动,并显示错误:“服务没有响应控制功能”。


1) echo logpath=F:\mongodb\log\mongo.log > F:\mongodb\mongod.cfg

2) dbpath=F:\mongodb\data\db [add this to the next line in mongod.cfg]

C:\>F:\mongodb\bin\mongod.exe –config F:\mongodb\mongod.cfg –install

参考


我使用的是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时,服务将显示启动,但它将终止。检查日志文件以确认服务已经真正启动。


如果您使用从管理员命令提示符下载的MSI安装MongoDB 2.6.1或更新版本,则会自动为您创建服务定义。

MongoDB文档也有一个教程,如果需要,可以帮助您手动创建Windows服务定义。


这对我来说很管用:

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

我尝试了所有的答案,然后按照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”——安装


以管理员身份运行“cmd.exe”,然后运行“sc.exe”,添加一个新的Windows服务。

例如:

sc.exe create MongoDB binPath= "c:\program files\mongodb\server\3.2\bin\mongod.exe"

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

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

我还必须从管理cmd运行它


我发现你应该用install标志将:dbpath, config和logfile传递给mongod

例子:

mongod --dbpath=c:\data\db --config=c:\data\db\config.cfg --logpath=c:\data\db\log.txt --install 

注意:我的路径变量中有mongod path。

你可以通过以下方式控制服务:

net start mongodb
net stop  mongodb 

不像其他答案。

在系统重启/重新启动时自动启动服务

MongoDB安装

窗户

(1)安装MongoDB

(2)添加bin到路径

(3)创建c:\data\db

(4)创建c:\data\log

(5)创建c:\data\mongo .cfg

systemLog:
    destination: file
    path: c:\data\log\mongod.log
storage:
    dbPath: c:\data\db

(6)创建在重启时自动启动的服务。以管理员身份运行..

sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe\" --service --config=\"C:\data\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"

(7)启动服务。以管理员身份运行..

net start MongoDB

重要:即使显示“MongoDB服务已成功启动”,它也可能失败

要再次检查打开的控制面板>服务,确保MongoDB服务的状态为“Running”

如果不是,检查C:\data\log\mongo .log中的日志文件,找出失败的原因并修复它

(不要通过控制面板>服务启动MongoDB,使用..net start MongoDB

(8)最后,重新启动你的机器,运行MongoDB,重新启动时它仍将运行

如果你想杀死它…

net stop MongoDB

sc.exe delete MongoDB

这个答案是为那些已经使用MSI安装mongo DB的人准备的。

假设您的默认安装位置是“C:\Program Files\MongoDB\Server\3.2\bin”

运行mongo作为窗口服务的步骤

以管理员身份打开命令提示符 输入cd C:\Program Files\MongoDB\Server\3.2\bin(请正确检查路径,因为您可能安装了不同的版本,而不是3.2)。 按回车键 Type net start MongoDB 按回车键 按Windows + R,输入services。检查Mongo是否作为服务运行。


以下是安装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


最简单的方法是,

创建文件夹C:\data\db 创建文件C:\data\db\log.txt 以“以管理员身份运行”打开命令提示符,确认mogodb bin目录路径正确并写入 C:\Program Files\MongoDB\Server\3.4\ bin> mongod.exe——install mongod——dbpath="c:\data\db"——logpath="c:\data\db\log.txt" 启动mongodb服务: net run 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 ->右键单击->开始


MongoDB 3.4 [Windows]

Create dir C:/mongodb/data Create a file in C:/mongodb/mongodb.config using this configuration: storage: engine: wiredTiger dbPath: "C:/mongodb/data" directoryPerDB: true journal: enabled: true systemLog: destination: file path: "C:/mongodb/data/mongod.log" logAppend: true timeStampFormat: iso8601-utc net: bindIp: 127.0.0.1 port: 27017 wireObjectCheck : false To install MongoDb as a service, run this command in powershell with admin power mongod --config="C:\mongodb\mongodb.config" --install --service Open Services.msc and look for MongoDb, then start it


正在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是否打开。


mongod --config "C:\Program Files\MongoDB\Server\3.6\mongod_primary.cfg" --install --serviceName "MongoDB_Primary" --serviceDisplayName "MongoDB Primary"

请尝试Mongo DB: 3.6和Windows 10中的步骤

mongod——删除 mongod——dbpath=C:/data/db——port 27017——logpath C:/data/log/log.txt——service . dbpath=C:/data/ log/log.txt mongod——dbpath=C:/data/db——port 27017——logpath C:/data/log/log.txt——install . sh net start MongoDB


check windows services if you have service for mongo remove it by run bellow command mongod --remove create mongo.cfg file with bellow content systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db path: where you want to store log datas dbPath: your database directory then run bellow command sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB\Server\3.2\bin\mongod.exe\" --service --config=\"C:\Program Files\MongoDB\Server\3.2\mongod.cfg\"" DisplayName= "MongoDB" start= "auto" binPath : mongodb installation directory config: .cfg file address DisplayName:Your Service Name start service 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>

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

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

就是这样!