Profile
Aman Kumar Singh
View Profile

AMAN KUMAR SINGH - BLOG

Step-by-Step MongoDB Setup on AWS EC2: Deploy, Secure, and Optimize

blog banner
6
1

Date: December 17, 2024

Introduction

MongoDB, a leading NoSQL database, has become essential for modern applications, offering flexible, JSON-like document storage and horizontal scalability. When combined with Amazon Web Services (AWS), MongoDB can be hosted in a reliable, secure, and scalable cloud environment. In this step-by-step guide, you’ll learn how to deploy, secure, and optimize MongoDB on an AWS EC2 instance.

Why Use MongoDB on AWS?

AWS offers a robust environment for hosting MongoDB databases. Key benefits include:

  • High availability through AWS's global infrastructure.
  • Seamless integration with AWS services like S3, Lambda, and CloudWatch.
  • Flexible instance types to suit performance and cost requirements.
  • Built-in security features, including IAM roles and Security Groups.

Prerequisites

Before starting, ensure you have the following:

  • An active AWS account with EC2 access.
  • Basic knowledge of Linux commands.
  • An SSH client installed (e.g., Terminal, PuTTY).
  • Access to a secure key pair for connecting to your EC2 instance.

Step 1: Launch an AWS EC2 Instance

To set up MongoDB, the first step is to launch an AWS EC2 instance:

  1. Log in to your AWS Management Console.
  2. Navigate to the EC2 Dashboard and click on Launch Instance.
  3. Select the operating system. Choose Ubuntu Server 22.04 LTS.
  4. Select an instance type:
    • t2.micro: Ideal for testing (Free Tier eligible).
    • t2.small or larger: Suitable for production use.
  5. Configure Security Groups:
    • Allow SSH on port 22 (your IP only).
    • Allow MongoDB access on port 27017 (trusted IPs only).
  6. Create or choose a key pair for SSH access and download it securely.
  7. Launch the instance and note the public IP address.

Step 2: Connect to Your EC2 Instance

Use SSH to access the EC2 instance. Replace the placeholders with your key file and instance IP address:

ssh -i your-key.pem ubuntu@your-ec2-public-ip

Fix permissions for the key file if you encounter permission errors:

chmod 400 your-key.pem

Step 3: Install MongoDB

Install MongoDB on your EC2 instance by following these steps:

  1. Update the package list:
  2. sudo apt-get update
  3. Install dependencies:
  4. sudo apt-get install -y gnupg curl
  5. Import the MongoDB public GPG key:
  6. curl -fsSL https://pgp.mongodb.com/server-6.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-6.0.gpg --dearmor
  7. Add MongoDB’s official repository:
  8. echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
  9. Install MongoDB:
  10. sudo apt-get update
    sudo apt-get install -y mongodb-org
  11. Start and enable MongoDB:
  12. sudo systemctl start mongod
    sudo systemctl enable mongod

Step 4: Secure MongoDB

Enable user authentication to secure your MongoDB instance:

  1. Access MongoDB shell:
  2. mongo
  3. Create an admin user:
  4. use admin
    db.createUser({ user: "admin", pwd: "secure-password", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })

Step 5: Test MongoDB Connectivity

Verify that MongoDB authentication is working correctly:

  1. Connect to MongoDB using the new admin credentials:
  2. mongo -u "admin" -p "secure-password" --authenticationDatabase "admin"
  3. Create and test a sample database:
  4. use testdb
    db.testCollection.insertOne({name: "Sample Entry", status: "Connected"})
    db.testCollection.find()
  5. Exit the MongoDB shell:
  6. exit

Step 6: Optimize MongoDB Performance

To ensure MongoDB runs efficiently, follow these optimization steps:

  • Enable journaling to ensure data integrity.
  • Use indexes to improve query performance:
  • db.testCollection.createIndex({name: 1})
  • Monitor logs for slow queries:
  • sudo tail -f /var/log/mongodb/mongod.log

Step 7: Backup and Restore MongoDB

Regular backups help prevent data loss in case of a failure.

1. Create a Backup

mongodump --db testdb --out /backup

2. Restore the Backup

mongorestore --db testdb /backup/testdb

Step 8: Monitor MongoDB Performance

Continuous monitoring helps you identify and resolve performance issues:

  • Use MongoDB Profiler to analyze queries:
  • db.setProfilingLevel(2)
    db.system.profile.find()
  • Leverage AWS CloudWatch for EC2 instance monitoring.

Conclusion

By following this guide, you have successfully set up MongoDB on an AWS EC2 instance, secured it with authentication, tested connectivity, optimized its performance, and configured backup processes. This deployment ensures a scalable, secure, and reliable database environment for modern applications. For more advanced management, consider using MongoDB Atlas, a managed service that integrates seamlessly with AWS.

Next Blog

© 2024 Aman Kumar Singh. All rights reserved.

Terms of Service

Contact

About