How to Install MongoDB 6 on AWS EC2 (with Full Configuration)

MongoDB is a popular NoSQL database, and running it on AWS EC2 gives you flexibility and control. This guide walks you through installing MongoDB 6, configuring it for production, and setting up systemd, config files, and security.


1. Prepare Your EC2 Instance

  • Launch an EC2 instance (Amazon Linux 2, RHEL, or CentOS recommended).
  • Connect via SSH or AWS Session Manager.
  • Switch to root:
sudo su -

2. Download and Install MongoDB 6

Download the MongoDB 6 binaries and extract them:

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel90-6.0.17.tgz
tar -xvf mongodb-linux-x86_64-rhel90-6.0.17.tgz
cp mongodb-linux-x86_64-rhel90-6.0.17/bin/mongod /usr/local/bin/

3. Install Mongo Shell

wget https://downloads.mongodb.com/compass/mongosh-2.2.15-linux-x64-openssl3.tgz
tar -xvf mongosh-2.2.15-linux-x64-openssl3.tgz
cp mongosh-2.2.15-linux-x64-openssl3/bin/mongosh /usr/local/bin
cp mongosh-2.2.15-linux-x64-openssl3/bin/mongosh_crypt_v1.so /usr/lib/

4. Create MongoDB User and Data Directory

adduser --no-create-home mongo
mkdir -p /var/lib/mongod/data
chown -R mongo /var/lib/mongod

5. Create the MongoDB Configuration File

Create /var/lib/mongod/config.yml with the following content:

# /var/lib/mongod/config.yml
storage:
  engine: wiredTiger
  dbPath: /var/lib/mongod/data

operationProfiling:
  mode: slowOp
  slowOpThresholdMs: 200

net:
  bindIp: 0.0.0.0
  port: 27017

replication:
  replSetName: rs0
  oplogSizeMB: 128

security:
  authorization: disabled
  keyFile: /var/lib/mongod/keyfile

setParameter:
  enableLocalhostAuthBypass: true

6. Create the Keyfile for Internal Authentication

Create /var/lib/mongod/keyfile:

# /var/lib/mongod/keyfile
18bffc289024bbe2cc51f56d

Set permissions:

chown mongo:mongo /var/lib/mongod/keyfile
chmod 600 /var/lib/mongod/keyfile

7. Create the Systemd Service File

Create /etc/systemd/system/mongod.service:

# /etc/systemd/system/mongod.service
[Unit]
Description=mongod

[Service]
Type=simple
User=mongo
PIDFile=/var/lib/mongod/mongod.pid
ExecStart=/usr/local/bin/mongod --config /var/lib/mongod/config.yml
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
RestartSec=60
StandardOutput=syslog
StandardError=syslog

[Install]
WantedBy=multi-user.target

8. Start and Enable MongoDB

systemctl daemon-reload
systemctl enable --now mongod

9. Initialize the Replica Set

Connect to MongoDB:

mongosh admin

Initiate the replica set (adjust host IPs as needed):

rs.initiate({
  _id: "rs0",
  members: [
    { _id: 0, host: "10.10.10.10:27017" },
    { _id: 1, host: "10.10.10.20:27017" }
  ]
})

10. Create Admin Users

db.createUser({ user: "root", pwd: "abc123123", roles: ["root"] })
db.auth("root", "abc123123")
db.createUser({
  user: "adminuser",
  pwd: "cde123123",
  roles: [
    { role: "userAdminAnyDatabase", db: "admin" },
    { role: "readWriteAnyDatabase", db: "admin" }
  ]
})

11. Enable Authentication

Stop MongoDB:

systemctl stop mongod

Edit /var/lib/mongod/config.yml:

# ...existing config...
security:
  authorization: enabled
  keyFile: /var/lib/mongod/keyfile

setParameter:
  enableLocalhostAuthBypass: false

Restart MongoDB:

systemctl start mongod

12. Connect and Test

Connect using your admin user:

mongosh -u adminuser -p adminpassword --authenticationDatabase admin

13. (Optional) Convert to Standalone if Needed

If a node is down and you want to run as a single node:

rs.reconfig({ _id: "rs0", members: [{ _id: 0, host: "10.10.10.10:27017" }] }, { force: true })

Conclusion

You now have a secure, production-ready MongoDB 6 instance running on EC2, with all configs and systemd integration. Adjust IPs, users, and passwords as needed for your environment.