- Replication >
- Add Members to a Replica Set
Add Members to a Replica Set¶
On this page
Overview¶
This tutorial explains how to add an additional member to an existing replica set.
Before adding a new member, see the Adding Members topic in the Replica Set Operation and Management document.
For background on replication deployment patterns, see the Replica Set Architectures and Deployment Patterns document.
Requirements¶
- An active replica set.
- A new MongoDB system capable of supporting your dataset, accessible by the active replica set through the network.
If neither of these conditions are satisfied, please use the MongoDB installation tutorial and the Deploy a Replica Set tutorial instead.
Procedures¶
The examples in this procedure use the following configuration:
- The active replica set is
rs0
. - The new member to be added is
mongodb3.example.net
. - The
mongod
instance default port is27017
. - The
mongodb.conf
configuration file exists in the/etc
directory and contains the following replica set information:
For more information on configuration options, see Configuration File Options.
Add a Member to an Existing Replica Set¶
This procedure uses the above example configuration.
Deploy a new
mongod
instance, specifying the name of the replica set. You can do this one of two ways:Using the
mongodb.conf
file. On the primary, issue a command that resembles the following:Using command line arguments. On the primary, issue command that resembles the following:
Replace
/srv/mongodb/db0
with the path of yourdbpath
.Take note of the host name and port information for the new
mongod
instance.Open a
mongo
shell connected to the replica set’s primary:Note
The primary is the only member that can add or remove members from the replica set. If you do not know which member is the primary, log into any member of the replica set using
mongo
and issue thedb.isMaster()
command to determine which member is in theisMaster.primary
field. For example, on the system shell:Then in the
mongo
shell:If you are not connected to the primary, disconnect from the current client and reconnect to the primary.
In the
mongo
shell, issue the following command to add the new member to the replica set.Note
You can also include the port number, depending on your setup:
Verify that the member is now part of the replica set by calling the
rs.conf()
method, which displays the replica set configuration:You can use the
rs.status()
function to provide an overview of replica set status.
Add a Member to an Existing Replica Set (Alternate Procedure)¶
Alternately, you can add a member to a replica set by specifying an
entire configuration document with some or all of the fields in a
members
sub-documents. For example:
This configures a hidden member that is accessible at
mongodb3.example.net:27017
. See host
, priority
, and hidden
for more information about
these settings. When you specify a full configuration object with
rs.add()
, you must declare the _id
field, which is not
automatically populated in this case.
Production Notes¶
- In production deployments you likely want to use and configure a control script to manage this process based on this command.
- A member can be removed from a set and re-added later. If the
removed member’s data is still relatively fresh, it can recover and
catch up from its old data set. See the
rs.add()
andrs.remove()
helpers. - If you have a backup or snapshot of an existing member, you can move
the data files (i.e.
/data/db
ordbpath
) to a new system and use them to quickly initiate a new member. These files must be:- clean: the existing dataset must be from a consistent copy of the database from a member of the same replica set. See the Backup Strategies for MongoDB Systems document for more information.
- recent: the copy must more recent than the oldest operation in the primary member’s oplog. The new secondary must be able to become current using operations from the primary’s oplog.
- There is a maximum of seven voting members in any replica set. When adding
more members to a replica set that already has seven votes, you must
either:
- add the new member as a non-voting members or,
- remove votes from an
existing member
.