Chapter 12: Technology Stacks — LAMP, LEMP, MEAN, MERN
12.5 Installing MongoDB from the official repository
Pay attention — always install MongoDB from the official MongoDB repository, not random tutorials. Type these commands with me for your OS. An old package from a random tutorial causes a lot of trouble later.
लक्ष द्या — MongoDB नेहमी ऑफिशियल MongoDB रिपॉझिटरीमधून इन्स्टॉल करा, कुठल्याही ट्युटोरियलमधून नाही. तुमच्या OS साठी या कमांड्स माझ्यासोबत टाइप करा. रँडम ट्युटोरियलमधलं जुनं पॅकेज नंतर खूप त्रास देतं.
ध्यान दो — MongoDB हमेशा ऑफ़िशियल MongoDB रिपॉज़िटरी से इंस्टॉल करो, किसी भी ट्यूटोरियल से नहीं. अपने 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.
Ravindra Bagale's Tip
After installing MongoDB, first run sudo ss -tlnp | grep 27017 – you should see only 127.0.0.1:27017 there. Don't change bindIp to 0.0.0.0 and never open 27017 in the security group; for production, turn on authentication too.
Ravindra Bagale's Tip – मराठी
MongoDB install झाल्यावर आधी sudo ss -tlnp | grep 27017 चालवा – तिथे फक्त 127.0.0.1:27017 दिसायला हवं. bindIp बदलून 0.0.0.0 करू नका आणि 27017 security group मध्ये कधीच open करू नका; production साठी authentication पण चालू करा.
Ravindra Bagale's Tip – हिंदी
MongoDB install होने के बाद पहले sudo ss -tlnp | grep 27017 चलाओ – वहाँ सिर्फ़ 127.0.0.1:27017 दिखना चाहिए. bindIp बदलकर 0.0.0.0 मत करो और 27017 security group में कभी open मत करो; production के लिए authentication भी चालू करो.
Lab
Chala, tumchya OS sathi MongoDB install kara, mongosh --eval 'db.runCommand({ ping: 1 })' chalva aani ok: 1 yetoy ka te bagha. Mag mongosh madhe use notesdb karun Mauli Dairy sathi ek note insert kara aani db.notes.find() ne ti disate ka te check kara.