Mongo DB as Replica Set


Mongo DB as Replica Set

MongoDB uses replica sets to achieve replication. A replica set is a collection of MongoDB instances comparable to store data. One node is the primary node in a replica, and it gets all write operations. All other instances, such as secondaries, use the primary’s actions to ensure they have the same data set. There can only be one primary node in a replica set.

  • A replica set is a collection of two or more identical nodes (generally, a minimum of 3 nodes are required).
  • One node in a replica set is the major node, while the others are secondary.
  • From the primary to the secondary node, all data is replicated.
  • Election for primary is established during automatic failover or maintenance, and a new primary node is elected.
  • After a failed node is recovered, it rejoins the replica set and becomes a secondary node.

Replica Set Features

  • N nodes in a cluster.
  • Any node can be the primary.
  • All write operations are sent to the primary storage.
  • Failover occurs automatically.
  • Recovery is made automatically.
  • Primary election by consensus

Methods to set up MongoDB Replica Sets

MongoDB lets users conveniently replicate their data using Replica Sets and use them for various purposes such as testing and development, production, etc. You can use the following methods to create and deploy your MongoDB Replica Sets:

  • Method 1: Setting up a new MongoDB Replica Set
  • Method 2: Converting a standalone instance to a MongoDB Replica Set
  • Method 3: Setting up a MongoDB Replica Set for testing & development

Method 1: Setting up a new MongoDB Replica Set

We can set up a new Replica Set in MongoDB using the following steps:

  • Step 1: Starting the mongod Instances
  • Step 2: Configuring the Replica Set
  • Step 3: Enabling Replication in MongoDB
  • Step 4: Adding MongoDB Instances to Replica Sets
  • Step 5: Removing Instances from Replica Sets
  • Step 6: Testing the Replication Process

    Step 1: Starting the MongoDB Instances

    To start the MongoDB instance, specify the port value for your Mongo instance and the path to your MongoDB installation on your system.

    step 2: Configuring the Replica Set

    A Replica Set contains multiple instances that communicate with each other. To establish communication between them, you need to specify the hostname along with their IPs

    Step 3: Enabling Replication in MongoDB

    Once you've made all the configurations, you need to open the Mongo Shell with your primary instance and use the initiate command.

    Step 4: Adding MongoDB Instances to Replica Sets

    Once you’ve initialized your Replica Set, you can now begin to add the various MongoDB instances to it using the add command.

    Step 5: Removing Instances from Replica Sets

    MongoDB Replica Sets also allows users to remove single or multiple instances they’ve added to the Replica set using the remove command. To remove a particular instance, you first need to shut it down using the following command:

    Step 6: Testing the Replication Process

    You can test the process by adding a document to the primary node. The document will automatically be copied into the secondary node if replication is working properly.

Method 2: Converting a standalone instance to a MongoDB Replica Set

MongoDB allows its users to transform their standalone MongoDB instances into a Replica Set. Standalone instances are used in the testing and development process, whereas the Replica Sets are a part of the production phase.

Method 3: Setting up a MongoDB Replica Set for testing & development

With replication in MongoDB, you can deploy Replica Sets in testing and development environments. You can set up Replica Sets for testing with access control enabled or disabled. In this method, you'll set up a Replica Set with access control disabled for deploying it in a testing environment.