如何将MongoDB作为Windows服务运行?
如何设置MongoDB,使其可以作为Windows服务运行?我认为,如果使用如何将MongoDB作为Windows服务运行?,mongodb,windows-services,nosql,Mongodb,Windows Services,Nosql,如何设置MongoDB,使其可以作为Windows服务运行?我认为,如果使用--install命令行开关运行它,它将作为Windows服务安装 mongod --install 不过,这本书可能值得一读。写入相关注册表项时,相对/绝对路径似乎存在一些问题。考虑使用 mongod --install --rest --master 不仅——安装 还需要--dbpath和--logpath 重新启动操作系统后,您需要手动删除“mongod.lock”您可以使用下面的命令将mongodb作为win
--install
命令行开关运行它,它将作为Windows服务安装
mongod --install
不过,这本书可能值得一读。写入相关注册表项时,相对/绝对路径似乎存在一些问题。考虑使用
mongod --install --rest --master
不仅——安装
还需要--dbpath
和--logpath
重新启动操作系统后,您需要手动删除“mongod.lock”您可以使用下面的命令将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或本地主机
- 您的端口号:27017(默认端口)或NOT put--port
- serviceDisplayName:仅当您运行多个服务时(从mongodb 1.8开始)
这里有关于这个命令的更多信息
经过几个小时的努力,我终于做到了
确保已将\bin
目录添加到系统变量路径
首先我执行了这个命令:
D:\mongodb\bin>mongod --remove
然后,我以管理员身份打开命令提示符后执行了此命令:
D:\mongodb\bin>mongod --dbpath=D:\mongodb --logpath=D:\mongodb\log.txt --install
之后,在命令提示符下执行:
services.msc
然后查找MongoDB服务并单击开始
注意:确保以管理员身份运行命令提示符。
如果不这样做,日志文件(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)
如果您尝试从非管理控制台启动服务(即PowerShell中的net start MongoDB
或start service MongoDB
),您将得到如下响应:
发生系统错误5。
访问被拒绝。
或者这个:
启动服务:由于以下错误,无法启动服务“MongoDB(MongoDB)”:无法打开MongoDB服务
在计算机“”上。
第1行字符:1
+启动服务MongoDB
+ ~~~~~~~~~~~~~~~~~~~~~
+CategoryInfo:OpenError:(System.ServiceProcess.ServiceController:ServiceController)[启动服务],
ServiceCommandException
+FullyQualifiedErrorId:CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceComman
此PowerShell脚本下载并安装MongoDB作为Windows服务:
Set ExecutionPolicy RemoteSigned
$mongoDbPath=“C:\MongoDB”
$mongodbconfig路径=“$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((测试路径-路径$mongoDbPath)-eq$True)
{
编写主机“似乎您已经安装了MongoDB”
出口
}
md$mongoDbPath
md“$mongoDbPath\log”
md“$mongoDbPath\data”
md“$mongoDbPath\data\db”
[System.IO.File]::AppendAllText($mongodbconfig路径”,“dbpath=C:\MongoDB\data\db`r`n”)
[System.IO.File]::AppendAllText($mongoDbConfigPath”,“logpath=C:\MongoDB\log\mongo.log`r`n”)
[System.IO.File]::AppendAllText(“$mongodbconfig路径”,“smallfiles=true`r`n”)
[System.IO.File]::AppendAllText($mongoDbConfigPath”,“noprealloc=true`r`n”)
$webClient=新对象System.Net.webClient
$webClient.DownloadFile($url,$zipFile)
$shellApp=新对象-com shell.application
$destination=$shellApp.namespace($mongoDbPath)
$destination.Copyhere($shellApp.namespace($zipFile.items())
复制项“$unzippedFolderContent\*”$mongoDbPath-递归
删除项$unzippedFolderContent-recurse-force
删除项目$zipFile-递归-强制
&$mongoDBPath\bin\mongod.exe--配置$mongoDbConfigPath--安装
&净启动mongodb
对于版本2.4.3(截至发布日期的当前版本),请创建一个配置文件,然后执行以下操作:
C:\MongoDB\bin\mongod.exe--config C:\MongoDB\mongod.cfg--service
当前(版本2.4.4之前),如果任何路径(dbpath/logpath/config)包含空格,则服务将不会启动,并显示错误:“服务未响应控制功能”
我使用的是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时,服务将显示为启动,但它将终止。检查日志文件以确认服务已真正启动。如果使用从安装MongoDB 2.6.1或更新版本,则应自动为您创建服务定义
MongoDB文档还提供了一个教程,可以在需要时帮助您进行定义。这对我很有用:
sc.exe create MongoDB binPath= "d:\MongoDB\bin\mongod.exe --service --config=d:\MongoDB\bin\mongod.config" displayname= "MongoDB 2.6 Standard" start= "auto"
正如Mongo文档中所描述的那样,对我来说,转义binPath失败了
失败:
sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB 2.6 Standard\bin\mongod.exe\" --service --config=\"C:\Program Files\MongoDB 2.6 Standard\mongod.cfg\"" DisplayName= "MongoDB 2.6 Standard" start= "auto"
建议的方法mongod--install
会导致错误:
2015-12-03T18:18:28.896+0100 I CONTROL --install has to be used with a log file for server output
在C:\mongodb
中安装mongodb后,只需添加日志路径:
mongod --install --logpath C:\mongodb\logs\mongo.log
日志文件的路径必须存在,并且必须是绝对Windows路径。然后通过键入以下命令启动MongoDB服务:
net start MongoDB
我尝试了所有的答案,然后按照描述的方式做了
使用配置文件
“C:\mongodb\bin\mongod.exe”--配置“C:\mongodb\mongod.cfg”--安装
以管理员身份运行“cmd.exe”,然后运行“sc.exe”添加新的Windows服务
例如:
sc.exe create MongoDB binPath= "c:\program files\mongodb\server\3.2\bin\mongod.exe"
这是唯一对我有用的东西。因为一切都必须是一条绝对路径:
C:\Program Files\MongoDB\Server\3.2\bin>mongod --install --dbpath=c:/data/db --logpath=c:/data/logs/log.txt
我还必须从admin cmd
运行它,我发现您应该使用install标志将:dbpath、config和logfile传递给mongod
mongod --remove
例如:
mongod --dbpath=c:\data\db --config=c:\data\db\config.cfg --logpath=c:\data\db\log.txt --install
注意:我的path变量中有mongod path
net start mongodb
net stop mongodb
systemLog:
destination: file
path: c:\data\log\mongod.log
storage:
dbPath: c:\data\db
sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe\" --service --config=\"C:\data\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"
net start MongoDB
net stop MongoDB
sc.exe delete MongoDB
C:\Program Files\MongoDB\Server\3.4\bin> mongod.exe --install mongod --dbpath="c:\data\db" --logpath="c:\data\db\log.txt"
net run MongoDB
mongod --remove
mongod --dbpath "C:\data\db" --logpath "C:\Program Files\MongoDB\Server\3.4\bin\mongod.log" --install --serviceName "MongoDB"
services.msc
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
mongod --config "C:\Program Files\MongoDB\Server\3.6\mongod_primary.cfg" --install --serviceName "MongoDB_Primary" --serviceDisplayName "MongoDB Primary"
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: D:\apps\MongoDB\Server\4.0\data
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: D:\apps\MongoDB\Server\4.0\log\mongod.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
#processManagement:
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
D:\apps\MongoDB\Server\4.0\bin>mongod --config D:\apps\MongoDB\Server\4.0\bin\mongod.cfg --install
D:\apps\MongoDB\Server\4.0\bin>net stop mongodb
The MongoDB service is stopping.
The MongoDB service was stopped successfully.
D:\apps\MongoDB\Server\4.0\bin>mongod --remove
2019-04-10T09:39:29.305+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-04-10T09:39:29.309+0800 I CONTROL [main] Trying to remove Windows service 'MongoDB'
2019-04-10T09:39:29.310+0800 I CONTROL [main] Service 'MongoDB' removed
D:\apps\MongoDB\Server\4.0\bin>
D:\apps\MongoDB\Server\4.0\bin>sc.exe create MongoDB binPath= "\"D:\apps\MongoDB\Server\4.0\bin\mongod.exe\" --service --config=\"D:\apps\MongoDB\Server\4.0\bin\mongod.cfg\""
[SC] CreateService SUCCESS
D:\apps\MongoDB\Server\4.0\bin>net start mongodb
The MongoDB service is starting..
The MongoDB service was started successfully.
D:\apps\MongoDB\Server\4.0\bin>
D:\apps\MongoDB\Server\4.0\bin>sc.exe create MongoDB binPath= "D:\apps\MongoDB\Server\4.0\bin\mongod --config D:\apps\MongoDB\Server\4.0\bin\mongod.cfg"
[SC] CreateService SUCCESS
D:\apps\MongoDB\Server\4.0\bin>net start mongodb
The service is not responding to the control function.
More help is available by typing NET HELPMSG 2186.
D:\apps\MongoDB\Server\4.0\bin>