Mongodb Replica Sets 副本集群搭建

Replica Sets 复制集搭建

MongoDB 有三种集群架构模式,分别为主从复制(Master-Slaver)、副本集(Replica Set)和分片(Sharding)模式。

Master-Slaver 是一种主从复制的模式,目前已经不推荐使用

ReplicaSet模式取代了Master-Slaver模式,是一种互为主从的关系。Replica Set 将数据复制多份保存,不同服务器保存同一份数据,在出现故障时自动切换,实现故障转移。

MongoDB复制集主要用于实现服务的高可用性,与Redis中的哨兵模式相似。它的核心作用是数据的备份和故障转移。

Sharding 模式适合处理大量数据,它将数据分开存储,不同服务器保存不同的数据,所有服务器数据的总和即为整个数据集。

本文档主要内容为复制集的搭建和操作

一、操作系统参数优化
1.1 关闭内存大页

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| echo never > /sys/kernel/mm/transparent_hugepage/enabled echo never > /sys/kernel/mm/transparent_hugepage/defrag 并追加⽌⽂件中 /etc/rc.local [root@localhost ~]# vim /etc/rc.d/rc.local 增加下列内容: if test -f /sys/kernel/mm/transparent_hugepage/enabled; then echo never > /sys/kernel/mm/transparent_hugepage/enabled fi if test -f /sys/kernel/mm/transparent_hugepage/defrag; then echo never > /sys/kernel/mm/transparent_hugepage/defrag fi 保存并退出,然后给rc.local添加可执行权限。 [root@localhost ~]# chmod +x /etc/rc.d/rc.local 最后重启。 |

1 . 2 禁用numa架构

使用普通用户启动命令之前添加 sudo -u mongo numactl --interleave=all

修改内核参数

|----------------------------------------------------------------------------------------------|
| echo 0 > /proc/sys/vm/zone_reclaim_mode echo vm.zone_reclaim_mode = 0 >> /etc/sysctl.conf |

1.3 设置 vm.swappiness

临时修改

|-----------------------------------------------------------------------|
| sysctl -w vm.swappiness=0 永久修改 vim /etc/sysctl.conf vm.swappiness = 0 |

1.4 修改打开⽂件数限制

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| vim /etc/security/limits.conf mongo soft nofile 65535 mongo hard nofile 65535 mongo soft nproc 65535 mongo hard nproc 65535 mongo soft stack 10240 mongo hards stack 10240 |

二、基础安装
2.1 一主二从副本集搭建

|-----|-----------|-------|--------------------------------------|
| 节点名 | IP | 端口 | 配置文件 |
| M1 | 127.0.0.1 | 27018 | /u01/mongodb/node1/conf/mongodb.conf |
| S1 | 127.0.0.1 | 27019 | /u01/mongodb/node2/conf/mongodb.conf |
| S2 | 127.0.0.1 | 27020 | /u01/mongodb/node3/conf/mongodb.conf |

2.2下载安装包并解压

|----------------------------------------------------------|
| cd /u01 tar -zxvf mongodb-linux-x86_64-rhel70-4.4.26.tgz |

移动目录

|---------------------------------------------------|
| mv mongodb-linux-x86_64-rhel70-4.4.26.tgz mongodb |

创建目录

|---------------------------------------------------------------------------------------------------------------|
| mkdir key node1 cd /u01/mongodb/node1 mkdir data log conf cd /u01/mongodb cp -r node1 node2 cp -r node2 node3 |

创建启动账号

|------------------------------------------|
| groupadd mongo useradd -M -g mongo mongo |

生成keyfile,如果部署到不同主机,需要拷贝到其他主机上

|-------------------------------------------------------------------------------------------------------------|
| openssl rand -base64 159 > /u01/mongodb/key/mongo_cluster.key chmod 600 /u01/mongodb/key/mongo_cluster.key |

修改目录权限

|-----------------------------------|
| chown -R mongo:mongo /u01/mongodb |

