mongo集群部署

2021-12-27

windows

install.bat注册mongo为服务

@echo off

setlocal enabledelayedexpansion
::数据库路径
set serverPath=F:\MongoDB\Server\4.2
set binPath=%serverPath%\bin
set dataPath=%serverPath%\data
::数据集名称
set setName=mySet
::从库数量
set setNum=2
set name=master
::数据库开始端口,从节点依次累加
set port=27117

mkdir %serverPath%\conf

call :add Master

for /l %%i in (1,1,%setNum%) do (
	set name=slave%%i
	set /a port=port+1
	call :add Slave%%i
)
goto :EOF

::创建数据库目录及配置文件并注册为服务
:add
	::创建数据库目录
	mkdir %dataPath%\%name%
	
	::配置配置文件
	set conf=%serverPath%\conf\%name%.conf
	del /F /S /Q %conf%
	echo dbpath=%dataPath%\%name% >> %conf%
	echo logpath=%serverPath%\log\%name%.log >> %conf%
	echo logappend=true >> %conf%
	echo journal=true >> %conf%
	echo quiet=true >> %conf%
	echo port=%port% >> %conf%
	echo replSet=%setName% >> %conf%
	
	::注册为服务
	%binPath%\mongod --config %conf% --install --serviceName mongo%1 --serviceDisplayName mongo%1

goto :EOF

pause

启动mongodb

mongod --config F:\MongoDB\Server\4.2\master\mongo.conf

配置集群

# 连接mongo
mongo -port 27117

# 配置集群节点
rs.initiate({'_id':'mySet', members:[{_id:0, host:'127.0.0.1:27117'}, {_id:1,host:'127.0.0.1:27118'}, {_id:2,host:'127.0.0.1:27119'}]})

 

{/if}