一: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.