Data replication is a must to keep your data protected, ensure high data availability, and ease of access, especially in the event of any unexpected error such as a system crash, hardware or software-based error, etc.

Replication in MongoDB takes place with the help of MongoDB Replica Sets, each of which contains a set of MongoDB instances.

The present article aims at providing a step-by-step guide to help you create a MongoDB Replica Set easily and execute data replication for your MongoDB instances. A complete walkthrough of the content will help you develop the skill to set up MongoDB Replica Sets using various methods.

What is MongoDB?

MongoDB is a high-performance document-oriented database that is powered by a NoSQL structure. It makes use of collections (tables) each having multiple documents (records) & allows the user to store data in a non-relational format. 

MongoDB stores its data as objects which are commonly identified as documents. These documents are stored in collections, analogous to how tables work in relational databases. MongoDB is known for its scalability, ease of use, reliability & no compulsion for using a fixed schema among all stored documents, giving them the ability to have varying fields (columns).

Key Features of MongoDB

  • Schema-less Database: A Schema-Less Database provides for the storage of many sorts of documents in a single collection (the equivalent of a table). In other words, a single collection in the MongoDB database can hold numerous documents, each with a distinct amount of Fields, Content, and Size. It is not required that one document be comparable to another, as is the case with Relational Databases. MongoDB provides customers with a lot of freedom because of this capability.
  • Index-based Document: In a MongoDB database, every field in the Document is indexed with Primary and Secondary Indices, making it easier to get data from the pool.

What is Replication in MongoDB?

MongoDB Replica Sets | Hevo Data

Data replication ensures data security not only in the case of a single server failure but also in the case of a hardware failure. Replication stores multiple data copies across various data servers, allowing users to recover data whenever required.

MongoDB supports replication with the help of Replica Sets. These Mongodb Replica Sets are a combination of various mongod instances, each of them having a single primary node and multiple secondary nodes.

Key features of replication:

  • Scalability: As the data volume increases the complexity of accessing data and working with data also increases. With replication in place, multiple data copies are available, allowing users to not only increase their data reserves but also recover any previous version in case of any errors or failures.
  • Performance: When data is available across multiple machines and servers, it not only makes accessing data easier but also makes recovering from unexpected and sudden failures much easier. replication ensures data availability and security at all times.

Why do you need Replication?

Replication is synchronizing data across multiple servers or storage space. It gives redundant data and allows users to store multiple copies of data in different servers to make it available all the time. Replication avoids data losses in events of uncertainty by making copies. Replication is needed because of the following points listed below:

  • It ensures data safety and keeps data safe on multiple servers.
  • Replication of data means less traffic on one server and it makes data available all the time. 
  • Keeping copy data allow users to recover data from any of the secondary server if any disaster occurs to prevent any data loss.
  • If there is any server with system failure or downtime for maintenance or indexing then another server can be used to deliver data.

How does Replication Work in MongoDB?

