Setting up a MongoDB replica set might sound daunting, but it doesn’t have to be. A replica set is a group of MongoDB servers that maintain the same data set, ensuring your data is always available, even in the face of hardware or software failures. This is essential for growing businesses facing the challenge of handling exponentially increasing data.

MongoDB replication is beneficial for:

  • safeguarding your data
  • enabling disaster recovery
  • proper backups, and reporting

Imagine having multiple MongoDB servers working together, automatically keeping your data in sync. That’s the magic of a replica set. Sounds cool. Let’s do this!

What is MongoDB?

  • MongoDB is a Document-oriented Open-source NoSQL Database. It stores the data in documents and supports NoSQL Query Language to interact with data.

What is Replication in MongoDB?

  • The process of making copies of data across servers to ensure the high availability of data in case of failures.
  • Replication of data ensures that the data is available all the time to users in case of any network failure or hardware failure that potentially causes data loss.
  • A MongoDB Replica Set is a group of mongod processes that maintain the same data set throughout.

How to set up MongoDB Replica Set Configuration in Linux?

  • Step 1: Set up the hosts
  • Step 2: Set up hostname
  • Step 3: Generate Key
  • Step 4: Configure Replica Set
  • Step 5: Initiate Replication
  • Step 6: Adding instances to Replication
  • Step 7: Check the status
Integrate MongoDB to BigQuery
Integrate MongoDB to Snowflake
Integrate MongoDB to Redshift
Integrate MongoDB to Databricks

Step 1: Set up the hosts

For this example, the following nodes are used to replicate the data: 

192.168.0.29 mongo-db1 
192.168.0.30 mongo-db2
192.168.0.31 mongo-db3

Add the above IP address to each node’s/etc/hosts file. Please ensure that all the three IPs need to be in each node’s/etc/hosts file.

Step 2: Set up hostname

On each node, rename the hostname to be identified via a name, not just by IP address.

$ sudo vim /etc/hostname     ## On Node1 
mongo-db1 
$ sudo vim /etc/hostname     ## On Node2
mongo-db2
$ sudo vim /etc/hostname     ## On Node3
mongo-db3

Step 3: Generate Key

Keys will help to communicate nodes without the requirement of an external passcode.

To generate the key, execute the given below commands:

On Node 1(mongo-db1)
# mkdir -p /etc/mongodb/keys/
# openssl rand -base64 756 > /etc/mongodb/keys/mongo-key
# chmod 400 /etc/mongodb/keys/mongo-key
# chown -R mongodb:mongodb /etc/mongodb

Now, you can copy the generated key file to all other nodes at the same location i.e. /etc/mongodb/keys.

Step 4: Configure Replica Set

Now that you have all the configurations done on the nodes, one last step is to add the IP addresses of the nodes and provide a replication set name. The MongoDB configuration file can be found at /etc/mongo.conf.

On node 1 => mongo-db1
# network interfaces
net:
  port: 27017
  bindIp: 192.168.0.29
#security:
security:
 authorization: enabled
 keyFile:  /etc/mongodb/keys/mongo-key
#replication:
replication:
  replSetName: "replicaset-01"

Perform the same thing on the other two nodes.

Once the changes are done, you can restart the MongoDB services by running the following command: 

sudo systemctl restart mongod

Step 5: Initiate replication

Log in to the primary node 192.168.0.29 .

$ mongo
> rs.initiate()
Want an easy option to replicate your MongoDB data? Try Hevo!

Hevo offers a real-time data replication feature that will allow your MongoDB data to be easily replicated with just a few clicks. Try our 14-days free trial feature now!

Get Started with Hevo for Free

Step 6: Adding Instances to Replica set

Once the replication REPL is started, it’s time to add nodes to the replication set by the following command:

rs.add(“mongo-db2:27017”)
rs.add(“mongo-db3:27017”)

Once you add the nodes, you will see the output as {‘ok’:1}, which indicates a successful addition of nodes in the replica set.

Step 7: Check the Status

The status of the replication set can be seen from the following command:

rs.status()

The above command will result in the following output:

