如何将 MongoDB 作为 Windows 服务运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2438055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I run MongoDB as a Windows service?
提问by heisthedon
How can I set up MongoDB so it can run as a Windows service?
如何设置 MongoDB 以便它可以作为 Windows 服务运行?
采纳答案by Stephen Curran
I think if you run it with the --install
command line switch, it installs it as a Windows Service.
我认为如果您使用--install
命令行开关运行它,它会将其安装为 Windows 服务。
mongod --install
It might be worth reading this threadfirst though. There seems to be some problems with relative/absolute paths when the relevant registry key gets written.
不过,可能值得先阅读此线程。当相关的注册表项被写入时,相对/绝对路径似乎存在一些问题。
回答by hyeomans
After trying for several hours, I finally did it.
经过几个小时的尝试,我终于做到了。
Make sure that you added the <MONGODB_PATH>\bin
directory to the system variable PATH
确保将<MONGODB_PATH>\bin
目录添加到系统变量中PATH
First I executed this command:
首先我执行了这个命令:
D:\mongodb\bin>mongod --remove
Then I executed this command after opening command prompt as administrator:
然后我在以管理员身份打开命令提示符后执行了这个命令:
D:\mongodb\bin>mongod --dbpath=D:\mongodb --logpath=D:\mongodb\log.txt --install
After that right there in the command prompt execute:
之后在命令提示符处执行:
services.msc
And look for MongoDB service and click start.
并查找 MongoDB 服务并单击开始。
NOTE: Make sure to run command prompt as administrator.
注意:确保以管理员身份运行命令提示符。
If you don't do this, your log file (D:\mongodb\log.txt
in the above example) will contain lines like these:
如果你不这样做,你的日志文件(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)
and if you try to start the service from a non-admin console, (i.e. net start MongoDB
or Start-Service MongoDB
in PowerShell), you'll get a response like this:
如果您尝试从非管理控制台(即net start MongoDB
或Start-Service MongoDB
在 PowerShell 中)启动该服务,您将收到如下响应:
System error 5 has occurred.
Access is denied.
or this:
或这个:
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
回答by ruby
not only --install
,
不仅--install
,
also need --dbpath
and --logpath
还需要--dbpath
和--logpath
and after reboot OS you need to delete "mongod.lock" manually
重新启动操作系统后,您需要手动删除“mongod.lock”
回答by danday74
Unlike other answers this will ..
与其他答案不同,这将..
START THE SERVICE AUTOMATICALLY ON SYSTEM REBOOT / RESTART
在系统重启/重启时自动启动服务
MongoDB Install
MongoDB 安装
Windows
视窗
(1) Install MongoDB
(1) 安装MongoDB
(2) Add bin to path
(2) 将 bin 添加到路径
(3) Create c:\data\db
(3) 创建c:\data\db
(4) Create c:\data\log
(4) 创建c:\data\log
(5) Create c:\data\mongod.cfg with contents ..
(5) 用内容创建 c:\data\mongod.cfg ..
systemLog:
destination: file
path: c:\data\log\mongod.log
storage:
dbPath: c:\data\db
(6) To create service that will auto start on reboot.. RUN AS ADMIN..
(6) 创建将在重新启动时自动启动的服务.. RUN AS ADMIN..
sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB\Server.4\bin\mongod.exe\" --service --config=\"C:\data\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"
(7) Start the service .. RUN AS ADMIN ..
(7) 启动服务..以管理员身份运行..
net start MongoDB
IMPORTANT: Even if this says 'The MongoDB service was started successfully' it can fail
重要提示:即使显示“MongoDB 服务已成功启动”,它也可能会失败
To double check open Control Panel > Services, ensure the status of the MongoDB service is 'Running'
要仔细检查打开的控制面板 > 服务,请确保 MongoDB 服务的状态为“正在运行”
If not, check your log file at C:\data\log\mongod.log for the reason for failure and fix it
如果没有,请检查 C:\data\log\mongod.log 中的日志文件以了解失败的原因并修复它
(Do not start MongoDB via Control Panel > Services, use .. net start MongoDB)
(不要通过控制面板 > 服务启动 MongoDB,使用 .. net start MongoDB)
(8) Finally, restart your machine with MongoDB running and it will still be running on restart
(8) 最后,在运行 MongoDB 的情况下重启你的机器,它仍然会在重启时运行
If you ever want to kill it ..
如果你想杀死它..
net stop MongoDB
sc.exe delete MongoDB
回答by xameeramir
The below steps apply to Windows.
以下步骤适用于 Windows。
Run below in an administrativecmd
在管理中运行cmd
mongod --remove
This will remove the existing MongoDB service (if any).
这将删除现有的 MongoDB 服务(如果有)。
mongod --dbpath "C:\data\db" --logpath "C:\Program Files\MongoDB\Server.4\bin\mongod.log" --install --serviceName "MongoDB"
Make sure that C:\data\db
folder exists
确保该C:\data\db
文件夹存在
Open services with:
通过以下方式打开服务:
services.msc
Find MongoDB-> Right click -> Start
找到MongoDB-> 右键单击 -> 开始
回答by chafank
You can use the command below for running mongodb as a windows service
您可以使用以下命令将 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
If you use mongodb with default parameters, you can use these values :
如果您使用带有默认参数的 mongodb,则可以使用以下值:
- yourIPadress : 127.0.0.1 or localhost
- yourPortNumber : 27017 (default port) or dont put --port
- serviceDisplayName : only if you run more than one service (since mongodb 1.8)
- 你的IP地址:127.0.0.1或本地主机
- yourPortNumber : 27017 (默认端口) 或不放 --port
- serviceDisplayName :仅当您运行多个服务时(自 mongodb 1.8 起)
There's more information on this command here
这里有关于这个命令的更多信息
回答by helt
I tried all answers and then did it the way https://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/#configure-a-windows-service-for-mongodb-community-editiondescribes it.
我尝试了所有答案,然后按照https://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/#configure-a-windows-service-for-mongodb-community-edition描述的方式进行了操作.
Use a config file...
使用配置文件...
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
回答by Serdar
This PowerShell Script Downloads and Installs MongoDB as Windows Service:
此 PowerShell 脚本将 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
回答by Jamie Hutber
This was the only thing that worked for me. As everything had to be an absolute path:
这是唯一对我有用的东西。因为一切都必须是绝对路径:
C:\Program Files\MongoDB\Server.2\bin>mongod --install --dbpath=c:/data/db --logpath=c:/data/logs/log.txt
I also had to run it from admin cmd
我也不得不从 admin cmd
回答by Peter G.
The recommended way mongod --install
results in error:
推荐的方式mongod --install
导致错误:
2015-12-03T18:18:28.896+0100 I CONTROL --install has to be used with a log file for server output
After having installed mongodb in C:\mongodb
you need to simply add the logpath:
安装 mongodb 后,C:\mongodb
您只需添加日志路径:
mongod --install --logpath C:\mongodb\logs\mongo.log
The path to the logfile must exist and it must be an absolute Windows path. Then you start the MongoDB service by typing:
日志文件的路径必须存在,并且必须是绝对的 Windows 路径。然后通过键入以下内容启动 MongoDB 服务:
net start MongoDB