MongoDB uses replica sets to achieve replication. A MongoDB replica set is a collection of MongoDB instances that all store identical 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 nodes that are identical (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 done automatically.
  • Primary election by consensus.

Example

mongod --port 27017 --dbpath "D:set upmongodbdata" --replSet rs0
  • On port 27017, it will launch a mongod instance with the name rs0.
  • Connect to this mongod instance using the command prompt.
  • To start a new replica set, use the Mongo client function rs.initiate().
  • Use the command rs.conf() to inspect the replica set setup (). Use the command rs.status() to verify the status of a replica set.

Prerequisites

  • Working knowledge of MongoDB.
  • MongoDB is installed at the host workstation.
  • A general idea of using the command-line.

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

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

Step 1: Starting the mongod Instances

To start the mongod instance, specify the port value for your Mongo instance along with the path to your MongoDB installation on your system. Use the following command to enable your mongod instance:

$ mongod --port 27017 --dbpath /var/lib/mongodb --replSet replicaSet1

MongoDB uses the port 27017 as its default port. In case the user doesn’t specify the port number, MongoDB automatically establishes a connection with the default port.

The –repelset parameter specifies the name of the Replica Set, which will store various MongoDB instances. Here the replica set is called replicaSet1.

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 as follows:

MongoDB Replica Set: IP address for nodes | Hevo Data

You can also connect them by using the following lines of code:

mongo –host node-2 –port 27017
mongo –host node-3 –port 27017

To establish a complete connection, you need to use these commands once with every server by changing the server/nodes as required.

Step 3: Enabling Replication in MongoDB

Once you’ve made all the configurations, you now need to open the Mongo Shell with your primary instance and use initiate command as follows:

rs.initiate()

You will now see the following output on your system. This indicates that the replication process has begun:

MongoDB shell version v3.4.10
connecting to: mongodb://172.34.21.121:27017
MongoDB server version: 3.4.10
> rs.initiate()
{
"info2": "no configuration specified. Using a default configuration for the set",
"me" : "example:27017",
"ok" : 1

This is how you can use the rs.initiate() command to start the replication process. The Mongo Shell will now change its prompt to the name of your  Replica Set, replicaSet1.

Step 4: Adding MongoDB Instances to Replica Sets

Once you’ve initialised your Replica Set, you can now begin to add the various MongoDB instances to it using the add command as follows:

rs.add(<servername:port>)

For example, if you want to add the node-2 and node-3 to the Replica Set replicaSet1, you can use the following command in your Mongo Shell:

rs.add('node-2:27017')
rs.add('node-3:27017')
Adding new nodes to Replica Set

The output {‘ok’:1} indicates that a MongoDB instance has been successfully added to the Replica Set. To check the status of the replication, you can use the status command as follows:

rs.status()

The status command will produce the following output if the MongoDB instances have been added successfully:

MongoDB Replica Set: Replica Set status | Hevo Data

This is how you can add MongoDB instances to your Replica Sets.

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:

db.shutdownserver

Once you’ve shut down the server, you need to connect with your primary server and use the remove command as follows:

rs.remove("server_name")

For example, if you want to remove the “node-2” instance from replicaSet1, you can use the following command:

rs.remove("node-2")

This is how you can remove a MongoDB instance from your Replica Set.

Step 6: Testing the Replication Process

You can test the process by adding a document in the primary node. If replication is working properly, the document will automatically be copied into the secondary node.

First, connect to the primary node and add a document using the insertOne command as follows:

replicaSet1: PRIMARY> use movies
switched to db movies
replicaSet1:PRIMARY> db.release_year.insertOne({ name: "Example", year: 2017})
{
"acknowledged" : true,
"insertedId" : ObjectId("5a018ea7c89da78ba2076f25")
}
replicaSet1: PRIMARY>

Switch to your secondary node, node-2 using the following command:

mongo -host node-2 -port  27017

If replication has taken place properly, the document in your primary node will automatically be copied to the secondaries. You can check this using the find command as follows:

replicaSet1: SECONDARY> use movies
switched to db movies
replicaSet1:SECONDARY> db.release_year.find();

The find command will access the database movies and the collection release_year and output the stored documents as follows: 

{ "_id" : ObjectId("5a018ea7c89da78ba2076f25"), "name" : "Example", "year" : 2017 }
resplicaSet1:SECONDARY>

The above output indicates that the document was automatically replicated into the secondary node. This is how you can easily set up a MongoDB Replica Set and perform various operations on it.

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.

First, shut down your mongod instance using the following command:

db.adminCommand({"shutdown":"1"})

Restart your mongod instance. You must use the –repelSet parameter in your command to specify the Replica Set you’re going to use. Users must specify the name of the server along with their unique IPs in their command as follows:

mongod --port 27017 --dbpath /var/lib/mongodb  --replSet replicaSet1 --bind_ip localhost,<hostname(s)|ip address(es)>

Connect your Mongo Shell with your MongoDB instance and use the initiate command to start the replication process and successfully convert your standalone MongoDB instance into a Replica Set.

You can now perform all the basic operations such as adding an instance, removing an instance, etc. using the following commands:

rs.add(“<host_name:port>”) & rs.remove(“host-name”)

You can even check the status of your MongoDB Replica Set, using the rs.status() and rs.conf() commands.

This is how you can convert your standalone MongoDB instance into a MongoDB Replica Set.

Method 3: Setting up a MongoDB Replica Set for Testing & Development

With replication in MongoDB, you can deploy Replica Sets in the testing and development environments. You can set up Replica Sets for testing either 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. If you want to set up with access control enabled, you can check the official documentation here.

This can be implemented using the following steps:

Step 1: Creating Data Directories for MongoDB Instances

You can easily create directories for all the instances that are a part of the Replica Set using the following command:

mkdir -p /srv/mongodb/rs0-0  /srv/mongodb/rs0-1 /srv/mongodb/rs0-2

This command will create directories for three MongoDB instances rs0-0, rs0-1, and rs0-2. 

Step 2: Initiating Replication in MongoDB

Once you’ve created directories for your Replica Set members, you can start the MongoDB instances for each of them using the following set of commands:

Use the following command for Server 1:

mongod --replSet rs0 --port 27017 --bind_ip localhost,<hostname(s)|ip address(es)> --dbpath /srv/mongodb/rs0-0  --oplogSize 128

Use the following command for Server 2:

mongod --replSet rs0 --port 27018 --bind_ip localhost,<hostname(s)|ip address(es)> --dbpath /srv/mongodb/rs0-0  --oplogSize 128

Use the following command for Server 3:

mongod --replSet rs0 --port 27019 --bind_ip localhost,<hostname(s)|ip address(es)> --dbpath /srv/mongodb/rs0-0  --oplogSize 128

The –oplogSize parameter is used to prevent the machine from getting overloaded during the test phase. It helps reduce the amount of disk space each disk consumes.

Connect to one of the various MongoDB instances that are a part of your ReplicaSet using the Mongo Shell. You can use the following command to connect using the port number:

mongo --port 27017

Use the rs.initiate() command to start the replication process and create a configuration object as follows by replacing the hostname parameter with your system name:

rs conf = {
  _id: "rs0",
  members: [
    {  _id: 0,  host: "<hostname>:27017},
    {  _id: 1,  host: "<hostname>:27018"},
    {  _id: 2,  host: "<hostname>:27019"}
   ] }

Pass the configuration object file as the parameter for the initiate command and use it as follows:

rs.initiate(rsconf)

This is how you can create a MongoDB Replica Set for testing and development purposes.

For further information on creating Replica Sets in MongoDB, you can check the official documentation here.

Adding Members to Replica Set

Start mongod instances on various machines to add members to the replica set. Start a mongo client and use rs.add() command.

The rs.add() command has the following basic syntax:

>rs.add(HOST_NAME:PORT)

Example:

Assume mongod1.net is your mongod instance, and it’s listening on port 27017. Use the Mongo client command rs.add() to add this instance to the replica set.

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

Only after you’re connected to the primary node can you add a mongod instance to the replica set. Use the mongo client command db.isMaster() to see if you’re connected to primary or not.

Helpful Resources on MongoDB Replica Set

Conclusion

This article teaches you how to set up MongoDB Replica Sets with ease and answers all your queries regarding it.

It provides a brief introduction of various concepts related to it & helps the users understand them better and use them to perform data replication & recovery in the most efficient way possible.

These methods, however, can be challenging especially for a beginner & this is where Hevo saves the day.

Divij Chawla
Marketing Operations and Analytics Manager, Hevo Data

Divij Chawla is interested in data analysis, software architecture, and technical content creation. With extensive experience driving Marketing Operations and Analytics teams, he excels at defining strategic objectives, delivering real-time insights, and setting up efficient processes. His technical expertise includes developing dashboards, implementing CRM systems, and optimising sales and marketing workflows. Divij combines his analytical skills with a deep understanding of data-driven solutions to create impactful content and drive business growth.

No-code Data Pipeline For MongoDB