On this page本页内容
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.有关复制部署模式的背景信息,请参阅副本集部署体系结构文档。
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
.
In production deployments you can configure a init script to manage member processes.在生产部署中,您可以配置init脚本来管理成员进程。
You can use these procedures to add new members to an existing set. 可以使用这些过程将新成员添加到现有集合。You can also use the same procedure to "re-add" a removed member. 也可以使用相同的过程“重新添加”已删除的成员。If the removed member's data is still relatively recent, it can recover and catch up easily.如果删除的成员的数据仍然相对较新,则可以轻松恢复并赶上。
If you have a backup or snapshot of an existing member, you can move the data files (e.g. 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.有关详细信息,请参阅使用文件系统快照备份和恢复文档。
Always use filesystem snapshots to create a copy of a member of the existing replica set. 始终使用文件系统快照创建现有副本集成员的副本。Do not use 不要使用mongodump
and mongorestore
to seed a new replica set member.mongodump
和mongorestore
来播种新的副本集成员。
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured 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. mongosh
和副本集或分片集群的其他成员。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 要覆盖默认绑定并绑定到其他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 nodes that are only configured with an IP address fail startup validation and report an error. 从MongDB 5.0开始,仅使用IP地址配置的拆分地平线DNS节点无法通过启动验证并报告错误。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
实例绑定到本地主机和主机名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 MongDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.从MongDB 5.0开始,仅使用IP地址配置的节点将无法通过启动验证,无法启动。
Otherwise, use the MongoDB installation tutorial and the Deploy a Replica Set tutorials.否则,请使用MongoDB安装教程和部署副本集教程。
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()
检查副本集成员相对于oplog的当前状态。
For background on replication deployment patterns, see the Replica Set Deployment Architectures document.有关复制部署模式的背景信息,请参阅副本集部署体系结构文档。
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 MongDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.从MongDB 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 the rs0
replica set:/srv/mongodb/db0
数据目录和rs0
副本集:
mongod --dbpath /srv/mongodb/db0 --replSet rs0 --bind_ip localhost,<hostname(s)|ip address(es)>
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured 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
手册页面。
You can specify the data directory, replica set name, and the ip binding in the 您可以在mongod.conf
configuration file, and start the mongod
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" } )
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 如果您运行的是早于5.0的MongoDB版本,并且添加了votes
and priority
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
and votes :0
. priority :0
、votes :0
的新二级。Then, run 然后,运行rs.status()
to ensure the member has transitioned into SECONDARY
state. rs.status()
以确保成员已转换为SECONDARY
状态。Finally, use 最后,使用rs.reconfig()
to update its priority and votes.rs.reconfig()
更新其优先级和投票。