MongoDB副本集配置:构建稳健的数据复制体系
1、启动三个mongodb服务,指定所属副本集
将原来的副本集删除掉,新建一个
replica
的文件夹,在文件夹下新建三个与之前一样的副本集,修改里面的conf
文件后,使用以下命令打开三个终端,启动三个副本集。
mongod --port 27017 --dbpath "D:\MongoDB\Server-5.0.18\replica\a" --replSet rs0
mongod --port 27018 --dbpath "D:\MongoDB\Server-5.0.18\replica\b" --replSet rs0
mongod --port 27019 --dbpath "D:\MongoDB\Server-5.0.18\replica\c" --replSet rs0
2、初始化副本集
使用命令登录任意一个副本集节点(
mongo --port 27017
),将replSet_config复制并粘贴,告诉该节点还存在哪些副本集节点。
config = {
_id:"rs0",
members:[
{
_id:0,
host:"127.0.0.1:27017",
priority:3
},
{
_id:1,
host:"127.0.0.1:27018",
priority:2
},
{
_id:2,
host:"127.0.0.1:27019",
priority:1
}
]
}
3、修改副本集权重[0-100]
副本priority的权重值越大,优先级越高,最高的为Primary,其次都为Secondary。如果副本集都不设置priority,默认members中第一个为Primary,且priority的值为0。
任意打开一个副本集mongo --port 27017
,执行以下操作。
-
获取副本集配置信息
cfg = rs.config()
-
重写cfg
cfg.members[0].priority = 1 cfg.members[2].priority = 3
-
刷新配置
rs.reconfig(cfg)
4、踩坑全集
1、Error:Our set name did not match that of the request target, requestTarget:127.0.0.1:27019
解决:出现这个问题是config的_id值与你的集群节点的replSet属性的值不一致。在第一步更改统一的replSet值,本地的cfg/conf文件同样也需要更改。
2、Error:This node was not started *with* the replSet option
解决:以管理员身份打开终端,关闭mongodb服务,net stop mongodb
5、其他
rs:该对象是副本集的全局对象
rs.status():查看副本集节点状态
rs.isMaster():是否为活跃节点
rs.secondaryOk():副本集同步 // mongodb4 可能是rs.salveOk()
最后附上一次性能启动三个副本集的脚本代码,粘贴到文本文件中,修改后缀名为bat,即可运行。想要可以多环境运行,可以转换成exe文件,需要特殊的软件。
注意:代码中的路径可能需要做一些修改。
@echo off
REM 设置默认 MongoDB 相关路径
set "DEFAULT_MONGODB_BIN_PATH=D:\MongoDB\Server\bin"
set "DEFAULT_DB_PATH=D:\MongoDB\Server\repl"
:input_paths
REM 提示用户输入 MongoDB 相关路径
set /p "MONGODB_BIN_PATH=Enter MongoDB bin path: "
if "%MONGODB_BIN_PATH%"=="" set "MONGODB_BIN_PATH=%DEFAULT_MONGODB_BIN_PATH%"
set /p "DB_PATH=Enter MongoDB DB path: "
if "%DB_PATH%"=="" set "DB_PATH=%DEFAULT_DB_PATH%"
REM 启动 MongoDB 副本集
start /B "" "%MONGODB_BIN_PATH%\mongod.exe" --port 27017 --dbpath "%DB_PATH%\a" --replSet rs0 >nul 2>&1
start /B "" "%MONGODB_BIN_PATH%\mongod.exe" --port 27018 --dbpath "%DB_PATH%\b" --replSet rs0 >nul 2>&1
start /B "" "%MONGODB_BIN_PATH%\mongod.exe" --port 27019 --dbpath "%DB_PATH%\c" --replSet rs0 >nul 2>&1
REM 等待 MongoDB 副本集启动完成
timeout /t 5 >nul
REM 检查 MongoDB 副本集是否成功启动
tasklist | find /i "mongod.exe" >nul
if %errorlevel% equ 0 (
REM MongoDB 副本集启动成功
echo MongoDB replica set successfully ran!
) else (
REM MongoDB 副本集启动失败,尝试使用默认路径
echo Failed to start MongoDB replica set with the provided paths. Trying with default paths...
start /B "" "%DEFAULT_MONGODB_BIN_PATH%\mongod.exe" --port 27017 --dbpath "%DEFAULT_DB_PATH%\a" --replSet rs0 >nul 2>&1
start /B "" "%DEFAULT_MONGODB_BIN_PATH%\mongod.exe" --port 27018 --dbpath "%DEFAULT_DB_PATH%\b" --replSet rs0 >nul 2>&1
start /B "" "%DEFAULT_MONGODB_BIN_PATH%\mongod.exe" --port 27019 --dbpath "%DEFAULT_DB_PATH%\c" --replSet rs0 >nul 2>&1
REM 等待 MongoDB 副本集启动完成
timeout /t 5 >nul
REM 再次检查 MongoDB 副本集是否成功启动
tasklist | find /i "mongod.exe" >nul
if %errorlevel% equ 0 (
REM MongoDB 副本集启动成功
echo MongoDB replica set successfully ran with default paths!
) else (
REM MongoDB 副本集启动失败
echo Failed to start MongoDB replica set with provided and default paths!
goto input_paths
)
)
REM 等待输入指令
set /p input=Press any key to stop MongoDB replica set and exit...
REM 关闭 MongoDB 副本集
taskkill /F /IM mongod.exe >nul 2>&1
REM 退出脚本
exit