一:MongoDB安装完后,将其注册成windows服务。
方式有两种:
1.通过命令行参数
mongod --dbpath "D:\1\12\mongodb\data\db" --logpath "D:\1\12\mongodb\data\log\mongodb.log" --install --serviceName "MongoDB"
2.通过配置文件
mongod --install -f "D:\Program Files\MongoDB\mongo.conf"
mongo.conf内容如下:
dbpath = "D:\Program Files\MongoDB\Data\DB" logpath = "D:\Program Files\MongoDB\Data\Log\mongodb.log" logappend = true directoryperdb = true serviceName = "MongoDBService" serviceDisplayName = "MongoDBService1" port = 27017
命令执行后命令行没有任何输出直接结束了,但是启动服务时调用net start MongoService没有反应——注册服务失败。
查看日志文件mongodb.log,发现如下记录:
59:46.730+0800 I CONTROL [main] ***** SERVER RESTARTED ***** 59:47.130+0800 I CONTROL [main] Trying to install Windows service 'MongoDBService' 59:47.130+0800 I CONTROL [main] Error connecting to the Service Control Manager: 拒绝访问。 (5)
解决方法:
找到cmd.exe,右键以管理员身份运行,然后再执行上述注册服务的命令。
二:又报错误:
logPath requires an absolute file path with Windows services dbPath requires an absolute file path with Windows services
解决方法:
将配置文件中引号去掉
三:安装成功:
再次执行注册服务的命令之后查看日志,发现如下记录,说明安装成功:
23:53.286+0800 I CONTROL [main] Trying to install Windows service '"MongoDBService"' 23:53.286+0800 I CONTROL [main] Service '"MongoDBService"' ("MongoDBService1") installed with command line '"D:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --config D:\program\MongoDB\mongo.conf --service' 23:53.286+0800 I CONTROL [main] Service can be started from the command line with 'net start "MongoDBService"'
四:启动服务:
D:\>net start MongoDBService The MongoDBService1 service is starting... The MongoDBService1 service was started successfully.