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


当前回答

您可以使用下面的命令将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

其他回答

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

下载并安装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

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

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

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

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

就是这样!

这对我来说很管用:

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"