π§ MongoDB Linux Installation
Complete step-by-step guide to install MongoDB Server on Ubuntu, Debian, RHEL, CentOS, and Fedora
Installation Overview
What you need to know
This guide covers MongoDB Community Edition installation on popular Linux distributions using official MongoDB repositories. We'll use package managers (apt/yum/dnf) for easy installation and automatic updates.
- You need sudo/root privileges for installation
- MongoDB requires 64-bit Linux systems
- Minimum 2 GB RAM recommended (4 GB+ for production)
- Default data directory:
/var/lib/mongodb - Default config file:
/etc/mongod.conf - Default port:
27017
System Requirements
Supported distributions
- Ubuntu: 20.04 LTS, 22.04 LTS, 24.04 LTS
- Debian: 10 (Buster), 11 (Bullseye), 12 (Bookworm)
- RHEL/CentOS: 7, 8, 9
- Fedora: 37, 38, 39
- Architecture: x86_64 (64-bit)
- RAM: Minimum 2 GB (4 GB+ recommended)
- Disk: 5 GB free space minimum
Ubuntu / Debian Installation
Using apt package manager
Import the public key used by the package management system to verify package authenticity.
Create a list file for MongoDB in your sources list directory.
For Ubuntu 22.04 (Jammy):
For Ubuntu 20.04 (Focal):
For Debian 12 (Bookworm):
lsb_release -a
Reload the local package database to include MongoDB packages.
Install the latest stable version of MongoDB Community Edition.
Start the MongoDB service and enable it to start on boot.
RHEL / CentOS / Fedora Installation
Using yum or dnf package manager
Create a .repo file to install MongoDB with yum or dnf.
For RHEL 9 / Rocky Linux 9 / AlmaLinux 9:
For RHEL 8 / CentOS 8:
Install MongoDB using yum (RHEL 7/8) or dnf (RHEL 9/Fedora).
Using yum (RHEL 7/8):
Using dnf (RHEL 9/Fedora):
Start and enable MongoDB to run on system boot.
Verify Installation
Confirm MongoDB is working
Use the MongoDB Shell (mongosh) to connect and verify.
Verify MongoDB is running as a system service.
Basic Configuration
Essential settings
MongoDB's configuration file is located at /etc/mongod.conf
Troubleshooting
Common issues and solutions
Symptoms: systemctl start mongod fails
Solutions:
- Check logs:
sudo journalctl -xe -u mongod - Verify data directory permissions:
$ sudo chown -R mongodb:mongodb /var/lib/mongodb $ sudo chown mongodb:mongodb /tmp/mongodb-27017.sock
- Check if port 27017 is already in use:
sudo lsof -i :27017 - Verify config file syntax:
mongod --config /etc/mongod.conf --test
Symptoms: mongosh connection fails
Solutions:
- Verify MongoDB is running:
sudo systemctl status mongod - Check firewall:
# Ubuntu/Debian (ufw) $ sudo ufw allow 27017 # RHEL/CentOS (firewalld) $ sudo firewall-cmd --permanent --add-port=27017/tcp $ sudo firewall-cmd --reload
- Check bindIp in
/etc/mongod.conf
Symptoms: "Package mongodb-org not found"
Solutions:
- Verify repository was added correctly
- Update package database:
# Ubuntu/Debian $ sudo apt-get update # RHEL/CentOS $ sudo yum clean all && sudo yum update
- Check internet connectivity
- Verify correct distribution codename in repository URL
Congratulations! MongoDB is now running on your Linux system. Here's what to do next:
- Secure MongoDB: Enable authentication and create admin user
- Install MongoDB Compass: Get the GUI tool for easier management
- Learn mongosh: Practice MongoDB shell commands
- Create Databases: Start building your applications
- Set Up Backups: Configure regular backups with mongodump
- Monitor Performance: Use built-in monitoring tools