On this page本页内容
The architecture of a replica set affects the set’s capacity and capability. 副本集的体系结构会影响副本集的容量和性能。This document provides strategies for replica set deployments and describes common architectures.本文档提供了副本集部署的策略,并描述了常见的体系结构。
The standard replica set deployment for production system is a three-member replica set. 生产系统的标准副本集部署是一个三成员副本集。These sets provide redundancy and fault tolerance. 这些集合提供冗余和容错。Avoid complexity when possible, but let your application requirements dictate the architecture.尽可能避免复杂性,但让应用程序需求决定体系结构。
Add members in a replica set according to these strategies.根据这些策略在副本集中添加成员。
A replica set can have up to 副本集最多可以有50 members
, but only 7 voting members
. 50
个成员,但只有7
个投票成员。If the replica set already has 7 voting members, additional members must be non-voting members.如果复制集已经有7个有投票权的成员,则其他成员必须是无投票权的成员。
Ensure that the replica set has an odd number of voting members. 确保复制集的投票成员数为奇数。A replica set can have up to 7 voting members. 副本集最多可以有7个投票成员。If you have an even number of voting members, deploy another data bearing voting member or, if constraints prohibit against another data bearing voting member, an arbiter.如果您有偶数个投票成员,请部署另一个数据承载投票成员,或者,如果限制禁止另一个数据承载投票成员,则部署一个仲裁者。
An arbiter does not store a copy of the data and requires fewer resources. 仲裁器不存储数据的副本,需要的资源更少。As a result, you may run an arbiter on an application server or other shared process. 因此,您可以在应用服务器或其他共享进程上运行仲裁器。With no copy of the data, it may be possible to place an arbiter into environments that you would not place other members of the replica set. 如果没有数据副本,可能会将仲裁器放置到您不会放置副本集其他成员的环境中。Consult your security policies.查阅你的安全政策。
For the following MongoDB versions, 对于以下MongoDB版本,与pv1
increases the likelihood of w:1
rollbacks compared to pv0
(no longer supported in MongoDB 4.0+) for replica sets with arbiters:pv0
(MongoDB 4.0+中不再支持)相比,对于带有仲裁器的副本集,pv1
增加了w:1
回滚的可能性:
See Replica Set Protocol Version.请参阅副本集协议版本。
Warning警告
In general, avoid deploying more than one arbiter per replica set.通常,避免在每个副本集部署多个仲裁器。
Fault tolerance for a replica set is the number of members that can become unavailable and still leave enough members in the set to elect a primary. 副本集的容错性是指可以变得不可用,但仍在副本集中保留足够成员以选择主副本的成员数。In other words, it is the difference between the number of members in the set and the 换句话说,这是集合中的成员数量与选举初选所需的多数投票成员之间的差异。majority
of voting members needed to elect a primary. Without a primary, a replica set cannot accept write operations. 如果没有主副本,副本集将无法接受写入操作。Fault tolerance is an effect of replica set size, but the relationship is not direct. 容错性是副本集大小的一种影响,但这种关系不是直接的。See the following table:请参阅下表:
3 | 2 | 1 |
4 | 3 | 1 |
5 | 3 | 2 |
6 | 4 | 2 |
Adding a member to the replica set does not always increase the fault tolerance. 向副本集中添加成员并不总是能提高容错性。However, in these cases, additional members can provide support for dedicated functions, such as backups or reporting.但是,在这些情况下,其他成员可以为专用功能提供支持,例如备份或报告。
Starting in version 4.2.1, 从版本4.2.1开始,rs.status()
returns majorityVoteCount
for the replica set.rs.status()
返回副本集的majorityVoteCount
。
In a deployment with very high read traffic, you can improve read throughput by distributing reads to secondary members. 在具有非常高读取流量的部署中,可以通过将读取分发给辅助成员来提高读取吞吐量。As your deployment grows, add or move members to alternate data centers to improve redundancy and availability.随着部署的增长,请将成员添加或移动到备用数据中心,以提高冗余度和可用性。
Note
Distributing replica set members across two data centers provides benefit over a single data center. 跨两个数据中心分发副本集成员比单个数据中心更具优势。In a two data center distribution,在两个数据中心的分布中,
If possible, distribute members across at least three data centers. For config server replica sets (CSRS), the best practice is to distribute across three (or more depending on the number of members) centers. 如果可能,将成员分布在至少三个数据中心。对于配置服务器副本集(CSR),最佳做法是跨三个(或更多,取决于成员数量)中心分发。If the cost of the third data center is prohibitive, one distribution possibility is to evenly distribute the data bearing members across the two data centers and store the remaining member in the cloud if your company policy allows.如果第三个数据中心的成本过高,一种分发可能性是在两个数据中心之间均匀地分发数据承载成员,并在公司政策允许的情况下将剩余成员存储在云中。
Always ensure that the main facility is able to elect a primary.始终确保主要机构能够选择主要机构。
The existing members of a replica set must have spare capacity to support adding a new member. 副本集的现有成员必须具有备用容量,以支持添加新成员。Always add new members before the current demand saturates the capacity of the set.一定要在当前的需求饱和之前加入新成员。
To protect your data in case of a data center failure, keep at least one member in an alternate data center. 为了在数据中心发生故障时保护数据,请在备用数据中心中至少保留一名成员。If possible, use an odd number of data centers, and choose a distribution of members that maximizes the likelihood that even with a loss of a data center, the remaining replica set members can form a majority or at minimum, provide a copy of your data.如果可能,请使用奇数个数据中心,并选择一个成员分布,以最大限度地提高即使在数据中心丢失的情况下,剩余的副本集成员也可以形成多数或至少提供数据副本的可能性。
Note
Distributing replica set members across two data centers provides benefit over a single data center. 跨两个数据中心分发副本集成员比单个数据中心更具优势。In a two data center distribution,在两个数据中心的分布中,
If possible, distribute members across at least three data centers. 如果可能,将成员分布在至少三个数据中心。For config server replica sets (CSRS), the best practice is to distribute across three (or more depending on the number of members) centers. 对于配置服务器副本集(CSR),最佳做法是跨三个(或更多,取决于成员数量)中心分发。If the cost of the third data center is prohibitive, one distribution possibility is to evenly distribute the data bearing members across the two data centers and store the remaining member in the cloud if your company policy allows.如果第三个数据中心的成本过高,一种分发可能性是在两个数据中心之间均匀地分发数据承载成员,并在公司政策允许的情况下将剩余成员存储在云中。
To ensure that the members in your main data center be elected primary before the members in the alternate data center, set the 要确保主数据中心中的成员比备用数据中心中的成员先被选为主成员,请将备用数据中心中成员的members[n].priority
of the members in the alternate data center to be lower than that of the members in the primary data center.members[n].priority
设置为低于主数据中心中成员的优先级。
For more information, see Replica Sets Distributed Across Two or More Data Centers有关更多信息,请参阅分布在两个或多个数据中心的副本集
Use replica set tag sets to target read operations to specific members or to customize write concern to request acknowledgement from specific members.使用副本集标记集将读取操作定向到特定成员,或自定义写入问题以请求特定成员的确认。
MongoDB enables journaling by default. 默认情况下,MongoDB支持日志记录。Journaling protects against data loss in the event of service interruptions, such as power failures and unexpected reboots.日志记录可在服务中断(如电源故障和意外重启)时防止数据丢失。
Tip
When possible, use a logical DNS hostname instead of an ip address, particularly when configuring replica set members or sharded cluster members. 如果可能,请使用逻辑DNS主机名而不是ip地址,尤其是在配置副本集成员或分片集群成员时。The use of logical DNS hostnames avoids configuration changes due to ip address changes.使用逻辑DNS主机名可以避免由于ip地址更改而导致的配置更改。
If your application connects to more than one replica set, each set should have a distinct name. 如果应用程序连接到多个副本集,则每个副本集都应该有一个不同的名称。Some drivers group replica set connections by replica set name.一些驱动程序按副本集名称对副本集连接进行分组。
The following documents describe common replica set deployment patterns. 以下文档描述了常见的副本集部署模式。Other patterns are possible and effective depending on the application’s requirements. 根据应用程序的需求,其他模式是可能的和有效的。If needed, combine features of each architecture in your own deployment:如果需要,在您自己的部署中结合每个体系结构的功能: