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.
host |
string or document | The new member to add to the replica set. Specify either as a string or a configuration document:
|
arbiterOnly |
boolean | <host> value is a string. If true , the added host is an arbiter. |
rs.add()
provides a wrapper around some of the functionality of the replSetReconfig
database command and the corresponding mongo
shell helper rs.reconfig()
. See the Replica Set Configuration document for full documentation of all replica set configuration options.
Starting in MongoDB 3.6, MongoDB binaries, mongod
and mongos
, bind to localhost by default. 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.
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.
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. Remote clients cannot connect to the binaries bound only to 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.
Warning
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured your cluster from unauthorized access. 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
:
In order to connect to this instance, remote clients must specify the hostname or its associated ip address 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. The use of logical DNS hostnames avoids configuration changes due to ip address changes.
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, the mongo
shell may display an error even if the operation succeeds.
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.
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
. Then, once the member has transitioned into SECONDARY
state, use rs.reconfig()
to update its priority and votes.
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:
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.
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
. Then, once the member has transitioned into SECONDARY
state, use rs.reconfig()
to update its priority and votes.
To add a new secondary member with default vote and priority settings to an existing replica set:
SECONDARY
state. To check the state of the replica set members, run rs.status()
:
where n
is the array index of the new member in the members
array.
Warning
rs.reconfig()
shell method can force the current primary to step down, which causes an election. When the primary steps down, the mongod
closes all client connections. While this typically takes 10-20 seconds, try to make these changes during scheduled maintenance periods.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:
You must specify the members[n].host
field in the member configuration document.
See the members
for the available replica set member configuration settings.
The following operation adds a mongod
instance, running on the host mongodb3.example.net
and accessible on the default port 27017
as an arbiter:
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:
See Replica Set Protocol Version.
See also: