On this page本页内容
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。
host | string or document |
|
arbiterOnly | boolean | <host> value is a string. <host>值为字符串时适用。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.有关所有副本集配置选项的完整文档,请参阅副本集配置文档。
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命令行选项,则该二进制文件还将绑定到本地主机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 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地址的节点将无法通过启动验证,无法启动。
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). 在某些情况下,可以触发初选,这将断开外壳(例如添加优先级高于当前初选的新成员)。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 如果您运行的是低于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()更新其优先级和投票。
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 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 如果您运行的是低于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设置大于零的secondary,这可能会导致大多数投票成员在线,但无法选出primary。To avoid such situations, consider adding the new secondary initially with 为避免出现这种情况,请考虑最初添加priority :0 and votes :0. priority :0、votes :0的新secondary。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()更新其优先级和投票。
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实例,该实例在主机mongodb4.example.net上运行,并可在默认端口27017上访问,作为优先级为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。
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回滚的可能性:
See Replica Set Protocol Version.请参见副本集协议版本。
See also:另请参见: