Chapter 12: Technology Stacks — LAMP, LEMP, MEAN, MERN
12.5 Installing MongoDB from the official repository
Dhyan do — always install MongoDB from the official MongoDB repository, not random tutorials. Type these commands with me for your OS.
The MongoDB package in some distribution repositories is outdated or missing, so we always use the official MongoDB repository. We use MongoDB 8.0 (a current production release series); for a different version replace 8.0 in the commands.
Amazon Linux 2023
sudo tee /etc/yum.repos.d/mongodb-org-8.0.repo > /dev/null <<'EOF'
[mongodb-org-8.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2023/mongodb-org/8.0/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://pgp.mongodb.com/server-8.0.asc
EOF
sudo yum install -y mongodb-mongosh-shared-openssl3
sudo yum install -y mongodb-org
sudo service mongod start
sudo systemctl enable mongod
Ubuntu 22.04 / 24.04
sudo apt install -y gnupg curl
curl -fsSL https://pgp.mongodb.com/server-8.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu $(. /etc/os-release && echo $VERSION_CODENAME)/mongodb-org/8.0 multiverse" | \
sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
sudo apt update
sudo apt install -y mongodb-org
sudo service mongod start
sudo systemctl enable mongod
($VERSION_CODENAME is jammy on 22.04 and noble on 24.04.)
CentOS Stream 9 (RHEL 9 family)
sudo tee /etc/yum.repos.d/mongodb-org-8.0.repo > /dev/null <<'EOF'
[mongodb-org-8.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/9/mongodb-org/8.0/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://pgp.mongodb.com/server-8.0.asc
EOF
sudo yum install -y mongodb-org
sudo service mongod start
sudo systemctl enable mongod
MongoDB and SELinux on CentOS
With SELinux enforcing, mongod may log permission denials (or fail to start) because of its monitoring features. MongoDB publishes an official SELinux policy (the mongodb-selinux project on MongoDB's GitHub) — install it as described in the MongoDB installation docs for RHEL. For a quick lab, check sudo service mongod status and sudo ausearch -m avc -ts recent.
Verify and basic use
sudo service mongod status
sudo ss -tlnp | grep 27017 # should show 127.0.0.1:27017 only
mongosh --eval 'db.runCommand({ ping: 1 })'
| Item | Value |
|---|---|
| Service | mongod |
| Config file | /etc/mongod.conf (YAML) |
| Data directory | /var/lib/mongo (RPM) or /var/lib/mongodb (Ubuntu) |
| Log | /var/log/mongodb/mongod.log |
| Default bind | 127.0.0.1 port 27017 |
| Shell | mongosh |
// inside mongosh
show dbs
use notesdb
db.notes.insertOne({ text: "Hello MongoDB", createdAt: new Date() })
db.notes.find()
Never expose MongoDB to the internet
Keep bindIp: 127.0.0.1 in /etc/mongod.conf and do not open 27017 in the security group. Many databases have been wiped by attackers because they were left open without authentication. For production also enable authentication: create an admin user in mongosh (use admin then db.createUser({user:"admin", pwd:passwordPrompt(), roles:["root"]})), add security: / authorization: enabled to mongod.conf, and restart mongod.