{ 
"set" : "myitsocial", 
"date" : ISODate("2022-02-10T06:15:02Z"), 
"myState" : 1, 
"members" : [ 
   { 
      "_id" : 0, 
      "name" : "192.168.0.29:27017", 
      "health" : 1, 
      "state" : 1, 
      "stateStr" : "PRIMARY", 
      "uptime" : 303165, 
      "optime" : Timestamp(1644516902, 1), 
      "optimeDate" : ISODate("2022-02-10T06:15:02Z"), 
      "self" : true 
   }, 
   { 
      "_id" : 1, 
      "name" : "192.168.0.30:27017", 
      "health" : 1, 
      "state" : 2, 
      "stateStr" : "SECONDARY", 
      "uptime" : 302985, 
      "optime" : Timestamp(1644516902, 1), 
      "optimeDate" : ISODate("2022-02-10T06:15:02Z"), 
      "lastHeartbeat" : ISODate("2022-02-10T06:15:02Z"), 
      "lastHeartbeatRecv" : ISODate("2014-08-12T06:15:02Z"), 
      "pingMs" : 0, 
   "syncingTo" : "10.20.30.40:27017" 
   },
   { 
      "_id" : 2, 
      "name" : "192.168.0.31:27017", 
      "health" : 1, "state" : 2, 
      "stateStr" : "SECONDARY", 
      "uptime" : 302985, 
      "optime" : Timestamp(1644516902, 1), 
      "optimeDate" : ISODate("2022-02-10T06:15:02Z"), 
      "lastHeartbeat" : ISODate("2022-02-10T06:15:02Z"), 
      "lastHeartbeatRecv" : ISODate("2022-02-10T06:15:02Z"), 
      "pingMs" : 0, 
      "syncingTo" : "192.168.0.29:27017" 
   } 
], 
"ok" : 1 
} 

How to set up MongoDB Replica Set Configuration in Windows?

Step 1: Setup Hosts

Go to your Hosts directory < C:\Windows\System32\drivers\etc\hosts> and make the following changes:

192.168.0.29 mongo-db1

192.168.0.30 mongo-db2

192.168.0.31 mongo-db3

Step 2: Set up the Hostname

Change the Hostname for each of the following nodes.

  • Node1: mongo-db1
  • Node2: mongo-db2
  • Node3: mongo-db3

Step 3: Generate Key

On Node1, create a key.

mkdir C:\mongodb\keys

openssl rand -base64 756 > C:\mongodb\keys\mongo-key

icacls C:\mongodb\keys\mongo-key /inheritance:r /grant:r "mongodb:F"

Copy ‘mongo-key’ to the exact location on all other nodes.

Step 4: Configure Replica Set

Go to MongoDB bin folder and search for a file named mongod.cfg. Edit this file for each node.

net:

  port: 27017

  bindIp: 192.168.0.29  # Change for each node

security:

  authorization: enabled

  keyFile: C:\mongodb\keys\mongo-key

replication:

  replSetName: "replicaset-01"

Step 5: Restart MongoDB services

net stop MongoDB

net start MongoDB

Step 6: Initiate Replication

On the primary node, in this case, 192.168.0.29, initiate replication

mongo

> rs.initiate()

Step 7: Add Instances to the Replica Set

Add other nodes to the replica set

> rs.add("mongo-db2:27017")

> rs.add("mongo-db3:27017")

Step 8: Check the status using the following command

> rs.status()

Note: If you are using AWS to create MongoDB replica sets, you can launch three EC2 instances running on Linux OS (for example, Amazon Linux 2) and follow the steps mentioned to configure them on Linux OS.

Why do you need Replication in MongoDB?

  • Data Replication replicates the data across servers, thereby ensuring high data availability.
  • Data Replication provides a level of fault tolerance against any data loss.
  • Keeping a copy of data in another server helps users recover the data in case of network failure or data loss.
  • It can also serve for disaster recovery, reporting, etc.
  • It can also act as a load balancer, as it causes less load on a particular server for data retrieval.

What is MongoDB Replica Set Configuration?

  • The Replication of MongoDB uses a replica set to perform Replication.
  • There are various configurations related to the replica set to replicate the data from one server to another successfully.
  • In this blog post, you will learn about various configuration options available and how you can use them to replicate your data effectively.

You can access replica set configuration via method rs.conf(). Give below is a sample configuration document: 

{
  _id: <string>,
  version: <int>,
  term: <int>,
  protocolVersion: <number>,
  writeConcernMajorityJournalDefault: <boolean>,
  configsvr: <boolean>,
  members: [
    {
      _id: <int>,
      host: <string>,
      arbiterOnly: <boolean>,
      buildIndexes: <boolean>,
      hidden: <boolean>,
      priority: <number>,
      tags: <document>,
      secondaryDelaySecs: <int>,
      votes: <number>
    },
    ...
  ],
  settings: {
    chainingAllowed : <boolean>,
    heartbeatIntervalMillis : <int>,
    heartbeatTimeoutSecs: <int>,
    electionTimeoutMillis : <int>,
    catchUpTimeoutMillis : <int>,
    getLastErrorModes : <document>,
    getLastErrorDefaults : <document>,
    replicaSetId: <ObjectId>
  }
}

 The above configuration document contains the following elements:  

  • _id: This is the name of the replica set.
  • version: This is an incrementing number to distinguish various versions of Replication.
  • term: An incrementing number used to distinguish revisions of the replica set configuration document from previous iterations of the configuration.
  • configsvr: is a boolean term that indicates whether the replica set is used for a sharded cluster’s config servers.
  • protocolVersion: It defaults to 1.
  • writeConcernMajorityJournalDefault: Determines the behavior of { w: “majority”} write concern if the write concern does not explicitly specify the journal option j.
  • members: This array element contains the information about various replication members (servers) included in the replication set. It stores id, hostname, indexes, priority, etc.
  • settings: It contains optional settings that are applied to the whole replica set.

