MongoDB 6.0 Migration on EC2: The Good, the Bad, and the Gotchas

MongoDB 6.0 Migration on EC2: The Good, the Bad, and the Gotchas

At Vyrill, we recently upgraded our database infrastructure by migrating to MongoDB 6.0 running on EC2. 

While MongoDB Atlas is our eventual goal for a fully managed service, getting MongoDB 6.0 up and running on EC2 was an essential interim step. In this post, I'll walk through our process, including the specific commands used.

We decided to upgrade MongoDB to take advantage of new features like faster queries, enhanced search, and multi-document ACID transactions. Here are the step-by-step actions we took.

Part 1: Create a New EC2 Instance 🌐

Kick off by initializing your EC2 instance. Choose Ubuntu 22.02 x64 as your operating system and R6a.2xlarge for the instance type. Don't skimp on storage; we went for 800 GB of EBS storage to allow data expansion.

Part 2: SSH into the Instance 🛠️

Once the EC2 instance is up, connect using SSH. Your command should look like this:

Note: IP addresses are obfuscated for security.

Part 3: Install AWS CLI 📦

The AWS Command Line Interface is essential for smooth sailing on AWS platform. Run this simple command to install it:

Part 4: Install MongoDB 📚

Here comes the core part, installing MongoDB itself.

1. Add MongoDB Key: Add the official MongoDB GPG key to ensure you're downloading the genuine package.

2. Add MongoDB Repository: Add the official MongoDB repository.

3. Update Package List:

4. Start MongoDB:

   

5. Enable MongoDB: This ensures MongoDB starts on boot.

6. Check MongoDB Status:

7. Enter MongoDB Shell to Verify Installation:

Part 5: Configure Network and Security 🛡️

1. Allow Incoming Connections: Modify mongod.conf to set bindIp value to 0.0.0.0 instead of the default 127.0.0.1.

2. Create Admin Credentials: For added security, create an admin user.

    

3. Enable Authorization:

    

4. Restart MongoDB:

Part 6: Copy Database Backup 📁

This step assumes that you have taken the db backup and stored the backup folder in an AWS S3 bucket.

The following command takes backup of your MongoDB databases.

Transfer your database backup from S3 to the EC2 instance.

Part 7: Restore Database 💾

Restore the MongoDB databases using the following command:

Part 8: Configure AWS Security Groups 🔐

Add entries in your EC2’s security group for SSH (port 22) and MongoDB (port 27017).

# Allow SSH from our VPC   

Inbound Rule - SSH - xxx.xxx.xxx.xxx/xx

# Allow MongoDB access from our VPC

Inbound Rule - Custom TCP - Port 27017 - xxx.xxx.xxx.xxx/xx

Security Group inbound rules screen on AWS console

Part 9: Connect to your database 💻

Connect to your MongoDB server from a tool like MongoDB Compass using this connection string:

Wrapping Up

Setting up MongoDB on an AWS EC2 instance can be a breeze with the right guide. We hope this step-by-step walkthrough aids you in enhancing your database infrastructure, as it has for us at Vyrill. Stay tuned for our transition to MongoDB Atlas! 🚀 💻

----------------------------------------------------------------------

To view or add a comment, sign in

Others also viewed

Explore topics