如何设置MongoDB,使其可以作为Windows服务运行?
当前回答
以下步骤适用于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 ->右键单击->开始
其他回答
我使用的是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时,服务将显示启动,但它将终止。检查日志文件以确认服务已经真正启动。
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
现在一切都办好了。享受,
这是唯一对我有用的方法。因为所有东西都必须是绝对路径:
C:\Program Files\MongoDB\Server\3.2\bin>mongod --install --dbpath=c:/data/db --logpath=c:/data/logs/log.txt
我还必须从管理cmd运行它
这个答案是为那些已经使用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作为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数据存储目录
- 在MongoDB中查找重复的记录
- 为什么MongoDB Java驱动在条件中使用随机数生成器?
- 在猫鼬,我如何排序的日期?(node . js)
- 将映像存储在MongoDB数据库中
- 重复Mongo ObjectId的可能性在两个不同的集合中生成?
- Redis比mongoDB快多少?
- 亚马逊SimpleDB vs亚马逊DynamoDB
- 无法连接到服务器127.0.0.1:27017
- 如何创建数据库的MongoDB转储?
- 如何将MongoDB作为Windows服务运行?
- 如何监听MongoDB集合的变化?
- 什么时候不使用Cassandra?
- 如何在猫鼬排序?
- BASE术语解释