Best Practices for MongoDB Replica Set Configuration

Configuring MongoDB replica sets effectively ensures data redundancy and high availability. Here are some best practices that you can follow:

  1. Replica Set Size: Aim for an odd number of members, typically three or five, to ensure a reliable quorum for election processes.
  2. Priority Settings: Assign priorities to control which members can become the primary.
  3. Delayed Members: Consider adding delayed members to protect against unintended deletions or data corruption by providing a time-delayed copy of the data.
  4. Backup Strategy: Maintain regular backups even with replica sets, as they provide additional security and recovery options.
  5. Version Consistency: Ensure all members run the same MongoDB version to avoid compatibility issues.

Common Issues and Troubleshooting in MongoDB Replica Set 

When managing MongoDB replica sets, several common issues can arise. Here are some typical problems and their troubleshooting steps:

  1. Election Failures: Ensure all members are reachable and check network connectivity.
  2. Replication Lag: Monitor replication performance and adjust the write concern levels. Check for hardware bottlenecks and network latency.
  3. Rollback Situations: Avoid this by ensuring the high availability of primary nodes and using delayed members for data protection.
  4. Stale Reads: Mitigate by setting appropriate read preferences, such as primary preferred or nearest.
  5. Authentication Issues: Verify user roles and permissions. Ensure consistent authentication configurations across all members.

Advanced Configuration Options for MongoDB Replica Sets

Advanced configuration can enhance performance, reliability, and manageability. Here are some key options that you can apply:

  1. Arbiters: Used to break ties in elections without storing data. To add an arbiter, you can use the following command.
rs.addArb("arbiter-host:27017")
  1. Hidden Members: Invisible to client applications and can be used for backups or analytics. To add hidden members, use the command.
rs.add({ host: "hidden-host:27017", hidden: true, priority: 0 })
  1. Delayed Members: Holds a delayed copy of data to protect against unintended deletions or corruption. Tto add delayed members, use the command.
rs.add({ host: "delayed-host:27017", priority: 0, slaveDelay: 3600 })
  1. Priority Settings: You can set the priority of members to influence which node is more likely to become the primary. To add priority settings, use the command.
rs.add({ host: "priority-host:27017", priority: 2 })

Helpful Resources on MongoDB Replica Set Configuration

Conclusion

  1. In this article, you have learned how to set up MongoDB Replica Set Configuration with help of a step-by-step procedure.
  2. MongoDB prevents the loss of any data via its Replication feature. Using Replica sets, MongoDB provides Data Redundancy, High Availability, and fault tolerance by allowing users to create additional data replicas for dedicated purposes such as Disaster Recovery, Backup, Reporting, etc.

FAQ on How to Set up MongoDB Replica

How to config replica set in MongoDB?

1. Initialize the Replica Set using using the ‘rs.initiate()’ command in the mongo shell
2. Add Additional Members (if needed)
3. Configure Replica Set Settings

How to reset MongoDB replica set settings?

If you need to reset replica set settings, you typically reconfigure the replica set with the desired configuration using ‘rs.reconfig()’.

How do I check the configuration of a replica set?

1. Connect to Primary Node
2. Run rs.conf() Command

How do you set a replica set to primary in MongoDB?

MongoDB automatically elects a primary node within a replica set based on its election rules. You cannot manually set a node to be the primary. However, you can influence elections by adjusting the priority settings of replica set members.

Vishal Agrawal
Technical Content Writer, Hevo Data

Vishal Agarwal is a Data Engineer with 10+ years of experience in the data field. He has designed scalable and efficient data solutions, and his expertise lies in AWS, Azure, Spark, GCP, SQL, Python, and other related technologies. By combining his passion for writing and the knowledge he has acquired over the years, he wishes to help data practitioners solve the day-to-day challenges they face in data engineering. In his article, Vishal applies his analytical thinking and problem-solving approaches to untangle the intricacies of data integration and analysis.

No-code Data Pipeline for MongoDB