一、首先須要 mongodb.msi安裝包mongodb
二、mongodb的配置文件mongod.cfg 內容以下:windows
systemLog: destination: file path: "D:/mongodb/logs/mongodb.log" storage: dbPath: "D:/mongodb/db" net: http: enabled: true RESTInterfaceEnabled: true
三、將mongodb以windows服務運行執行的命令爲:ui
"%installpath%\bin\mongod.exe" --auth --config "%installpath%\mongod.cfg" --smallfiles -install
%installpath% 爲安裝的目錄spa
--auth 表明其餘身份驗證日誌
--config 指定配置文件code
--smaillfiles 限制日誌大小,不然硬盤空間不足時,沒法啓動服務blog
-install 表示安裝未windos服務rem
四、設置登陸身份命令io
"%installpath%\bin\mongo.exe" 127.0.0.1:27017/admin --quiet SetAuth.js
SetAuth.js 文件內容以下:class
db.createUser({ user: "admin", pwd: "admin", roles: [{ role: "userAdminAnyDatabase", db: "admin" }] })
設置完上述登陸帳號後,須要重啓服務才能生效
五、由於配置文件的路徑須要根據安裝的路徑動態生成,下面是生成conf文件的腳本
set installpath=%~1 set installpath2=%installpath:\=/% rem Create installation directory mkdir "%installpath%" del "%installpath%\mongod.cfg" echo systemLog:>>"%installpath%\mongod.cfg" echo destination: file>>"%installpath%\mongod.cfg" echo path: "%installpath2%/logs/mongodb.log">>"%installpath%\mongod.cfg" echo storage:>>%installpath%\mongod.cfg echo dbPath: "%installpath2%/db">>"%installpath%\mongod.cfg" echo net:>>"%installpath%\mongod.cfg" echo http:>>"%installpath%\mongod.cfg" echo enabled: true>>"%installpath%\mongod.cfg" echo RESTInterfaceEnabled: true>>"%installpath%\mongod.cfg"
將全部綜合起來寫成一個安裝批處理setup.bat,以下:
set installpath=%~1 set installpath2=%installpath:\=/% rem Create installation directory mkdir "%installpath%" del "%installpath%\mongod.cfg" echo systemLog:>>"%installpath%\mongod.cfg" echo destination: file>>"%installpath%\mongod.cfg" echo path: "%installpath2%/logs/mongodb.log">>"%installpath%\mongod.cfg" echo storage:>>%installpath%\mongod.cfg echo dbPath: "%installpath2%/db">>"%installpath%\mongod.cfg" echo net:>>"%installpath%\mongod.cfg" echo http:>>"%installpath%\mongod.cfg" echo enabled: true>>"%installpath%\mongod.cfg" echo RESTInterfaceEnabled: true>>"%installpath%\mongod.cfg" rem install mongodb msiexec /x mongodb.msi /qn mongodb.msi /qn INSTALLLOCATION="%installpath%" ADDLOCAL="all" mkdir "%installpath%"\db mkdir "%installpath%"\logs rem create service "%installpath%\bin\mongod.exe" --auth --config "%installpath%\mongod.cfg" --smallfiles -install rem start service net start MongoDB "%installpath%\bin\mongo.exe" 127.0.0.1:27017/admin --quiet SetAuth.js net stop MongoDB net start MongoDB pause
使用方法
setup.bat "D:\Tes\mongoDB"