Terry : Deploy a Replica Set

Deploy a MongoDB Replica Set

Overview

Three member replica sets provide enough redundancy to survive most network partitions and other system failures. Additionally, these sets have sufficient capacity for many distributed read operations. Most deployments require no additional members or configuration.

Requirements

Most replica sets consist of two or more mongod instances. [1] This tutorial describes a three member set. Production environments should have at least three distinct systems so that each system can run its own instance of mongod. For development systems you can run all three instances of the mongod process on a local system or within a virtual instance. For production environments, you should maintain as much separation between members as possible. For example, when using virtual machines for production deployments, each member should live on a separate host server, served by redundant power circuits and with redundant network paths.

NOTE: To ensure smooth elections always design replica sets with odd numbers of members. Use Arbiters to ensure the set has odd number of voting members and avoid tied elections.

Deploy a Development or Test Replica Set

The examples in this procedure create a new replica set named rs0 (3 mongod instances on 1 single virtual machine).

1. Before creating your replica set, verify that every member can successfully connect to every other member. The network configuration must allow all possible connections between any two members. To test connectivity, see Test Connections Between all Members.

2. Start three instances of mongod as members of a replica set named rs0, as described in this step, use tmux or screen.

Create the necessary data directories by issuing a command similar to the following

# Crete dbpath for each mongod instance
mkdir -p /srv/mongodb/rs{0-0,0-1,0-2}
# Change ownership
chown mongodb:mongodb /srv/mongodb/rs* -R

Start each mongod instance against its own dbpath

# Start each mongod against its own dbpath
mongod --port 27017 --dbpath /srv/mongodb/rs0-0 --replSet rs0
mongod --port 27018 --dbpath /srv/mongodb/rs0-1 --replSet rs0
mongod --port 27019 --dbpath /srv/mongodb/rs0-2 --replSet rs0

This starts each instance as a member of a replica set named rs0, each running on a distinct port. If you are already using these ports, you can select different ports. See the documentation of the following options for more information: --port, --dbpath, and --replSet.

3. Open a mongo shell and connect to the first mongod instance, with the following command

mongod --port 27017

4. Create a replica set configuration object in the mongo shell environment to use to initiate the replica set with the following sequence of operations

rsconf = {
           _id: "rs0",
           members: [
                      {
                       _id: 0,
                       host: "localhost:27017"
                      }
                    ]
         }

5. Use rs.initiate() to initiate a replica set consisting of the current member and using the default configuration

rs.initiate( rsconf )

6. Display the current replica configuration

rs.conf()

7. Add the second and third mongod instances to the replica set using the rs.add() method. Replace <hostname> with localhost in this case

rs.add("localhost:27018")
rs.add("localhost:27019")

After these commands return you have a fully functional replica set. New replica sets elect a primary within a few seconds.

8. Check the status of your replica set at any time with the rs.status() operation.

Deploy a Replica Set in Production

See Deploy a Replica Set in Production for details

Reference

Deploy a Replica Set