MongoDB Replica Set: 3 Easy Methods to Replicate Data

on Database Management Systems, ETL, MongoDB, Tutorials • October 7th, 2021 • Write for Hevo

MongoDB Replica Set

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.

Table of Contents

What is MongoDB?

MongoDB Replica Set: Logo | Hevo Data

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.
  • Horizontal Scalability: It is possible with MongoDB’s sharding. The process of distributing data over numerous servers is known as sharding. The Shard Key is used to partition a huge quantity of data into data chunks, which are then uniformly dispersed among Shards that span several Physical Servers.
  • Replication: MongoDB provides high availability of data by generating several copies of the data and sending these copies to a separate Server, allowing the data to be retrieved even if one Server fails.

Replicate MongoDB Data in Minutes using Hevo’s Data Pipelines

Hevo can be your go-to tool if you’re looking for Data Replication from 100+ Data Sources (including 40+ Free Data Sources) like MongoDB into Redshift, Google BigQuery, Databricks, Snowflake, and many other databases and warehouse systems. Hevo supports real-time replication for MongoDB, it also supports Changestreams mode, hence you can replicate in real-time. To further streamline and prepare your data for analysis, you can process and enrich Raw Granular Data using Hevo’s robust & built-in Transformation Layer without writing a single line of code!

In addition, Hevo’s native integration with BI & Analytics Tools will empower you to mine your replicated data to get actionable insights. With Hevo as one of the best MongoDB Replication tools, replication of data becomes easier.

Try our 14-day full access free trial today!

Get Started with Hevo for Free

What is Replication in MongoDB?

MongoDB Replica Sets | Hevo Data

It is the process of making data available across multiple data servers. 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. The secondary node automatically copies the changes made to the primary, ensuring the same data is maintained across all servers. During the time of a failover or recovery process, the election mechanism chooses the new primary node. Replicating data from MongoDB can be a tiresome task without the right set of tools. Hevo’s automated platform empowers you with everything you need to have a smooth Data Replication, Collection, and Processing experience.

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.
  • Availability: With replication in place, there’s no need to worry about data failures. In situations where your primary source of data fails, you can easily access the same up-to-date data from a secondary reserve. This highly promotes data availability.

For further information on replication in MongoDB, you can check the official documentation here.

Download the Ultimate Guide on Database Replication
Download the Ultimate Guide on Database Replication
Download the Ultimate Guide on Database Replication
Learn the 3 ways to replicate databases & which one you should prefer.

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.

What makes Hevo’s Data Replication Experience Best in Class?

Replicating data can be a tiresome task without the right set of tools. Hevo’s automated platform empowers you with everything you need to have a smooth Data Replication, Collection, and Processing experience. Our platform has the following in store for you!

  • Exceptional Security: A Fault-tolerant Architecture that ensures Zero Data Loss.
  • Real-Time Replication: Hevo supports real-time replication for MongoDB, it also supports Changestreams mode, hence you can replicate in real-time.
  • Built to Scale: Exceptional Horizontal Scalability with Minimal Latency for Modern-data Needs.
  • Built-in Connectors: Support for 100+ Data Sources, including Databases such as MongoDB, SaaS Platforms, Files & More. Native Webhooks & REST API Connector available for Custom Sources.
  • Incremental Data Load: Hevo allows the transfer of data that has been modified in real-time. This ensures efficient utilization of bandwidth on both ends.
  • Auto Schema Mapping: Hevo takes away the tedious task of schema management & automatically detects the format of incoming data and replicates it to the destination schema. You can also choose between Full & Incremental Mappings to suit your Data Replication requirements.
  • Blazing-fast Setup: Straightforward interface for new customers to work on, with minimal setup time.
  • Live Support: The Hevo team is available round the clock to extend exceptional support to its customers through chat, email, and support calls.
Sign up here for a 14-Day Free Trial!

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.

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.

visit our website to explore hevo

Hevo Data, a No-code Data Pipeline can help you replicate data from various sources to your desired destination in real-time, without having to write any code. It also helps you transform & enrich your data, & make it analysis-ready so that you can focus only on your key business needs and perform insightful analysis using BI tools. Hevo being a fully-managed system provides a highly secure automated solution to help perform replication in just a few clicks using its interactive UI.

Want to give Hevo a spin? sign up here for a 14-day free trial and experience the feature-rich Hevo suite first hand. Have a look at our unbeatable pricing, that will help you choose the right plan for you.

Why don’t you share your experience of setting up MongoDB Replica Sets in the comments? We would love to hear from you!

No-code Data Pipeline For MongoDB