1、首先需要 mongodb.msi安装包
2、mongodb的配置文件mongod.cfg 内容如下:
systemLog: destination: file path: "D:/mongodb/logs/mongodb.log" storage: dbPath: "D:/mongodb/db" net: http: enabled: true RESTInterfaceEnabled: true
3、将mongodb以windows服务运行执行的命令为:
"%installpath%inmongod.exe" --auth --config "%installpath%mongod.cfg" --smallfiles -install
%installpath% 为安装的目录
--auth 代表其他身份验证
--config 指定配置文件
--smaillfiles 限制日志大小,否则硬盘空间不足时,无法启动服务
-install 表示安装未windos服务
4、设置登录身份命令
"%installpath%inmongo.exe" 127.0.0.1:27017/admin --quiet SetAuth.js
SetAuth.js 文件内容如下:
db.createUser({ user: "admin", pwd: "admin", roles: [{ role: "userAdminAnyDatabase", db: "admin" }] })
设置完上述登录账号后,需要重启服务才能生效
5、因为配置文件的路径需要根据安装的路径动态生成,下面是生成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%inmongod.exe" --auth --config "%installpath%mongod.cfg" --smallfiles -install rem start service net start MongoDB "%installpath%inmongo.exe" 127.0.0.1:27017/admin --quiet SetAuth.js net stop MongoDB net start MongoDB pause
使用方法
setup.bat "D:TesmongoDB"