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 |
| |
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 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.有关所有副本集配置选项的完整文档,请参阅副本集配置文档。
Starting in MongoDB 3.6, MongoDB binaries, 从MongoDB 3.6开始,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地址。
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 当仅绑定到本地主机时,这些MongoDB 3.6二进制文件只能接受来自同一台计算机上运行的客户端(包括mongo shell、副本集和分片集群部署中的其他成员)的连接。mongo
shell, other members in your deployment for replica sets and sharded clusters) that are running on the same machine. Remote clients cannot connect to the binaries bound only to localhost.远程客户端无法连接到仅绑定到localhost的二进制文件。
To override and bind to other ip addresses, you can 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地址列表。
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
关联:
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
:
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地址更改而导致的配置更改。
在某些情况下,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.votes
和priority
设置大于零时,在其初始同步期间,该辅助服务器仍将计为投票成员,即使它无法提供读取服务,也无法成为主服务器,因为它的数据尚不一致。
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 :0
且votes :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 a new replica set, you can call the 要将具有默认投票和优先级设置的新辅助成员添加到新副本集中,可以使用以下方法调用rs.add()
method with:rs.add()
方法:
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.votes
和priority
设置大于零时,在其初始同步期间,该辅助服务器仍将计为投票成员,即使它无法提供读取服务,也无法成为主服务器,因为它的数据尚不一致。
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 :0
且votes :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:要将具有默认投票和优先级设置的新辅助成员添加到现有副本集中,请执行以下操作:
SECONDARY
state. SECONDARY
状态。rs.status()
:rs.status()
:
where 其中,n
is the array index of the new member in the members
array.n
是members
数组中新成员的数组索引。
Warning
rs.reconfig()
shell method can force the current primary to step down, which causes an election. rs.reconfig()
shell方法可以强制当前主进程退出,从而导致选举。mongod
closes all client connections. mongod
会关闭所有客户端连接。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的辅助成员:
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
上访问:
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:另请参见: