微信登录

高可用 - Replica Set - 副本集模式 - P、S、A

高可用 - Replica Set - 副本集模式 - P、S、A

Replica Set 副本集模式

Replica Set 是 mongod 的实例集合,包含三类节点角色:
Primary( 主节点 )
Secondary( 副本节点 )
Arbiter( 仲裁者 )

Primary( 主节点 ):
只有主节点可读可写,Primary 接收所有写请求,然后把数据同步到所有 Secondary 。一个 Replica Set 只有一个 Primary 节点,当 Primary 挂掉后,其他 Secondary 或者 Arbiter 节点会选举一个 Primary 节点,这样就又可以提供服务了。读请求默认是发到 Primary 节点处理,如果需要故意转发到 Secondary 需要客户端修改一下配置。
Secondary( 副本节点 ):
数据副本节点,当主节点挂掉的时候,参与选主。
Arbiter( 仲裁者 )
不存数据,不会被选为主,只进行选主投票。使用 Arbiter 可以减轻在减少数据的冗余备份,又能提供高可用的能力。

MongoDB 的 Replica Set 副本集模式特点:

数据多副本,在故障的时候,可以使用完的副本恢复服务(自动恢复)。
读写分离,读的请求分流到副本上,减轻主(Primary)的读压力;
节点直接互有心跳,可以感知集群的整体状态;

局限性

每两个节点之间互有心跳,这种模式会导致节点的心跳几何倍数增大,单个 Replica Set 集群规模不能太大,一般来讲最大不要超过 50 个节点。
备注:参与投票节点数要是奇数,这个非常重要。因为偶数会导致脑裂,也就是投票数对等的情况,无法选出 Primary。
Sharding 模式
Replica Set 模式无法应对海量数据所以有了分布式技术。有了Sharding 模式。

解决性能和容量瓶颈一般来说优化有两个方向:
纵向优化
横向优化
纵向优化是传统企业最常见的思路,持续不断的加大单个磁盘和机器的容量和性能。
横向优化通俗来讲就是加节点。业务上要划分系统数据集,并在多台服务器上处理,做到容量和能力跟机器数量成正比。
那么,实际情况下,哪一种更具可行性呢?
自然是分布式技术的方案,纵向优化的方案非常容易到达物理极限,横向优化则对个体要求不高,而是群体发挥效果。MongoDB 的 Sharding 模式就是 MongoDB 横向扩容的一个架构实现。

副本集

创建不同的data、log
不同的ip或者端口号启动
配置文件要加上

  1. replication:
  2. #复制:
  3. #副本集的名称
  4. replSetName: myrs

根据配置文件启动

  1. ->进入主节点:
  2. 初始化:
  3. rs.initiate()
  4. 添加副本节点:
  5. rs.add("副本节点ip")
  6. 添加仲裁节点:
  7. rs.addArb("仲裁节点ip")
  8. rs.add("仲裁节点ip",false)
  9. 状态
  10. rs.status
  11. ->进入副本节点:
  12. 确认是副本节点:
  13. rs.slave()
  14. rs.slave(true)
  15. 取消是副本节点:
  16. rs.slave(false)

