how to install MongoDB replica set

How to deploy a Production-Ready MongoDB Replica Set on Ubuntu

MongoDB replica sets provide high availability and fault tolerance by allowing multiple copies of your data to be stored across different servers or regions. In this tutorial, we will guide you through the process of setting up a production-ready MongoDB replica set on Ubuntu.

When to Use Replica Sets

Replica sets are useful when you need to:

  • Ensure high availability of your MongoDB database in case of a node failure.
  • Distribute the read load among multiple nodes to improve performance.
  • Increase the durability of your data by storing multiple copies in different servers or regions.

For replication to setup, you require minimum of 3 replica servers to setup replication, this can increase cost. You can reduce server cost by adding a dummy replica that can never become the primary node.

Prerequisites:

You will need basic knowledge of the Linux command line. This blog is part of the series “Installing MongoDB on Production Environment”. Checkout the previous parts here:

  1. Ubuntu servers with MongoDB installed.
  2. Securing Your MongoDB: How to Enable Authentication.

Step 1: Configuring MongoDB on the Primary Node

In a replica set, one node acts as the primary node that accepts write operations, while the others act as secondary nodes that replicate data from the primary. Choose one of the servers as the primary node and add the following configuration to the MongoDB configuration file /etc/mongod.conf:

replication:
  replSetName: <replication_set_name>

Replace <replication_set_name> with a unique name for your replica set.

Save the changes and restart the MongoDB service:

sudo systemctl restart mongod

Step 2: Adding Secondary Nodes

Add the following configuration to the MongoDB configuration file /etc/mongod.conf on the secondary nodes:

replication:
  replSetName: <replication_set_name>

Save the changes and restart the MongoDB service on each secondary node:

sudo systemctl restart mongod

Step 3: Initiating the Replica Set

Connect to the primary node using the MongoDB shell and initiate the replica set with the following command:

rs.initiate()

This command will create the initial configuration for the replica set.

Step 4: Adding Secondary Nodes to the Replica Set

To add a secondary node to the replica set, connect to the primary node using the MongoDB shell and run the following command:

rs.add("<secondary_node_hostname>")

Replace <secondary_node_hostname> with the hostname or IP address of the secondary node.

Repeat this step for each secondary node.

Step 5: Adding the Dummy Replica

To add a dummy replica that can never become the primary node, connect to the primary node using the MongoDB shell and run the following command:

rs.addArb("<dummy_node_hostname>")

Replace <dummy_node_hostname> with the hostname or IP address of the dummy node.

Step 6: Verifying the Replica Set Status

To verify the status of the replica set, connect to the primary node using the MongoDB shell and run the following command:

rs.status()

This command will show you the status of each node in the replica set.

Conclusion

In this tutorial, we showed you how to set up a production-ready MongoDB replica set on Ubuntu, including how to add a dummy replica that can never become the primary node. By following these steps, you can ensure that your data is available and durable even in case of a node failure. Replica sets are a powerful feature of MongoDB that can improve the availability and performance of your database, and it is important to configure them correctly to ensure the stability and reliability of your system.

Hope this blog helped. Enjoy!!!. If you want to support the writing, please feel free to buy me a coffee.

References: Official MongoDB Documentation

Leave a ReplyCancel reply

Exit mobile version