AWS, Cloud Computing

3 Mins Read

Migrating MongoDB to a Multi-Node Replica Set on Amazon EC2

Voiced by Amazon Polly

Overview

High availability and fault tolerance are crucial for any production-grade database system. MongoDB offers built-in support for replication via Replica Sets, which maintains multiple copies of your data across different instances. In this guide, we walk through migrating MongoDB to a multi-node replica set using Amazon EC2 instances, including the setup of primary, secondary, and arbiter nodes. We will also briefly integrate supporting AWS services like AWS IAM, Amazon S3 for backup, and Amazon CloudWatch for monitoring.

Pioneers in Cloud Consulting & Migration Services

  • Reduced infrastructural costs
  • Accelerated application deployment
Get Started

Introduction

Replica sets in MongoDB provide redundancy and high availability. A replica set consists of a primary node receiving all write operations and one or more secondary nodes replicating the primary data. An optional arbiter node can be added to participate in elections without storing data.

This blog details a step-by-step approach to migrate and deploy MongoDB on three Amazon EC2 instances, configure replication, and automate monitoring using AWS-native tools for enhanced observability and security.

Prerequisites

Before you begin, ensure the following:

  • You have three Ubuntu 20.04+ EC2 instances (Primary, Secondary, and Arbiter) in the same Amazon VPC with appropriate Security Group rules (allowing internal communication on port 27017).
  • Amazon EC2 instances are launched with AWS IAM roles for secure access to Amazon S3 (optional backup) or Amazon CloudWatch.
  • SSH access is enabled via a private key, and you have root/sudo privileges.
  • Basic familiarity with Linux terminal, AWS Console, and MongoDB is helpful.

Step-by-Step Guide

  1. Install MongoDB on Secondary and Other Nodes

Create a user-data script for cloud-init or manual execution on the secondary and arbiter nodes:

  1. MongoDB Replica Set Configuration Script

Run the following script on all nodes, adjusting the IP addresses:

  1. Add Monitoring Script

On all nodes:

Advantages

  • High Availability: The replica set ensures the database remains available even if one node goes down.
  • Data Redundancy: Automatically replicates data across nodes to prevent data loss.
  • Automatic Failover: MongoDB automatically elects a new primary if the primary node fails.
  • Scalability: You can add more secondary nodes or convert to sharded clusters for horizontal scaling.
  • Read Distribution: Secondary nodes can serve read traffic if configured, improving performance.

Conclusion

Setting up a MongoDB replica set on Amazon EC2 ensures a production-ready architecture with high availability and resilience. This guide introduced essential setup concepts while abstracting sensitive commands to avoid misapplication.

You can further enhance this setup by:

  • Automating instance provisioning via AWS CloudFormation or Terraform.
  • Securing credentials using AWS IAM Roles and AWS Secrets Manager.
  • Enabling daily backups to Amazon S3 via a cron job or AWS Lambda.

Drop a query if you have any questions regarding Amazon EC2 and we will get back to you quickly.

Empowering organizations to become ‘data driven’ enterprises with our Cloud experts.

  • Reduced infrastructure costs
  • Timely data-driven decisions
Get Started

About CloudThat

CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.

CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 850k+ professionals in 600+ cloud certifications and completed 500+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training PartnerAWS Migration PartnerAWS Data and Analytics PartnerAWS DevOps Competency PartnerAWS GenAI Competency PartnerAmazon QuickSight Service Delivery PartnerAmazon EKS Service Delivery Partner AWS Microsoft Workload PartnersAmazon EC2 Service Delivery PartnerAmazon ECS Service Delivery PartnerAWS Glue Service Delivery PartnerAmazon Redshift Service Delivery PartnerAWS Control Tower Service Delivery PartnerAWS WAF Service Delivery PartnerAmazon CloudFront Service Delivery PartnerAmazon OpenSearch Service Delivery PartnerAWS DMS Service Delivery PartnerAWS Systems Manager Service Delivery PartnerAmazon RDS Service Delivery PartnerAWS CloudFormation Service Delivery PartnerAWS ConfigAmazon EMR and many more.

FAQs

1. What is the role of the arbiter in a replica set?

ANS: – Arbiters participate in elections but do not store data. They help break ties and maintain an odd number of votes for consistent elections.

2. Can I add more secondaries later?

ANS: – Yes, MongoDB supports dynamic reconfiguration of the replica set to add or remove members.

WRITTEN BY Dhruv Rajeshbhai Patel

Dhruv Patel is a Research Intern at CloudThat. He has completed his Master's in Computer Application and Cloud Certification in Azure and AWS. His area of interest lies in Cloud and Mobile Development Solutions. He loves to take ownership of the work that he is doing.

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!