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>,
       slaveDelay: <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 corresponding mongo shell helper rs.reconfig(). 围绕replSetReconfig数据库命令和相应的mongo shell帮助程序rs.reconfig()的一些功能提供包装。See the Replica Set Configuration document for full documentation of all replica set configuration options.有关所有副本集配置选项的完整文档,请参阅副本集配置文档。

IP Binding绑定

Starting in MongoDB 3.6, MongoDB binaries, mongod and mongos, bind to localhost by default. 从MongoDB 3.6开始,MongoDB二进制文件mongodmongos默认绑定到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地址。

Previously, starting from MongoDB 2.6, only the binaries from the official MongoDB RPM (Red Hat, CentOS, Fedora Linux, and derivatives) and DEB (Debian, Ubuntu, and derivatives) packages bind to localhost by default.以前,从MongoDB 2.6开始,默认情况下,只有官方MongoDB RPM(Red Hat、CentOS、Fedora Linux和衍生物)和DEB(Debian、Ubuntu和衍生物)包中的二进制文件绑定到localhost。

When bound only to the localhost, these MongoDB 3.6 binaries can only accept connections from clients (including the mongo shell, other members in your deployment for replica sets and sharded clusters) that are running on the same machine. 当仅绑定到本地主机时,这些MongoDB 3.6二进制文件只能接受来自同一台计算机上运行的客户端(包括mongo shell、副本集和分片集群部署中的其他成员)的连接。Remote clients cannot connect to the binaries bound only to localhost.远程客户端无法连接到仅绑定到localhost的二进制文件。

To override and bind to other ip addresses, you can 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警告

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 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

mongo --host My-Example-Associated-Hostname

mongo --host 198.51.100.1

Tip

When possible, use a logical DNS hostname instead of an ip address, particularly when configuring replica set members or sharded cluster members. 如果可能,请使用逻辑DNS主机名而不是ip地址,尤其是在配置副本集成员或分片集群成员时。The use of logical DNS hostnames avoids configuration changes due to ip address changes.使用逻辑DNS主机名可以避免由于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, the mongo shell may display an error even if the operation succeeds.在这种情况下,即使操作成功,mongo shell也可能显示错误。

Tip

When a newly added secondary has its votes and priority settings greater than zero, during its initial sync, the secondary still counts as a voting member even though it cannot serve reads nor become primary because its data is not yet consistent.当新添加的辅助服务器的votespriority设置大于零时,在其初始同步期间,该辅助服务器仍将计为投票成员,即使它无法提供读取服务,也无法成为主服务器,因为它的数据尚不一致。

This can lead to a case where a majority of the voting members are online but no primary can be elected. 这可能会导致大多数有投票权的成员在线,但无法选出初选成员。To avoid such situations, consider adding the new secondary initially with priority :0 and votes :0. 为了避免这种情况,请考虑最初添加priority :0votes :0的新辅助对象。Then, once the member has transitioned into SECONDARY state, use rs.reconfig() to update its priority and votes.然后,一旦成员转换到SECONDARY状态,使用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将辅助副本添加到现有副本集

Tip

When a newly added secondary has its votes and priority settings greater than zero, during its initial sync, the secondary still counts as a voting member even though it cannot serve reads nor become primary because its data is not yet consistent.当新添加的辅助服务器的votespriority设置大于零时,在其初始同步期间,该辅助服务器仍将计为投票成员,即使它无法提供读取服务,也无法成为主服务器,因为它的数据尚不一致。

This can lead to a case where a majority of the voting members are online but no primary can be elected. 这可能会导致大多数有投票权的成员在线,但无法选出初选成员。To avoid such situations, consider adding the new secondary initially with priority :0 and votes :0. 为了避免这种情况,请考虑最初添加priority :0votes :0的新辅助对象。Then, once the member has transitioned into SECONDARY state, use rs.reconfig() to update its priority and votes.然后,一旦成员转换到SECONDARY状态,使用rs.reconfig()更新其优先级和投票。

To add a new secondary member with default vote and priority settings to an existing replica set:要将具有默认投票和优先级设置的新辅助成员添加到现有副本集中,请执行以下操作:

  1. Add the member initially as a non-voting, priority 0 member:最初将成员添加为无投票权优先级为0的成员:

    rs.add( { host: "mongodbd4.example.net:27017", priority: 0, votes: 0 } )
  2. Ensure that the new member has reached SECONDARY state. 确保新成员已达到SECONDARY状态。To check the state of the replica set members, run rs.status():要检查副本集成员的状态,请运行rs.status()

    rs.status()
  3. Reconfigure the replica set to update the votes and priority of the new member:重新配置副本集以更新新成员的投票和优先级:

    var cfg = rs.conf();
    
    cfg.members[n].priority = 1;  // Substitute the correct array index for the new member
    cfg.members[n].votes = 1;     // Substitute the correct array index for the new member
    
    rs.reconfig(cfg)

    where n is the array index of the new member in the members array.其中,nmembers数组中新成员的数组索引。

Warning

  • The rs.reconfig() shell method can force the current primary to step down, which causes an election. rs.reconfig()shell方法可以强制当前主进程退出,从而导致选举When the primary steps down, the mongod closes all client connections. 当主服务器退出时,mongod会关闭所有客户端连接。While this typically takes 10-20 seconds, try to make these changes during scheduled maintenance periods.虽然这通常需要10-20秒,但请尝试在计划的维护期间进行这些更改。
  • Avoid reconfiguring replica sets that contain members of different MongoDB versions as validation rules may differ across MongoDB versions.避免重新配置包含不同MongoDB版本成员的副本集,因为不同MongoDB版本的验证规则可能不同。

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:以下操作将在主机mongodb4.example.net上运行并可在默认端口27017上访问的mongod实例添加为优先级为0的辅助成员:

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 earlierMongoDB 3.2.11或更早版本

See Replica Set Protocol Version.请参阅副本集协议版本

See also:另请参见: