rs.add()
On this page本页内容
Definition定义
rs.add(host, arbiterOnly)
-
Adds a member to a replica set. To run the method, you must connect to the primary of the replica set.将成员添加到副本集中。若要运行该方法,必须连接到复制副本集的primary。Parameter参数Type类型Description描述host
string or document The new member to add to the replica set. Specify either as a string or a configuration document:要添加到复制副本集中的新成员。指定为字符串或配置文档:If a document, specify a replica set member configuration document as found in the如果是文档,请指定在members
array.members
数组中找到的副本集成员配置文档。You must specify the您必须在成员配置文档中指定host
field in the member configuration document.host
字段。{
_id: <int>,
host: <string>, // required
arbiterOnly: <boolean>,
buildIndexes: <boolean>,
hidden: <boolean>,
priority: <number>,
tags: <document>,
secondaryDelaySecs: <int>,
votes: <number>
}For a description of the configuration field, refer to有关配置字段的说明,请参阅members
.members
。If a string, specify the hostname and optionally the port number for the new member.如果是字符串,请指定新成员的主机名和端口号(可选)。
arbiterOnly
boolean Optional.可选的。Applies only if the仅当<host>
value is a string.<host>
值为字符串时适用。If如果为true
, the added host is an arbiter.true
,则添加的主机是一个仲裁器。rs.add()
provides a wrapper around some of the functionality of the提供了replSetReconfig
database command and the correspondingmongosh
helperrs.reconfig()
.replSetReconfig
数据库命令和相应的mongosh
助手rs.reconfig()
的一些功能的包装。See the Replica Set Configuration document for full documentation of all replica set configuration options.有关所有副本集配置选项的完整文档,请参阅副本集配置文档。
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. mongosh
和复制集或分片集群的其他成员。Remote clients cannot connect to binaries that are bound only to localhost.远程客户端无法连接到仅绑定到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. Starting in MongoDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.使用主机名而不是IP地址来跨拆分的网络范围配置群集。从MongoDB 5.0开始,只配置了IP地址的节点将无法通过启动验证,也不会启动。
Behavior行为
在某些情况下,rs.add()
can, in some cases, trigger an election for primary which will disconnect the shell (such as adding a new member with a higher priority than the current primary). rs.add()
可以触发primary的选择,这将断开shell的连接(例如添加一个优先级高于当前primary的新成员)。In such cases, 在这种情况下,即使操作成功,mongosh
may display an error even if the operation succeeds.mongosh
也可能显示错误。
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,并且添加了一个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()
更新其优先级和投票。
Example实例
Add a Secondary to a New Replica Set将辅助副本添加到新副本集中
To add a new secondary member with default vote and priority settings to a new replica set, you can call the 要将具有默认投票和优先级设置的新辅助成员添加到新副本集中,可以使用以下命令调用rs.add()
method with:rs.add()
方法:
Member Configuration Document成员配置文档rs.add( { host: "mongodbd4.example.net:27017" } )
Host name主机名rs.add( "mongodbd4.example.net:27017" )
Add a Secondary to an Existing Replica Set将辅助副本添加到现有副本集中
Add a new secondary member with default vote and priority settings to an existing replica set:将具有默认投票和优先级设置的新辅助成员添加到现有复制副本集中:
rs.add( { host: "mongodbd4.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 如果您运行的MongoDB版本早于5.0,并且添加了一个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()
更新其优先级和投票。
Add a Priority 0 Member to a Replica Set将优先级为0的成员添加到副本集中
The following operation adds a 以下操作添加一个mongod
instance, running on the host mongodb4.example.net
and accessible on the default port 27017
, as a priority 0 secondary member:mongod
实例作为priority 0的辅助成员,该实例运行在主机mongodb4.example.net
上,可在默认端口27017
上访问:
rs.add( { host: "mongodbd4.example.net:27017", priority: 0 } )
You must specify the 您必须在成员配置文档中指定members[n].host
field in the member configuration document.members[n].host
字段。
See the 有关可用副本集成员配置设置,请参阅members
for the available replica set member configuration settings.members
。
Add an Arbiter to a Replica Set将仲裁器添加到副本集中
The following operation adds a 以下操作添加了一个mongod
instance, running on the host mongodb3.example.net
and accessible on the default port 27017
as an arbiter:mongod
实例,该实例在主机mongodb3.example.net
上运行,并可作为仲裁器在默认端口27017
上访问:
Member Configuration Document成员配置文档rs.add( { host: "mongodb3.example.net:27017", arbiterOnly: true } )
Host name主机名rs.add("mongodb3.example.net:27017", true)
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
回滚的可能性:
- MongoDB 3.4.1
- MongoDB 3.4.0
- MongoDB 3.2.11 or earlier
See Replica Set Protocol Version.请参阅副本集协议版本。
See also:另请参阅: