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


当前回答

不仅——安装,

还需要——dbpath和——logpath

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

其他回答

最简单的方法是,

创建文件夹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

请尝试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

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