How to Install MongoDB Community Edition on an EC2 Ubuntu 22.04

This guide walks you through how to install MongoDB Community Edition on EC2 Ubuntu 22.04 using MongoDB’s official APT repository. You’ll set up MongoDB 8.0, enable it as a service, create an admin user, and verify the installation with mongosh.

Prerequisites

Add the MongoDB Repository

Ubuntu’s default repositories don’t include MongoDB. You need to add MongoDB’s official APT repo, maintained by MongoDB Inc.

1. Import the GPG key

sudo apt-get install -y gnupg curl
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor

This downloads MongoDB’s signing key and stores it in the keyring so APT can verify packages.

2. Add the repository source

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list

Install MongoDB 8.0

sudo apt update
sudo apt install -y mongodb-org

The mongodb-org meta-package installs the server (mongod), the shell (mongosh), and the database tools.

Start and Enable the Service

sudo systemctl start mongod
sudo systemctl enable mongod

Verify it’s running:

sudo systemctl status mongod

You should see active (running). If the service fails to start, run sudo systemctl daemon-reload first, then try starting it again.

Verify with mongosh

Connect to the MongoDB instance using the shell:

mongosh

You should see the MongoDB shell prompt. Run a quick test:

db.runCommand({ ping: 1 })

If you see { ok: 1 }, MongoDB is running and accepting connections. Type exit to leave the shell.

Create an Admin User

MongoDB ships with no authentication enabled by default. Anyone who can reach port 27017 can read and write your data. Set up an admin user before doing anything else.

Connect to the shell and switch to the admin database:

mongosh
use admin

db.createUser({
  user: "adminUser",
  pwd: "your-strong-password",
  roles: [{ role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase"]
})

Replace adminUser and your-strong-password with your own values.

Enable authentication

Edit the MongoDB config file:

sudo nano /etc/mongod.conf

Find the security section (it’s commented out by default) and add:

security:
  authorization: enabled

Restart MongoDB to apply:

sudo systemctl restart mongod

From now on, you’ll need to authenticate when connecting:

mongosh -u adminUser -p --authenticationDatabase admin

Configure Network Binding

By default, MongoDB only listens on 127.0.0.1 (localhost). If you need to connect from another machine (like your local dev machine through MongoDB Compass), edit /etc/mongod.conf:

net:
  port: 27017
  bindIp: 127.0.0.1,10.0.1.50

Replace 10.0.1.50 with your instance’s private IP. This lets MongoDB accept connections on both localhost and the private network.

Avoid setting bindIp: 0.0.0.0 on production instances. This makes MongoDB listen on all interfaces, including the public IP. If you must allow external access, use your EC2 Security Group to restrict port 27017 to specific IP addresses, and always have authentication enabled first.

Restart after changing the config:

sudo systemctl restart mongod

Manage the MongoDB Service

Action Command
Start sudo systemctl start mongod
Stop sudo systemctl stop mongod
Restart sudo systemctl restart mongod
Enable on boot sudo systemctl enable mongod
Check status sudo systemctl status mongod
View logs sudo journalctl -u mongod

Uninstall MongoDB

If you need to remove MongoDB completely:

sudo systemctl stop mongod
sudo apt-get purge -y mongodb-org*
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb

This stops the service, removes the packages, and deletes the data and log directories.

Conclusion

You now have MongoDB 8.0 Community Edition running on your EC2 Ubuntu 22.04 instance with authentication enabled and a service that starts on boot.

If your instance is running low on memory, adding swap space can help MongoDB perform better on smaller instance types. For a relational database alternative, check out How to Install MySQL Database on EC2 Ubuntu 22.04 LTS.