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
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
- 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#!/bin/bash -xe # Update the system # Install MongoDB 6.0 curl -fsSL https://pgp.mongodb.com/server-6.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-6.0.gpg apt-get install -y mongodb-org # Create necessary directories mkdir -p /var/lib/mongodb chown -R mongodb:mongodb /var/lib/mongodb # Configuration file cat > /etc/mongod.conf << 'EOC' storage: dbPath: /var/lib/mongodb journal: enabled: true systemLog: destination: file path: /var/log/mongodb/mongod.log logAppend: true net: port: 27017 bindIp: 0.0.0.0 replication: replSetName: "rs0" EOC # Start MongoDB |
- MongoDB Replica Set Configuration Script
Run the following script on all nodes, adjusting the IP addresses:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#!/bin/bash # Define node IPs PRIMARY_IP="<primary_private_ip>" SECONDARY_IP="<secondary_private_ip>" ARBITER_IP="<arbiter_private_ip>" # Determine current node IP # Overwrite MongoDB config cat > /etc/mongod.conf << EOC storage: dbPath: /var/lib/mongodb journal: enabled: true systemLog: destination: file path: /var/log/mongodb/mongod.log logAppend: true net: port: 27017 bindIp: 0.0.0.0 replication: replSetName: "rs0" EOC # Restart MongoDB # Wait a bit # Only on PRIMARY node if [ "$CURRENT_IP" == "$PRIMARY_IP" ]; then echo "Initializing replica set..." cat > /tmp/init_replica.js << EOC rs.initiate({ _id: "rs0", members: [ { _id: 0, host: "${PRIMARY_IP}:27017", priority: 2 }, { _id: 1, host: "${SECONDARY_IP}:27017", priority: 1 }, { _id: 2, host: "${ARBITER_IP}:27017", arbiterOnly: true } ] }) EOC mongosh < /tmp/init_replica.js # Wait and create users ADMIN_PASS=$(openssl rand -base64 12) TEST_PASS=$(openssl rand -base64 12) #Create User |
- Add Monitoring Script
On all nodes:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
cat > /home/ubuntu/check_mongodb_status.sh << 'EOC' #!/bin/bash mongosh --eval "rs.status()" | tee /tmp/rs_status.txt if grep -q "PRIMARY" /tmp/rs_status.txt; then echo "Replica set healthy" else echo "WARNING: No PRIMARY found" fi if grep -q "unreachable" /tmp/rs_status.txt; then echo "Some members unreachable" fi EOC |
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
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 Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner, Amazon CloudFront Service Delivery Partner, Amazon OpenSearch Service Delivery Partner, AWS DMS Service Delivery Partner, AWS Systems Manager Service Delivery Partner, Amazon RDS Service Delivery Partner, AWS CloudFormation Service Delivery Partner, AWS Config, Amazon 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.
Comments