MongoDB replica set replica set (basic knowledge)

background

2.7之前是主从,之后推荐副本集

Write in front
摘录自https://www.cnblogs.com/lijiaman/p/13184308.html
link

Overview

对于副本集,官方文档是这样定义的:副本集是一组mongod维护相同数据的实例,一个副本集包含多个数据承载节点和一个仲裁节点(可选),在数据承载节点中,只有一个成员节点被视为主节点,主节点能够进行读写操作,其它节点则被视为次要节点,次要节点只能进行读操作。

1. PSS Architecture

“一主两从”架构,如果主节点不可用,则符合条件的次要节点将进行选举以自行选举新的主要节点。
Insert picture description here

Replica sets distributed across 2 or more data centers:

Distributing replica set members between two data centers can provide benefits over a single data center. In the distribution of two data centers,
-if one of the data centers fails, unlike the distribution of a single data center, the data can still be read.
– If a data center with a small number of members fails, the replica set can still perform write and read operations at the same time.
– However, if the data center with the majority of the members fails, the replica set will become read-only.
If possible, assign members to at least three data centers. For configuration server replica sets (CSRS), the best practice is to distribute them in three (or more, depending on the number of members) centers. If the cost of the third data center is too high, one allocation possibility is to distribute the data-carrying members equally between the two data centers and store the remaining members in the cloud if the company policy allows it.

2. PSA architecture

在某些情况下(例如只有一个主服务器和一个辅助服务器,但由于成本限制,禁止添加另外一个服务器),你可以选择一个mongod实例作为仲裁器添加到副本集,仲裁节点参加选举,但是不保存数据。
Insert picture description here

3. Automatic failover

当主节点与其它成员的通信时间超过参数electionTimeoutMillis(默认10000,即10s)的限定时,会发生自动故障转移,原来的某个从节点会转换角色为主节点。

Insert picture description here

4. Read preferences (separate read and write)

默认情况下,客户端从主节点读取数据,但是,客户端也可以指定首选项,将读取请求发送到从节点。
Insert picture description here

5. MongoDB replica set members

MongoDB replica set has three main types of members:
Primary: Primary node
Secondaries: Slave node
Arbiter: Arbitration node, optional.

Master node and
主节点是副本集中接收写操作的唯一成员,MongoDB在主数据库上执行写操作,然后再主数据库的oplog上记录操作,从节点复制该日志,将操作应用于从数据库。
Insert picture description here
在以下3节点副本集中,主数据库不可用,将会触发一次选举,将从节点之一选举为新的主节点。
Insert picture description here
slave node (replica node)

The replica node is a read-only node. For the replica node, it can be configured as:

Priority 0 Replica Set Members: cannot become the primary node and cannot trigger elections, reside in the secondary data center or act as a cold standby storage.

Hidden Replica Set Members: The client application is invisible, preventing the application from reading data from the node. Hidden members must be members with priority 0

Delayed Replica Set Members: Delayed application log, used to recover from certain errors (such as accidental deletion of data). A member of a delayed replica set must be a member with a priority of 0. Set the priority to 0 to prevent it from becoming a primary member; it is best to set it as a hidden member to prevent applications from viewing delayed data.

Arbitration node
在某些情况下(例如您有一个主服务器和一个辅助服务器,但由于成本限制,禁止添加另一个辅助服务器),您可以选择将仲裁器添加到副本集。一个仲裁器不具有数据集的副本,并不能成为主节点。但是,仲裁节点参加选举。仲裁员具有确切的1个投票。 (应用在偶数节点中,是投票为奇数,推荐单独部署,复制集状态为7)
Insert picture description here

6. Operation log oplog

MongoDB在主数据库上执行写操作,并在操作日志中记录该操作,然后从数据库将日志异步同步过去并应用,每个副本集节点都包含一个oplog,保存在local.oplog.rs中。

因为每个副本集成员都存在oplog日志,为了促进复制,所有副本集成员都会发送心跳(pings)给所有的其它成员,任何从节点都可以从其它节点导入操作日志条目。

oplog size setting
首次启动副本集成员时,如果未指定操作日志大小,则MongoDB将创建默认大小的操作日志

The default operation log size depends on the storage engine:

储存引擎             默认操作日志大小     下界       上界 
-----------------  -----------------  ------    ------- 
内存中存储引擎       物理内存的550兆字节   50 GB 
WiredTiger存储引擎  可用磁盘空间的5990兆字节   50 GB

Before mongod creates the operation log, you can use the oplogSizeMB parameter to specify its size. After starting the replica set member for the first time, use the replSetResizeOplog command to change the operation log size.

View oplog status

要查看操作日志状态,包括操作的大小和时间范围,可以使用rs.printReplicationInfo()方法。

This article explains that the main technical content comes from the sharing of Internet technology giants, as well as some self-processing (only for the role of annotations). If related questions, please leave a message after the confirmation, the implementation of infringement will be deleted

Guess you like

Origin blog.csdn.net/baidu_34007305/article/details/111554786