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


当前回答

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

参考

其他回答

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

以下步骤适用于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

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