三、配置文件

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| systemLog: destination: file path: /u01/mongodb/node1/log/mongod.log # log path logAppend: true logRotate: reopen destination: file timeStampFormat: "iso8601-local" storage: dbPath: /u01/mongodb/node1/data # data directory journal: #是否启用journal日志 enabled: true directoryPerDB: true syncPeriodSecs: 60 engine: wiredTiger #存储引擎 wiredTiger: engineConfig: cacheSizeGB: 10 journalCompressor: "snappy" directoryForIndexes: false collectionConfig: blockCompressor: "snappy" indexConfig: prefixCompression: true net: port: 27018 # port bindIpAll: true maxIncomingConnections: 50000 wireObjectCheck: true ipv6: false unixDomainSocket: enabled: true pathPrefix: "/u01/mongodb/node1/tmp" filePermissions: 0700 processManagement: fork: true pidFilePath: /u01/mongodb/node1/node1.pid security: keyFile: "/u01/mongodb/key/mongo_cluster.key" clusterAuthMode: "keyFile" authorization: "enabled" javascriptEnabled: true operationProfiling: slowOpThresholdMs: 100 mode: slowOp replication: oplogSizeMB: 20480 replSetName: "repl_cluster" enableMajorityReadConcern: false |

3个节点配置文件一致,根据需要更改相应值即可

四、启动并初始化
4.1 启动3个数据节点

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| sudo -u mongo numactl --interleave=all mongod -f /u01/mongodb/node1/conf/mongodb.conf sudo -u mongo numactl --interleave=all mongod -f /u01/mongodb/node2/conf/mongodb.conf sudo -u mongo numactl --interleave=all mongod -f /u01/mongodb/node3/conf/mongodb.conf |

4.2 初始化配置

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| use admin cfg={_id:"repl_cluster", members:[{_id:0,host:"localhost:27018",priority:1}, {_id:1,host:"localhost:27019"}, {_id:2,host:"localhost:27020"}] } rs.initiate(cfg); |

4.3 查看复制集状态

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| repl_cluster:PRIMARY> rs.status() { "set" : "repl_cluster", "date" : ISODate("2024-01-05T06:35:09.080Z"), "myState" : 1, "term" : NumberLong(2), "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 2, "writeMajorityCount" : 2, "votingMembersCount" : 3, "writableVotingMembersCount" : 3, "members" : [ { "_id" : 0, "name" : "localhost:27018", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 78682, "optime" : { "ts" : Timestamp(1704436504, 1), "t" : NumberLong(2) }, "optimeDate" : ISODate("2024-01-05T06:35:04Z"), "lastAppliedWallTime":ISODate("2024-01-05T06:35:04.073Z"), "lastDurableWallTime":ISODate("2024-0105T06:35:04.073Z"), "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1704357871, 1), "electionDate" : ISODate("2024-01-04T08:44:31Z"), "configVersion" : 1, "configTerm" : 2, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "localhost:27019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 78647, "optime" : { "ts" : Timestamp(1704436504, 1), "t" : NumberLong(2) }, "optimeDurable" : { "ts" : Timestamp(1704436504, 1), "t" : NumberLong(2) }, "optimeDate" : ISODate("2024-01-05T06:35:04Z"), "optimeDurableDate" : ISODate("2024-01-05T06:35:04Z"), "lastAppliedWallTime":ISODate("2024-01-05T06:35:04.073Z"), "lastDurableWallTime":ISODate("2024-0105T06:35:04.073Z"), "lastHeartbeat" : ISODate("2024-01-05T06:35:07.970Z"), "lastHeartbeatRecv" : ISODate("2024-01-05T06:35:08.967Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "localhost:27020", "syncSourceId" : 2, "infoMessage" : "", "configVersion" : 1, "configTerm" : 2 }, { "_id" : 2, "name" : "localhost:27020", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 78642, "optime" : { "ts" : Timestamp(1704436504, 1), "t" : NumberLong(2) }, "optimeDurable" : { "ts" : Timestamp(1704436504, 1), "t" : NumberLong(2) }, "optimeDate" : ISODate("2024-01-05T06:35:04Z"), "optimeDurableDate" : ISODate("2024-01-05T06:35:04Z"), "lastAppliedWallTime":ISODate("2024-01-05T06:35:04.073Z"), "lastDurableWallTime":ISODate("2024-0105T06:35:04.073Z"), "lastHeartbeat" : ISODate("2024-01-05T06:35:07.971Z"), "lastHeartbeatRecv" : ISODate("2024-01-05T06:35:08.465Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "localhost:27018", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1, "configTerm" : 2 } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1704436504, 1), "signature" : { "hash" : BinData(0,"v2qzTOmGVCC4cSO2EF38WQ3TGLM="), "keyId" : NumberLong("7319816138693541892") } }, "operationTime" : Timestamp(1704436504, 1) } |

查看Replica Sets 状态

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| repl_cluster:PRIMARY> rs.isMaster() { "topologyVersion" : { "processId" : ObjectId("65966fc3b2be80d8248ff95e"), "counter" : NumberLong(6) }, "hosts" : [ "localhost:27018", "localhost:27019", "localhost:27020" ], "setName" : "repl_cluster", "setVersion" : 1, "ismaster" : true, "secondary" : false, "primary" : "localhost:27018", "me" : "localhost:27018", "electionId" : ObjectId("7fffffff0000000000000002"), "lastWrite" : { "opTime" : { "ts" : Timestamp(1704436574, 1), "t" : NumberLong(2) }, "lastWriteDate" : ISODate("2024-01-05T06:36:14Z"), "majorityOpTime" : { "ts" : Timestamp(1704436574, 1), "t" : NumberLong(2) }, "majorityWriteDate" : ISODate("2024-01-05T06:36:14Z") }, "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 100000, "localTime" : ISODate("2024-01-05T06:36:19.346Z"), "logicalSessionTimeoutMinutes" : 30, "connectionId" : 39, "minWireVersion" : 0, "maxWireVersion" : 9, "readOnly" : false, "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1704436574, 1), "signature" : { "hash" : BinData(0,"GIed52Qk4RdLOy9M0YmdT2z76NA="), "keyId" : NumberLong("7319816138693541892") } }, "operationTime" : Timestamp(1704436574, 1) } |

