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.您可以使用这些过程向现有副本集添加新成员。
Warning
Each replica set node must belong to one, and only one, replica set. Replica set nodes cannot belong to more than one 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.如果删除的节点上的数据相对较新,则该节点会恢复并赶上副本集的其余部分。
Important
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. The files must be:dbPath目录)移动到新系统,并使用它们快速启动新成员。文件必须是:
A valid copy of the data files from a member of the same replica set. See Back Up and Restore a Self-Managed Deployment with Filesystem Snapshots document for more information.来自同一副本集成员的数据文件的有效副本。有关更多信息,请参阅使用文件系统快照备份和还原自我管理部署文档。Important
Always use filesystem snapshots to create a copy of a member of the existing replica set. Do not use始终使用文件系统快照创建现有副本集成员的副本。不要使用mongodumpandmongorestoreto seed a new replica set member.mongodump和mongorestore为新的副本集成员添加种子。More recent than the oldest operation in the primary's oplog.比primary的oplog中最古老的操作更近。The new member must be able to become current by applying operations from the primary's oplog.新成员必须能够通过应用主oplog中的操作来成为最新成员。
IP BindingIP绑定
Warning
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 for Self-Managed Deployments. At minimum, consider enabling authentication and hardening network infrastructure.
MongoDB binaries, mongod and mongos, bind to localhost by default. 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.
By default mongod and mongos that are bound to localhost only accept connections from clients that are running on the same computer. 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.
To override the default binding and bind to other IP addresses, use the net.bindIp configuration file setting or the --bind_ip command-line option to specify a list of hostnames or IP addresses.
Warning
Starting in MongDB 5.0, split horizon DNS nodes that are only configured with an IP address fail startup validation and report an error. See 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 --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
Important
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.为避免因IP地址更改而进行配置更新,请使用DNS主机名而不是IP地址。在配置副本集成员或分片集群成员时,使用DNS主机名而不是IP地址尤为重要。
Use hostnames instead of IP addresses to configure clusters across a split network horizon. Starting in MongoDB 5.0, nodes that are only configured with an IP address fail startup validation and do not start.使用主机名而不是IP地址来配置跨拆分网络范围的集群。从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 Self-Managed Replica Set tutorials.
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. 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. Otherwise, the new instance will have to perform an initial sync, which completely resynchronizes the data, as described in Resync a Member of a Self-Managed Replica Set.
Use使用rs.printReplicationInfo()to check the current state of replica set members with regards to the oplog.rs.printReplicationInfo()检查副本集成员相对于oplog的当前状态。
For background on replication deployment patterns, see the Replica Set Deployment Architectures document.有关复制部署模式的背景,请参阅副本集部署体系结构文档。
Add a Member to an Existing Replica Set向现有副本集添加成员
Important
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.为避免因IP地址更改而进行配置更新,请使用DNS主机名而不是IP地址。在配置副本集成员或分片集群成员时,使用DNS主机名而不是IP地址尤为重要。
Use hostnames instead of IP addresses to configure clusters across a split network horizon. Starting in MongoDB 5.0, nodes that are only configured with an IP address fail startup validation and do not start.使用主机名而不是IP地址来配置跨拆分网络范围的集群。从MongoDB 5.0开始,仅配置了IP地址的节点无法启动验证,也无法启动。
Start the new
mongodinstance. Specify the data directory and the replica set name. The following example specifies the/srv/mongodb/db0data directory and thers0replica set:mongod --dbpath /srv/mongodb/db0 --replSet rs0 --bind_ip localhost,<hostname(s)|ip address(es)>Warning
Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist for Self-Managed Deployments.在将实例绑定到可公开访问的IP地址之前,必须保护集群免受未经授权的访问。有关安全建议的完整列表,请参阅自我管理部署的安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证并加强网络基础设施。For more information on configuration options, see the有关配置选项的更多信息,请参阅mongodmanual page.mongod手册页。Note
Optional可选的You can specify the data directory, replica set name, and the IP binding in the您可以在mongod.confconfiguration file, and start themongodwith the following command:mongod.conf配置文件中指定数据目录、副本集名称和IP绑定,并使用以下命令启动mongod:mongod --config /etc/mongod.confConnect 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. Pass themember configuration documentto the method. For example, to add a member at hostmongodb3.example.net, issue the following command:rs.add()将新成员添加到副本集中。将成员配置文档传递给方法。例如,要在主机mongodb3.example.net上添加成员,请发出以下命令:rs.add( { host: "mongodb3.example.net:27017" } )Warning
Before 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,并且添加了一个votesandprioritysettings 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(优先级)设置大于零的secondary成员,这可能会导致大多数投票成员在线,但无法选出primary成员。To avoid such situations, consider adding the new secondary initially with为了避免这种情况,请考虑在初始添加priority :0andvotes :0.priority :0、votes :0的新secondary。Then, run然后,运行rs.status()to ensure the member has transitioned intoSECONDARYstate.rs.status()以确保成员已转换为SECONDARY状态。Finally, use最后,使用rs.reconfig()to update its priority and votes.rs.reconfig()更新其优先级和投票。