A beginner-friendly guide to setting up MongoDB on an AWS EC2 instance.
MongoDB is a widely used NoSQL database known for its flexibility, scalability, and ease of use. This guide is designed for beginners looking to set up their own MongoDB database on an AWS EC2 instance in simple and clear steps.
Amazon Elastic Compute Cloud (EC2) is a cloud-based service that provides resizable computing power, allowing users to deploy and manage virtual machines easily. It is widely used for hosting applications, running development environments, and managing databases. In this guide, we will set up an EC2 instance running Ubuntu 22.04 LTS. Ubuntu is a popular choice for cloud servers due to its stability, security, and strong community support. Specifically, for MongoDB, Ubuntu provides official repositories, making installation and maintenance straightforward. Additionally, its compatibility with modern cloud environments ensures optimal performance and security updates.
Let's proceed with launching our EC2 instance and configuring it for MongoDB deployment.
For security reasons, configure SSH access to allow connections only from your public IP address.
0.0.0.0/0
).
This helps prevent unauthorized access and significantly reduces the risk of brute-force attacks.
To securely connect to your EC2 instance, we use PuTTY, a popular SSH client for Windows that allows secure remote access to Linux servers.
Follow these steps to establish an SSH connection:
ubuntu
. This grants you access to the server with SSH privileges.
ec2-user
).
Copy and pase the following commands into the PuTTY terminal (make sure you are connected to the EC2 instance).
sudo apt update && sudo apt upgrade -y
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-7.0.gpg
echo "deb [signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt update && sudo apt install -y mongodb-org
sudo systemctl enable mongod && sudo systemctl start mongod
sudo nano /etc/mongod.conf
bindIp
and security
setting:bindIp: 0.0.0.0
security:
authorization: enabled
sudo systemctl restart mongod
After enabling authentication in MongoDB, you need to create an administrative user to manage database access.
mongosh
admin
database:use admin
db.createUser({
user: "admin",
pwd: "securepassword",
roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
})
userAdminAnyDatabase
: Grants the user permission to manage users and roles on any database.db: "admin"
: Specifies that the user is created in the admin
database.exit
securepassword
.MongoDB Compass is a graphical interface that allows you to visually interact with your MongoDB database. It provides an easy way to explore your collections, run queries, and manage your data.
mongodb://admin:securepassword@EC2_Public_IP:27017
securepassword
with a strong, unique password.Now that you’re connected, you can start creating databases and collections. Refer to the official MongoDB documentation for best practices on structuring your data efficiently: