This tutorial explains how to add an additional member to an existing replica set.
Before adding a new member to an existing replica set, do one of the following to prepare the new member’s data directory:
Make sure the new member’s data directory does not contain data. The new member will copy the data from an existing member.
If the new member is in a recovering state, it must exit and become a secondary before MongoDB can copy all data as part of the replication process. This process takes time but does not require administrator intervention.
Manually copy the data directory from an existing member. The new member becomes a secondary member and will catch up to the current state of the replica set after a short interval. Copying the data over manually shortens the amount of time for the new member to become current.
Ensure that you can copy the data directory to the new member and begin replication within the window allowed by the oplog. If the difference in the amount of time between the most recent operation and the most recent operation to the database exceeds the length of the oplog on the existing members, then the new instance will have to perform an initial sync, which completely resynchronizes the data, as described in Resync a Member of a Replica Set.
Use db.printReplicationInfo() to check the current state of replica set members with regards to the oplog.
For background on replication deployment patterns, see the Replica Set Architectures and Deployment Patterns document.
If neither of these conditions are satisfied, please use the MongoDB installation tutorial and the Deploy a Replica Set tutorial instead.
The examples in this procedure use the following configuration:
port = 27017
bind_ip = 10.8.0.10
dbpath = /srv/mongodb/db0
logpath = /var/log/mongodb.log
fork = true
replSet = rs0
For more information on configuration options, see Configuration File Options.
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:
mongod --config /etc/mongodb.conf
Using command line arguments. On the primary, issue command that resembles the following:
mongod --dbpath /srv/mongodb/db0 --replSet rs0
Replace /srv/mongodb/db0 with the path of your dbpath.
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:
mongo
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 the db.isMaster() command to determine which member is in the isMaster.primary field. For example, on the system shell:
mongo mongodb0.example.net
Then in the mongo shell:
db.isMaster()
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.
rs.add("mongodb3.example.net")
Note
You can also include the port number, depending on your setup:
rs.add("mongodb3.example.net:27017")
Verify that the member is now part of the replica set by calling the rs.conf() method, which displays the replica set configuration:
rs.conf()
You can use the rs.status() method to view replica set status. For a description of the status fields, see replSetGetStatus.
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:
rs.add({_id: 1, host: "mongodb3.example.net:27017", priority: 0, hidden: true})
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.