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:

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

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:

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! ?? ??

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

要查看或添加评论,请登录

Abhi Mahule的更多文章

  • Ask Vyrill - UGC Video Intelligence copilot

    Ask Vyrill - UGC Video Intelligence copilot

    We recently released our version of ChatGPT called "Ask Vyrill," a RAG (Retrieval Augmented Generation) based approach…

    2 条评论
  • Migrating to MongoDB Atlas

    Migrating to MongoDB Atlas

    We recently completed a major migration journey of our database infrastructure at Vyrill. The goal was to move from a…

    1 条评论
  • Harnessing the Power of GraphQL

    Harnessing the Power of GraphQL

    Introduction ?? As the CTO of a startup, my role involves continuously exploring ways to optimize our tech stack. In…

  • ?? Chains of Thought: Building Smarter AI with LangChain

    ?? Chains of Thought: Building Smarter AI with LangChain

    Langchain - What is it and how to use it? The AI world is buzzing about LangChain, the new toolkit for working with…

  • ?? The Rising Star of ML Ops: VectorDB - Why They're Outperforming SQL & NoSQL for Embedding Storage

    ?? The Rising Star of ML Ops: VectorDB - Why They're Outperforming SQL & NoSQL for Embedding Storage

    Why VectorDB? As part of our journey at Vyrill, we're always learning and exploring new things because of our AI-driven…

  • Generative AI, you say?

    Generative AI, you say?

    While the hype in the AI landscape has been increasing steadily over the years, it has reached a crescendo, thanks to…

    4 条评论
  • Biggest crypto scam of all time?

    Biggest crypto scam of all time?

    Intro Decentralized Finance (DeFi) is a fascinating space and has a promising future with an enormous number of…

  • What is NoSQL, and why may you care?

    What is NoSQL, and why may you care?

    The term NoSQL was created to stand out in contrast to another term, "SQL". Let's take a look at what it means and its…

  • What is the blockchain trilemma?

    What is the blockchain trilemma?

    Trilemma (noun) - a situation in which a difficult choice has to be made between three alternatives, especially when…

  • #1 reason VCs are bullish about crypto

    #1 reason VCs are bullish about crypto

    Every few years, there are phases that entrepreneurs and VCs get tremendously excited about. Crypto is the area that…

    1 条评论

社区洞察