Home >>MongoDB Tutorial >MongoDB Replication

MongoDB Replication

Use of Replication in MongoDB

The process of synchronizing data through several servers is replication. With several copies of data on various database servers, replication provides consistency and improves the availability of data. Replication defends a database from a single server 's loss. Replication also helps you to recover from failure of hardware and interruptions of service. You may dedicate one to disaster recovery, reporting, or backup, with additional copies of the data.

Why Replication?

  • To keep your data safe
  • High (24*7) availability of data
  • Disaster recovery
  • No downtime for maintenance (like backups, index rebuilds, compaction)
  • Read scaling (extra copies to read from)
  • Replica set is transparent to the application

How Replication Works in MongoDB

By using the replica set, MongoDB achieves replication. A replica set is a group of instances of Mongod holding the same set of data. One node is a primary node in a replica that receives all writing operations. All other cases, such as secondary ones, apply primary operations so that they have the same collection of data. The replica set can only contain a single primary node.

  • The replica set is a group of two or more nodes (a minimum of 3 nodes is usually required).
  • One node is the primary node in a replica set and the remaining nodes are secondary.
  • It replicates all data from the primary to the secondary node.
  • At the time of automatic failover or maintenance, the primary option is established and a new primary node is selected.
  • It decides to join the replica collection again after the recovery of the failed node and acts as a secondary node.

A standard MongoDB replication diagram is shown in which the client application always interacts with the primary node and then replicates the data to the secondary nodes from the primary node.

MongoDB Replication

Replica Set Features

  • A cluster of N nodes
  • Any one node can be primary
  • All write operations go to primary
  • Automatic failover
  • Automatic recovery
  • Consensus election of primary

Set Up a Replica Set

We'll convert the standalone MongoDB instance to a replica set in this tutorial. To convert to the replica set, the steps below are

mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"
  • Shutdown The MongoDB server is already running.
  • Start the server on MongoDB by specifying the-- replSet option. The following is the simple replSet -- syntax

Example

mongod --port 27017 --dbpath "D:\set up\mongodb\data" --replSet rs0
  • This will initiate a Mongod instance on port 27017 with the name rs0.
  • Start the command prompt now and connect to this instance of the Mongod.
  • Use the rs.initiate() command in the Mongo client to initiate a new replica set.
  • To check the configuration of the replica set, issue the rs.conf() command. The rs.status() command is used to check the status of the replica set.

Add Members to Replica Set

Start the Mongod instances on multiple machines to add members to a replica set. Open a Mongo client now and issue an rs.add() command.

Syntax

The basic syntax of rs.add() command is as follows −

>rs.add(HOST_NAME:PORT)

Example

Suppose your name for the Mongod instance is mongod1.net and runs on port 27017. Use the rs.add() command in the Mongo client to add this instance to the replica set.

>rs.add("mongod1.net:27017")
>

You can only add the Mongod instance to the replica collection when you are connected to the primary node. Use the db.isMaster() command in the mongo client to check if you are connected to the primary or not.


No Sidebar ads