To avoid data loss that may occure due to hardware failure or some service interruptions, you need to ensure your availability of database. to increase availability you can run the same instance of a database on different locations.

Replication is the process of synchronizing data across multiple servers. Replication provides redundancy and increases data availability with multiple copies of data on different database servers. Replication protects a database from the loss of a single server. Replication also allows you to recover from hardware failure and service interruptions. With additional copies of the data, you can dedicate one to disaster recovery, reporting, or backup.

In some cases, replication can provide increased read capacity as clients can send read operations to different servers.

Replica Set in MongoDB

A replica set is a group of mongodb instances that maintain the same data set. In a replica set, one node is primary node that receives all write operations. All other instances, such as secondaries, apply operations from the primary so that they have the same data set. Replica set can have only one primary node. There also is an optional arbiter node.

The primary records all changes to its data sets in its operation log (oplog). the secondaries replicate the primary's oplog and apply the operations to their data sets such that the secondaries' data sets reflect the primary's data set.

When the primary goes down, one of secondaries will hold an election to elect itself the new primary.

Arbiters do not maintain any data set. It's purpose is to maintain a share in a replica set by responding to heartbeat and election requests by other replica set members. So they can function with a cheaper resource than a fully functional replica set member. Arbiters stay arbiter during election, and changing in primary member does not affect the arbiter.


to create a replica set, you need at least three mongodb installed (on three servers). these servers need to communicate with each other.

1. Configuring Hosts

each member of replica set should have a hostname that identifies it as a member of the set. add the following lines to /etc/hosts file on each member.		mongorpl-01		mongorpl-02		mongorpl-03		mongorpl-arb

2. MongoDB Authentication

Members of a replica set must use the same authentication, so they can communicate with each other. we will create a key file that will be used to secure authentication between the members.

openssl rand -base64 756 > mongo-keyfile

copy this file to all members under /opt/mongodb directory and assign the correct permissions.

sudo mv ~/mongo-keyfile /opt/mongodb
sudo chmod 400 /opt/mongodb/mongo-keyfile
sudo chown mongod:mongod /opt/mongodb/mongo-keyfile

3. Create Admin User

On the server that you intend to use as the primary member, create an administrative user with root privileges.

    user: "mongo-admin", 
    pwd: "password", 
        {role: "root", db: "admin"}

4. Configure MongoDB

make the following changes to your /etc/mongod.conf file on each machine. change to your machine's ip.

	port: 27017
	keyFile: /opt/mongodb/mongo-keyfile
	replSetName: repl01

after changes, restart the mongod service.

sudo systemctl restart mongod

5. Start Replication and Add Member

log int to primary machine and connect to mongodb shell using the administrator user:

mongo -u mongo-admin -p --authenticationDatabase admin

start the replication:


you should an output like this:

    "info2" : "no configuration specified. Using a default configuration for the set",
    "me" : "",
    "ok" : 1

add other members to replica set:


check the configuration of replica set:


you should see the members and the replica set configurations.

for checking the replica set status you can call rs.status() function on your shell on each machine.

you can also add arbiter node by this command:


Automatic Failover

When a primary does not communicate with the other members of the set for more than the configured period (electionTimeoutMillis = 10 seconds by default), an eligible secondary calls for an election to nominate itself as the new primary. The cluster attempts to complete the election of a new primary and resume normal operations. The replica set cannot process write operations until the election completes successfully.