Docs HomeMongoDB Manual

rs.add()

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描述
hoststring or documentThe 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.如果是字符串,请指定新成员的主机名和端口号(可选)。
arbiterOnlybooleanOptional.可选的。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 corresponding mongosh helper rs.reconfig(). 提供了replSetReconfig 数据库命令和相应的mongosh助手rs.reconfig()的一些功能的包装。See the Replica Set Configuration document for full documentation of all replica set configuration options.有关所有副本集配置选项的完整文档,请参阅副本集配置文档。

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. 有关安全建议的完整列表,请参阅安全检查表At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证强化网络基础设施

MongoDB binaries, mongod and mongos, bind to localhost by default. MongoDB二进制文件mongodmongos默认绑定到localhostIf 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 mongod and mongos that are bound to localhost only accept connections from clients that are running on the same computer. 默认情况下,绑定到localhost的mongodmongos只接受来自在同一台计算机上运行的客户端的连接。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 net.bindIp configuration file setting or the --bind_ip command-line option to specify a list of hostnames or IP addresses.要覆盖默认绑定并绑定到其他IP地址,请使用net.bindIp配置文件设置或--bind_ip命令行选项指定主机名或IP地址列表。

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. 从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实例绑定到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 198.51.100.1:为了连接到此实例,远程客户端必须指定主机名或其关联的IP地址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. 若要避免由于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也可能显示错误。

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 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. 如果您运行的MongoDB版本早于5.0,并且添加了一个votespriority设置大于零的次要成员,这可能会导致大多数投票成员都在线,但无法选出主要成员。To avoid such situations, consider adding the new secondary initially with priority :0 and votes :0. 为了避免出现这种情况,请考虑添加新的次级,初始priority :0votes :0Then, 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" } )
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 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. 如果您运行的MongoDB版本早于5.0,并且添加了一个votespriority设置大于零的次要成员,这可能会导致大多数投票成员都在线,但无法选出主要成员。To avoid such situations, consider adding the new secondary initially with priority :0 and votes :0. 为了避免出现这种情况,请考虑添加新的次级,初始priority :0votes :0Then, 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, pv1 increases the likelihood of w:1 rollbacks compared to pv0 (no longer supported in MongoDB 4.0+) for replica sets with arbiters:对于以下MongoDB版本,与带有仲裁器的副本集的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:另请参阅: