MongoDB 6.0 Migration on EC2: The Good, the Bad, and the Gotchas
Abhi Mahule
Tech executive with expertise in building large scale systems. Building a GenAI platform to help ecommerce brands with video ads using LLMs. 2x founder | 1 IPO | Ex-Roku | Holder of O-1A extraordinary ability visa
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:
ssh -i <path to yoursecurity.pem file> [email protected]
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:
sudo apt install awscli
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.
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
2. Add MongoDB Repository: Add the official MongoDB repository.
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
3. Update Package List:
sudo apt-get update
4. Start MongoDB:
sudo systemctl start mongod
???
5. Enable MongoDB: This ensures MongoDB starts on boot.
sudo systemctl enable mongod
6. Check MongoDB Status:
sudo systemctl status mongod
7. Enter MongoDB Shell to Verify Installation:
mongosh
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.
sudo vim /etc/mongod.conf
###
net:
port: 27017
????bindIp: 0.0.0.0
2. Create Admin Credentials: For added security, create an admin user.
mongosh
use admin
db.createUser({
user: "admin",
pwd: "<yourStrongPassword>",
roles: ["root"]
})????
????
3. Enable Authorization:
sudo vim ./etc/mongod.conf
#######
security:
??????authorization: "enabled"
????
4. Restart MongoDB:
sudo systemctl restart mongod
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.
mongodump --host localhost --port 27017 --out <output folder>
Transfer your database backup from S3 to the EC2 instance.
sudo aws s3 sync s3://< s3 bucket db backup>/ <dir for db backup>
Part 7: Restore Database ??
Restore the MongoDB databases using the following command:
mongorestore --host localhost --port 27017 <dir for db backup>
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
Part 9: Connect to your database ??
Connect to your MongoDB server from a tool like MongoDB Compass using this connection string:
mongodb://<username>:<password>@ec2-xx-xxx-xx-xxx.us-west-2.compute.amazonaws.com:27017/
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! ?? ??
----------------------------------------------------------------------