4.4 创建账号并认证

|-------------------------------------------------------------------------------------------------------------|
| db.createUser({user:"admin", pwd: "123456",roles:[{role:"root",db:"admin"}]}) db.auth("admin", "123456"); |

4.5 重新登录并验证

mongo --port 27018 -u admin -p

rs.status()

五、replica Set 管理
5.1 读写分离

主库插入测试数据

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| use test j={name:"mongo_new",age:5} i={x:3} db.test.insert(j) db.test.insert(i) db.test.save(i) db.test.save(j) db.test.find() repl_cluster:PRIMARY>use test switched to db test repl_cluster:PRIMARY> db.test.find() { "_id" : ObjectId("65962274d846bcd325533e5b"), "x" : 3 } { "_id" : ObjectId("659626a1d846bcd325533e5c"), "name" : "mongo_new", "age" : 5 } |

从库进行查询操作

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| repl_cluster:SECONDARY> show collections uncaught exception: Error: listCollections failed: { "topologyVersion" : { "processId" : ObjectId("65966fe4c8db6c6ee7ee58fa"), "counter" : NumberLong(4) }, "operationTime" : Timestamp(1704422833, 1), "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435, "codeName" : "NotPrimaryNoSecondaryOk", "$clusterTime" : { "clusterTime" : Timestamp(1704422833, 1), "signature" : { "hash" : BinData(0,"zp3Q5q7KdZpW6jlTQSwi3bbLgkA="), "keyId" : NumberLong("7319816138693541892") } } } |

查询报错,从库无法执行查询操作

开启从库查询操作

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| repl_cluster:SECONDARY> db.getMongo().setSecondaryOk() repl_cluster:SECONDARY> show collections test repl_cluster:SECONDARY> db.test.find() { "_id" : ObjectId("65962274d846bcd325533e5b"), "x" : 3 } { "_id" : ObjectId("659626a1d846bcd325533e5c"), "name" : "mongo_new", "age" : 5 } repl_cluster:SECONDARY> |

5.2 故障转移

模拟主库down机,验证故障是否自动转移

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 杀掉主库进程 [root@erp-db ~]# ps -ef |grep mongo | more mongo 15862 1 0 Jan04 ? 00:12:23 mongod -f /u01/mongodb/node1/conf/mongodb.conf mongo 16006 1 0 Jan04 ? 00:12:25 mongod -f /u01/mongodb/node2/conf/mongodb.conf mongo 16111 1 0 Jan04 ? 00:12:38 mongod -f /u01/mongodb/node3/conf/mongodb.conf [root@erp-db ~]# kill -9 15862 |

查看副本集群状态

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| repl_cluster:PRIMARY> rs.status() { "set" : "repl_cluster", "date" : ISODate("2024-01-05T06:45:16.062Z"), "myState" : 1, "term" : NumberLong(3), "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 2, "writeMajorityCount" : 2, "votingMembersCount" : 3, "writableVotingMembersCount" : 3, "members" : [ { "_id" : 0, "name" : "localhost:27018", "health" : 0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "optimeDurable" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"), "lastAppliedWallTime":ISODate("2024-01-05T06:44:34.087Z"), "lastDurableWallTime":ISODate("2024-0105T06:44:34.087Z"), "lastHeartbeat" : ISODate("2024-01-05T06:45:15.756Z"), "lastHeartbeatRecv" : ISODate("2024-01-05T06:44:37.970Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "Error connecting to localhost:27018 (127.0.0.1:27018) :: caused by :: Connection refused", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "configVersion" : 1, "configTerm" : 2 }, { "_id" : 1, "name" : "localhost:27019", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 79256, "optime" : { "ts" : Timestamp(1704437107, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2024-01-05T06:45:07Z"), "lastAppliedWallTime":ISODate("2024-01-05T06:45:07.750Z"), "lastDurableWallTime":ISODate("2024-0105T06:45:07.750Z"), "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1704437087, 1), "electionDate" : ISODate("2024-01-05T06:44:47Z"), "configVersion" : 1, "configTerm" : 3, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 2, "name" : "localhost:27020", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 79248, "optime" : { "ts" : Timestamp(1704437107, 1), "t" : NumberLong(3) }, "optimeDurable" : { "ts" : Timestamp(1704437107, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2024-01-05T06:45:07Z"), "optimeDurableDate" : ISODate("2024-01-05T06:45:07Z"), "lastAppliedWallTime":ISODate("2024-01-05T06:45:07.750Z"), "lastDurableWallTime":ISODate("2024-0105T06:45:07.750Z"), "lastHeartbeat" : ISODate("2024-01-05T06:45:15.742Z"), "lastHeartbeatRecv" : ISODate("2024-01-05T06:45:14.249Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "localhost:27019", "syncSourceId" : 1, "infoMessage" : "", "configVersion" : 1, "configTerm" : 3 } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1704437107, 1), "signature" : { "hash" : BinData(0,"Q6vILQyJe0QvnCDuZZUfS2IJafA="), "keyId" : NumberLong("7319816138693541892") } }, "operationTime" : Timestamp(1704437107, 1) } |

27018端口mongodb出现异常,状态为(not reachable/healthy)",系统自动选举了27019端口为主,实现了故障的自动转移

当原主进程启动后,自动加入到集群中,角色为SECONDARY

5.3 增加节点
5.3.1 通过oplog来增加节点

1、 配置并启动新节点,启用20721作为新节点端口,配置文件根据需要更改

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| sudo -u mongo numactl --interleave=all mongod -f /u01/mongodb/node4/conf/mongodb.conf about to fork child process, waiting until server is ready for connections. forked process: 25698 child process started successfully, parent exiting |

2、 添加新节点到现有Replica Set

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| repl_cluster:PRIMARY> rs.add("localhost:27021") { "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1704439046, 1), "signature" : { "hash" : BinData(0,"S8ApX1WUo83welNeMKf5jkBdj+Q="), "keyId" : NumberLong("7319816138693541892") } }, "operationTime" : Timestamp(1704439046, 1) } |

3、 查看集群状态

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| { "_id" : 3, "name" : "localhost:27021", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 10, "optime" : { "ts" : Timestamp(1704439046, 1), "t" : NumberLong(3) }, "optimeDurable" : { "ts" : Timestamp(1704439046, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2024-01-05T07:17:26Z"), "optimeDurableDate" : ISODate("2024-01-05T07:17:26Z"), "lastAppliedWallTime":ISODate("2024005T07:17:26.414Z"), "lastDurableWallTime":ISODate("20240105T07:17:26.414Z"), "lastHeartbeat" : ISODate("2024-01-05T07:17:36.481Z"), "lastHeartbeatRecv":ISODate("2024-0105T07:17:36.772Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "configVersion" : 2, "configTerm" : 3 } ], "ok" : 1, |

4、 验证数据同步

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| repl_cluster:SECONDARY> rs.secondaryOk() repl_cluster:SECONDARY> db.test.find() { "_id" : ObjectId("65962274d846bcd325533e5b"), "x" : 3 } { "_id" : ObjectId("659626a1d846bcd325533e5c"), "name" : "mongo_new", "age" : 5 } |

通过oplog日志进行添加节点操作简单,但oplog是capped collection,采用循环的方式处理日志,通过oplog日志来添加节点,可能导致数据的不一致(日志被刷新),因此我们引出快照和oplog相结合的方式来添加节点,保证数据的一致性。

方式:复制一份副本集的物理文件来做初始化数据,然后使用oplog日志来追溯数据,最终达到数据的一致性。

5.3.2 通过数据库快照(--fastsync)和oplog来增加节点

1、复制一份副本集成员的物理文件做初始数据

|----------------------------------------------|
| cp --r /u01/mongodb/node4 /u01/mongodb/node5 |

2、复制完成后,清理不需要的文件,并插入一条新文档,作为同步验证使用

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| repl_cluster:PRIMARY> j={name:"node5",age:27022} { "name" : "node5", "age" : 27022 } repl_cluster:PRIMARY> db.test.insert(j) WriteResult({ "nInserted" : 1 }) repl_cluster:PRIMARY> db.test.find() { "_id" : ObjectId("65962274d846bcd325533e5b"), "x" : 3 } { "_id" : ObjectId("659626a1d846bcd325533e5c"), "name" : "mongo_new", "age" : 5 } { "_id" : ObjectId("6597b42827437b37032f0aa2"), "name" : "node5", "age" : 27022 } |

3、启动新节点27022端口

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [root@erp-db]# sudo -u mongo numactl --interleave=all mongod -f /u01/mongodb/node5/conf/mongodb.conf about to fork child process, waiting until server is ready for connections. forked process: 2520 child process started successfully, parent exiting |

4、添加新节点到现有Replica Set

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| repl_cluster:PRIMARY> rs.add("localhost:27022") { "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1704442052, 1), "signature" : { "hash" : BinData(0,"nQHjkoZke4XsoroiK8SqqisCfsA="), "keyId" : NumberLong("7319816138693541892") } }, "operationTime" : Timestamp(1704442052, 1) } |

5、 验证数据同步

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| repl_cluster:SECONDARY> rs.secondaryOk() repl_cluster:SECONDARY> db.test.find() { "_id" : ObjectId("65962274d846bcd325533e5b"), "x" : 3 } { "_id" : ObjectId("659626a1d846bcd325533e5c"), "name" : "mongo_new", "age" : 5 } { "_id" : ObjectId("6597b42827437b37032f0aa2"), "name" : "node5", "age" : 27022 } |

5.4减少节点

将刚刚添加的两个新节点27021和27022从复制集中去除掉,只需执行rs.remove 指令

就可以了,具体如下:

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| repl_cluster:PRIMARY> rs.remove("localhost:27021") { "ok" : 1, "clusterTime" : { "clusterTime" : Timestamp(1704444644, 1), "signature" : { "hash" : BinData(0,"2XlJmd+hB0L2oNeXGO4GNwS3E1U="), "keyId" : NumberLong("7319816138693541892") } }, "operationTime" : Timestamp(1704444644, 1) } repl_cluster:PRIMARY\> rs.remove("localhost:27022") { "ok" : 1, "clusterTime" : { "clusterTime" : Timestamp(1704444654, 1), "signature" : { "hash" : BinData(0,"d1MJJ3p+dzvKBS63W3DWVWteb0k="), "keyId" : NumberLong("7319816138693541892") } }, "operationTime" : Timestamp(1704444654, 1) } |

查看集群状态,27021,27022节点已经删除

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| repl_cluster:PRIMARY> rs.status() { "set" : "repl_cluster", "date" : ISODate("2024-01-05T08:51:31.008Z"), "myState" : 1, "term" : NumberLong(3), "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 2, "writeMajorityCount" : 2, "votingMembersCount" : 3, "writableVotingMembersCount" : 3, "members" : [ { "_id" : 0, "name" : "localhost:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 7101, "optime" : { "ts" : Timestamp(1704444687, 1), "t" : NumberLong(3) }, "optimeDurable" : { "ts" : Timestamp(1704444687, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2024-01-05T08:51:27Z"), "optimeDurableDate" : ISODate("2024-01-05T08:51:27Z"), "lastAppliedWallTime":ISODate("2024-01-05T08:51:27.956Z"), "lastDurableWallTime":ISODate("2024-0105T08:51:27.956Z"), "lastHeartbeat" : ISODate("2024-01-05T08:51:30.974Z"), "lastHeartbeatRecv" : ISODate("2024-01-05T08:51:30.977Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "localhost:27020", "syncSourceId" : 2, "infoMessage" : "", "configVersion" : 5, "configTerm" : 3 }, { "_id" : 1, "name" : "localhost:27019", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 86831, "optime" : { "ts" : Timestamp(1704444687, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2024-01-05T08:51:27Z"), "lastAppliedWallTime":ISODate("2024-01-05T08:51:27.956Z"), "lastDurableWallTime":ISODate("2024-0105T08:51:27.956Z"), "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1704437087, 1), "electionDate" : ISODate("2024-01-05T06:44:47Z"), "configVersion" : 5, "configTerm" : 3, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 2, "name" : "localhost:27020", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 86823, "optime" : { "ts" : Timestamp(1704444687, 1), "t" : NumberLong(3) }, "optimeDurable" : { "ts" : Timestamp(1704444687, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2024-01-05T08:51:27Z"), "optimeDurableDate" : ISODate("2024-01-05T08:51:27Z"), "lastAppliedWallTime":ISODate("2024-01-05T08:51:27.956Z"), "lastDurableWallTime":ISODate("2024-0105T08:51:27.956Z"), "lastHeartbeat" : ISODate("2024-01-05T08:51:30.975Z"), "lastHeartbeatRecv" : ISODate("2024-01-05T08:51:30.977Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "localhost:27019", "syncSourceId" : 1, "infoMessage" : "", "configVersion" : 5, "configTerm" : 3 } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1704444687, 1), "signature" : { "hash" : BinData(0,"bBPDXuxrAqjzfO86rcj13v7mKQI="), "keyId" : NumberLong("7319816138693541892") } }, "operationTime" : Timestamp(1704444687, 1) } |

相关推荐
vvvae12347 小时前
分布式数据库
数据库
雪域迷影7 小时前
PostgreSQL Docker Error – 5432: 地址已被占用
数据库·docker·postgresql
bug菌¹8 小时前
滚雪球学Oracle[4.2讲]:PL/SQL基础语法
数据库·oracle
逸巽散人8 小时前
SQL基础教程
数据库·sql·oracle
月空MoonSky9 小时前
Oracle中TRUNC()函数详解
数据库·sql·oracle
momo小菜pa9 小时前
【MySQL 06】表的增删查改
数据库·mysql
向上的车轮9 小时前
Django学习笔记二:数据库操作详解
数据库·django
编程老船长10 小时前
第26章 Java操作Mongodb实现数据持久化
数据库·后端·mongodb
全栈师10 小时前
SQL Server中关于个性化需求批量删除表的做法
数据库·oracle
Data 31710 小时前
Hive数仓操作(十七)
大数据·数据库·数据仓库·hive·hadoop