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


当前回答

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

例如:

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

其他回答

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

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

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

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

mongod --install

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

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