Add Members to a Replica Set将成员添加到副本集中
On this page本页内容
Overview概述
This tutorial explains how to add an additional member to an existing replica set. 本教程介绍如何将附加成员添加到现有复制副本集中。For background on replication deployment patterns, see the Replica Set Deployment Architectures document.有关复制部署模式的背景信息,请参阅副本集部署体系结构文档。
Maximum Voting Members最多有投票权的成员
A replica set can have a maximum of seven voting members. 一个复制集最多可以有七个投票成员。To add a member to a replica set that already has seven voting members, you must either add the member as a non-voting member or remove a vote from an 要将成员添加到已经有七个投票成员的副本集中,必须将该成员添加为无投票成员,或者从现有成员中删除投票。existing member
.
Init Scripts初始化脚本
In production deployments you can configure a init script to manage member processes.在生产部署中,您可以配置init脚本来管理成员进程。
Existing Members现有成员
You can use these procedures to add new members to an existing replica set.您可以使用这些过程将新成员添加到现有复制副本集中。
Restore Former Members恢复以前的成员
You can use these procedures to re-add a node that was removed.您可以使用这些过程来重新添加已删除的节点。
If the data on the removed node is relatively recent, the node recovers and catches up to the rest of the replica set.如果删除的节点上的数据相对较新,则该节点将恢复并赶上副本集的其余部分。
Avoid creating new replicated collections on the removed node while it is in standalone mode. 避免在已删除的节点处于独立模式时在该节点上创建新的复制集合。If the standalone node rejoins the replica set, subsequent operations on the new collection produce an error.如果独立节点重新加入复制副本集,则对新集合的后续操作会产生错误。
Data Files数据文件
If you have a backup or snapshot of an existing member, you can move the data files (for example, the 如果您有现有成员的备份或快照,则可以将数据文件(例如,dbPath
directory) to a new system and use them to quickly initiate a new member. dbPath
目录)移动到新系统,并使用它们快速启动新成员。The files must be:文件必须是:
A valid copy of the data files from a member of the same replica set.来自同一复制副本集成员的数据文件的有效副本。See Back Up and Restore with Filesystem Snapshots document for more information.有关详细信息,请参阅使用文件系统快照备份和恢复文档。ImportantAlways use filesystem snapshots to create a copy of a member of the existing replica set.始终使用文件系统快照来创建现有副本集成员的副本。Do not use不要使用mongodump
andmongorestore
to seed a new replica set member.mongodump
和mongorestore
为新的副本集成员种子。More recent than the oldest operation in the primary's oplog.比primary操作日志中最旧的操作更新。The new member must be able to become current by applying operations from the primary's oplog.通过应用主操作日志中的操作,新成员必须能够成为最新成员。
IP BindingIP绑定
Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. 在将实例绑定到可公开访问的IP地址之前,必须保护群集不受未经授权的访问。For a complete list of security recommendations, see Security Checklist. 有关安全建议的完整列表,请参阅安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证和强化网络基础设施。
MongoDB binaries, MongoDB二进制文件mongod
and mongos
, bind to localhost by default. mongod
和mongos
默认绑定到localhost。If the 如果为二进制文件设置了net.ipv6
configuration file setting or the --ipv6
command line option is set for the binary, the binary additionally binds to the localhost IPv6 address.net.ipv6
配置文件设置或--ipv6
命令行选项,则二进制文件将额外绑定到localhost ipv6地址。
By default 默认情况下,绑定到localhost的mongod
and mongos
that are bound to localhost only accept connections from clients that are running on the same computer. mongod
和mongos
只接受来自在同一台计算机上运行的客户端的连接。This binding behavior includes 这种绑定行为包括mongosh
and other members of your replica set or sharded cluster. Remote clients cannot connect to binaries that are bound only to localhost.mongosh
和复制集或分片集群的其他成员。远程客户端无法连接到仅绑定到localhost的二进制文件。
To override the default binding and bind to other IP addresses, use the 要覆盖默认绑定并绑定到其他IP地址,请使用net.bindIp
configuration file setting or the --bind_ip
command-line option to specify a list of hostnames or IP addresses.net.bindIp
配置文件设置或--bind_ip
命令行选项指定主机名或IP地址列表。
Starting in MongDB 5.0, split horizon DNS从MongDB 5.0开始,仅配置有IP地址的拆分域DNS节点 nodes that are only configured with an IP address fail startup validation and report an error.
无法通过启动验证并报告错误。
See 请参阅disableSplitHorizonIPCheck
.disableSplitHorizonIPCheck
。
For example, the following 例如,以下mongod
instance binds to both the localhost and the hostname My-Example-Associated-Hostname
, which is associated with the IP address 198.51.100.1
:mongod
实例绑定到localhost和主机名My-Example-Associated-Hostname
,后者与IP地址198.51.100.1
关联:
mongod --bind_ip localhost,My-Example-Associated-Hostname
In order to connect to this instance, remote clients must specify the hostname or its associated IP address 为了连接到此实例,远程客户端必须指定主机名或其关联的IP地址198.51.100.1
:198.51.100.1
:
mongosh --host My-Example-Associated-Hostname
mongosh --host 198.51.100.1
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. 若要避免由于IP地址更改而导致配置更新,请使用DNS主机名而不是IP地址。It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.在配置复制集成员或分片集群成员时,使用DNS主机名而不是IP地址尤为重要。
Use hostnames instead of IP addresses to configure clusters across a split network horizon. 使用主机名而不是IP地址来跨拆分的网络范围配置群集。Starting in MongoDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.从MongoDB 5.0开始,只配置了IP地址的节点将无法通过启动验证,也不会启动。
Requirements要求
An active replica set.活动复制副本集。A new MongoDB system capable of supporting your data set, accessible by the active replica set through the network.一个新的MongoDB系统,能够支持您的数据集,活动副本集可以通过网络访问。
Otherwise, use the MongoDB installation tutorial and the Deploy a Replica Set tutorials.否则,请使用MongoDB安装教程和部署副本集教程。
Procedures过程
Prepare the Data Directory准备数据目录
Before adding a new member to an existing replica set, prepare the new member's data directory using one of the following strategies:在将新成员添加到现有副本集中之前,请使用以下策略之一准备新成员的数据目录:
Make sure the new member's data directory does not contain data.请确保新成员的数据目录不包含数据。The new member will copy the data from an existing member.新成员将从现有成员复制数据。If the new member is in a recovering state, it must exit and become a secondary before MongoDB can copy all data as part of the replication process.如果新成员处于恢复状态,则必须退出并成为secondary,然后MongoDB才能作为复制过程的一部分复制所有数据。This process takes time but does not require administrator intervention.此过程需要时间,但不需要管理员干预。Manually copy the data directory from an existing member.从现有成员手动复制数据目录。The new member becomes a secondary member and will catch up to the current state of the replica set.新成员将成为辅助成员,并将赶上副本集的当前状态。Copying the data over may shorten the amount of time for the new member to become current.复制数据可能会缩短新成员成为最新成员的时间。Ensure that you can copy the data directory to the new member and begin replication within the window allowed by the oplog.确保您可以将数据目录复制到新成员,并在oplog允许的窗口内开始复制。Otherwise, the new instance will have to perform an initial sync, which completely resynchronizes the data, as described in Resync a Member of a Replica Set.否则,新实例将不得不执行初始同步,这将完全重新同步数据,如重新同步副本集的成员中所述。Use使用rs.printReplicationInfo()
to check the current state of replica set members with regards to the oplog.rs.printReplicationInfo()
检查复制集成员相对于操作日志的当前状态。
For background on replication deployment patterns, see the Replica Set Deployment Architectures document.有关复制部署模式的背景信息,请参阅副本集部署体系结构文档。
Add a Member to an Existing Replica Set将成员添加到现有副本集中
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. 若要避免由于IP地址更改而导致配置更新,请使用DNS主机名而不是IP地址。It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.在配置复制集成员或分片集群成员时,使用DNS主机名而不是IP地址尤为重要。
Use hostnames instead of IP addresses to configure clusters across a split network horizon. 使用主机名而不是IP地址来跨拆分的网络范围配置群集。Starting in MongoDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.从MongoDB 5.0开始,只配置了IP地址的节点将无法通过启动验证,也不会启动。
Start the new启动新的mongod
instance.mongod
实例。Specify the data directory and the replica set name. The following example specifies the指定数据目录和复制副本集名称。以下示例指定/srv/mongodb/db0
data directory and thers0
replica set:/srv/mongodb/db0
数据目录和rs0
副本集:mongod --dbpath /srv/mongodb/db0 --replSet rs0 --bind_ip localhost,<hostname(s)|ip address(es)>
WarningBefore you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access.在将实例绑定到可公开访问的IP地址之前,必须保护群集不受未经授权的访问。For a complete list of security recommendations, see Security Checklist.有关安全建议的完整列表,请参阅安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证和强化网络基础设施。For more information on configuration options, see the有关配置选项的更多信息,请参阅mongod
manual page.mongod
手册页面。NoteOptional可选择的You can specify the data directory, replica set name, and the ip binding in the您可以在mongod.conf
configuration file, and start themongod
with the following command:mongod.conf
配置文件中指定数据目录、副本集名称和ip绑定,并使用以下命令启动mongod
:mongod --config /etc/mongod.conf
Connect to the replica set's primary.连接到复制副本集的主服务器。You can only add members while connected to the primary.您只能在连接到主节点时添加成员。If you do not know which member is the primary, log into any member of the replica set and issue the如果您不知道哪个成员是主成员,请登录到副本集的任何成员并发出db.hello()
command.db.hello()
命令。Use使用rs.add()
to add the new member to the replica set.rs.add()
将新成员添加到副本集中。Pass the将成员配置文档传递给方法。member configuration document
to the method.For example, to add a member at host例如,要在主机mongodb3.example.net
, issue the following command:mongodb3.example.net
上添加成员,请发出以下命令:rs.add( { host: "mongodb3.example.net:27017" } )
WarningBefore MongoDB 5.0, a newly added secondary still counts as a voting member even though it can neither serve reads nor become primary until its data is consistent.在MongoDB 5.0之前,新添加的次要成员仍然算作投票成员,即使在数据一致之前,它既不能提供读取服务,也不能成为主要成员。If you are running a MongoDB version earlier than 5.0 and add a secondary with its如果您运行的MongoDB版本早于5.0,并且添加了一个votes
andpriority
settings greater than zero, this can lead to a case where a majority of the voting members are online but no primary can be elected.votes
和priority
设置大于零的次要成员,这可能会导致大多数投票成员都在线,但无法选出主要成员。To avoid such situations, consider adding the new secondary initially with为了避免出现这种情况,请考虑添加新的次级,初始priority :0
andvotes :0
.priority :0
,votes :0
。Then, run然后,运行rs.status()
to ensure the member has transitioned intoSECONDARY
state.rs.status()
以确保成员已转换到SECONDARY
状态。Finally, use最后,使用rs.reconfig()
to update its priority and votes.rs.reconfig()
更新其优先级和投票。