3个文件

  1. # mongod.conf
  2. # 安装MongoDB服务命令
  3. # mongod --config "F:\Program Files\MongoDB\Server\8.0\bin\mongod.cfg" --install --serviceName "MongoDB-inst1"
  4. # .\mongod --config "F:\Program Files\MongoDB\Server\8.0\bin\mongod2.cfg" --install --serviceName "MongoDB-inst2" --serviceDisplayName "MongoDB Server (inst2)"
  5. # .\mongod --config "F:\Program Files\MongoDB\Server\8.0\bin\mongod3.cfg" --install --serviceName "MongoDB-inst3" --serviceDisplayName "MongoDB Server (inst3)"
  6. # for documentation of all options, see:
  7. # http://docs.mongodb.org/manual/reference/configuration-options/
  8. # Where and how to store data.
  9. storage:
  10. dbPath: F:\Program Files\MongoDB\Server\8.0\data
  11. # where to write logging data.
  12. systemLog:
  13. destination: file
  14. logAppend: true
  15. path: F:\Program Files\MongoDB\Server\8.0\log\mongod.log
  16. # network interfaces
  17. net:
  18. port: 27017
  19. bindIp: 0.0.0.0
  20. #processManagement:
  21. #security:
  22. #operationProfiling:
  23. replication:
  24. replSetName: "rs0" # 将 "rs0" 替换为你想要的复制集名称
  1. # mongod.conf
  2. # for documentation of all options, see:
  3. # http://docs.mongodb.org/manual/reference/configuration-options/
  4. # Where and how to store data.
  5. storage:
  6. dbPath: F:\Program Files\MongoDB\Server\8.0\data2
  7. # where to write logging data.
  8. systemLog:
  9. destination: file
  10. logAppend: true
  11. path: F:\Program Files\MongoDB\Server\8.0\log\mongod2.log
  12. # network interfaces
  13. net:
  14. port: 27018
  15. bindIp: 0.0.0.0
  16. replication:
  17. replSetName: "rs0" # 将 "rs0" 替换为你想要的复制集名称
  1. # mongod.conf
  2. # for documentation of all options, see:
  3. # http://docs.mongodb.org/manual/reference/configuration-options/
  4. # Where and how to store data.
  5. storage:
  6. dbPath: F:\Program Files\MongoDB\Server\8.0\data3
  7. # where to write logging data.
  8. systemLog:
  9. destination: file
  10. logAppend: true
  11. path: F:\Program Files\MongoDB\Server\8.0\log\mongod3.log
  12. # network interfaces
  13. net:
  14. port: 27019
  15. bindIp: 0.0.0.0
  16. replication:
  17. replSetName: "rs0" # 将 "rs0" 替换为你想要的复制集名称

区别3个:

1、dbPath是不同的data文件夹

2、systemLog path 是不同的log文件

3、net port 不同的启动端口

windows 启动命令

  1. mongod --config "D:\mongo_inst1\inst1.cfg" --install --serviceName "MongoDB‑inst1"
  2. # .\mongod --config "F:\Program Files\MongoDB\Server\8.0\bin\mongod2.cfg" --install --serviceName "MongoDB-inst2" --serviceDisplayName "MongoDB Server (inst2)"
  3. # .\mongod --config "F:\Program Files\MongoDB\Server\8.0\bin\mongod3.cfg" --install --serviceName "MongoDB-inst3" --serviceDisplayName "MongoDB Server (inst3)"

service就可以看见了

mongosh进入

D:\Program Files\MongoDB\mongosh>mongosh.exe

  1. Current Mongosh Log ID: 6a7c1ba4b6eaf4388817e3af
  2. Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.9.2
  3. Using MongoDB: 8.0.13
  4. Using Mongosh: 2.9.2
  5. For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/
  6. To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
  7. You can opt-out by running the disableTelemetry() command.
  8. ------
  9. The server generated these startup warnings when booting
  10. 2026-08-12T15:01:29.935+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
  11. 2026-08-12T15:01:29.936+08:00: You are running on a NUMA machine. We suggest disabling NUMA in the machine BIOS by enabling interleaving to avoid performance problems. See your BIOS documentation for more information
  12. ------
  13. test>

直接发送启动副本集

  1. rs.initiate({
  2. _id: "rs0",
  3. members: [
  4. { _id: 0, host: "127.0.0.1:27017" },
  5. { _id: 1, host: "127.0.0.1:27018" },
  6. { _id: 2, host: "127.0.0.1:27019" }
  7. ]
  8. })

看见输出:

  1. test> rs.initiate({
  2. | _id: "rs0",
  3. | members: [
  4. | { _id: 0, host: "127.0.0.1:27017" },
  5. | { _id: 1, host: "127.0.0.1:27018" },
  6. | { _id: 2, host: "127.0.0.1:27019" }
  7. | ]
  8. | })
  9. {
  10. ok: 1,
  11. '$clusterTime': {
  12. clusterTime: Timestamp({ t: 1786075909, i: 1 }),
  13. signature: {
  14. hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
  15. keyId: Long('0')
  16. }
  17. },
  18. operationTime: Timestamp({ t: 1786075909, i: 1 })
  19. }
  20. rs0 [direct: secondary] test>