To install MongoDB on a Linux server, add the official MongoDB repository for your distribution, install the mongodb-org package, start and enable the mongod service, then secure it by creating an admin user and enabling authentication. This step-by-step guide covers Ubuntu/Debian, RHEL/CentOS/Amazon Linux, firewall rules, remote access, and production hardening.
If you want to install MongoDB on a Linux server the right way—fast, secure, and ready for production—this guide walks you through every step. We’ll use the official MongoDB repositories, cover Ubuntu/Debian and RHEL/CentOS/Amazon Linux, show you how to start the service, enable authentication, allow remote connections safely, and optimize for performance.
What Is MongoDB and Why Use the Official Repository?

MongoDB is a popular NoSQL document database used for scalable apps, analytics, and microservices. Installing from the official repository ensures you receive the latest stable builds, security patches, and tools like mongosh. Avoid random third-party builds and old snaps if you’re setting up a server—go with mongodb-org packages for reliability.
Prerequisites and Best Practices
Before you install MongoDB on a Linux server, make sure you have:
- A supported OS: Ubuntu 22.04/20.04, Debian 12/11, RHEL 9/8, CentOS Stream, Amazon Linux 2
- Root or sudo access
- Outbound internet access for repositories
- Time sync enabled (chrony or systemd-timesyncd) to avoid replica set issues
- Firewall access planned (allow only trusted IPs to port 27017)
Production tips from experience: dedicate local SSD storage, keep OS and MongoDB on separate volumes if possible, and avoid running multiple heavy services on the same VM.
Install MongoDB on Ubuntu and Debian
These steps use MongoDB 7.0 as an example. Replace jammy or bookworm with your OS codename if needed.
Ubuntu 22.04 (Jammy) / 20.04 (Focal)
sudo apt-get update
sudo apt-get install -y curl gnupg
# Import MongoDB public GPG key
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-7.0.gpg
# Add the MongoDB repository (Jammy example)
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
# Start and enable the service
sudo systemctl start mongod
sudo systemctl enable mongod
# Verify
mongod --version
mongosh --eval "db.runCommand({ connectionStatus: 1 })"
Debian 12 (Bookworm) / 11 (Bullseye)
sudo apt-get update
sudo apt-get install -y curl gnupg
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-7.0.gpg
# Debian Bookworm example
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/debian bookworm/mongodb-org/7.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo systemctl start mongod
sudo systemctl enable mongod
mongosh --eval "db.serverStatus().ok"
The MongoDB config file is located at /etc/mongod.conf, data at /var/lib/mongo, and logs at /var/log/mongodb/mongod.log.
Install MongoDB on RHEL, CentOS, and Amazon Linux
RHEL 9/8 and CentOS Stream
sudo tee /etc/yum.repos.d/mongodb-org-7.0.repo >/dev/null <<'EOF'
[mongodb-org-7.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/7.0/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://pgp.mongodb.com/server-7.0.asc
EOF
sudo dnf install -y mongodb-org
sudo systemctl start mongod
sudo systemctl enable mongod
mongosh --eval "db.adminCommand({ buildInfo: 1 }).version"
Amazon Linux 2
sudo tee /etc/yum.repos.d/mongodb-org-7.0.repo >/dev/null <<'EOF'
[mongodb-org-7.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/7.0/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://pgp.mongodb.com/server-7.0.asc
EOF
sudo yum install -y mongodb-org
sudo systemctl start mongod
sudo systemctl enable mongod
Verify the Service and Basic Commands
After installation, confirm MongoDB is running and set to start on boot.
# Service status
systemctl status mongod
# Check listening port (27017)
ss -tulpn | grep 27017
# Basic check with mongosh
mongosh --eval "db.runCommand({ ping: 1 })"
If the service fails, review /var/log/mongodb/mongod.log for errors such as permissions, port conflicts, or storage issues.
Secure MongoDB: Create Admin User and Enable Authentication
By default, MongoDB listens on localhost and allows connections without auth. In production, you must enable authorization and create an administrative user.
1) Create the first admin user
mongosh
use admin
db.createUser({
user: "siteAdmin",
pwd: passwordPrompt(), // or "StrongSecret#2025"
roles: [ { role: "root", db: "admin" } ]
})
quit()
2) Enable authorization
sudo cp /etc/mongod.conf /etc/mongod.conf.bak
sudo nano /etc/mongod.conf
# Add or edit:
security:
authorization: enabled
sudo systemctl restart mongod
# Authenticate as admin
mongosh -u siteAdmin -p --authenticationDatabase admin --eval "db.runCommand({ connectionStatus: 1 })"
Use unique users per application database, grant only necessary roles, and store credentials securely (e.g., environment variables or a secret manager).
Allow Remote Connections Safely
MongoDB binds to 127.0.0.1 by default. To allow remote access, bind to a specific private IP (recommended) and open the firewall only to trusted sources.
Update bindIp
sudo nano /etc/mongod.conf
# Example: allow local and a private LAN IP
net:
port: 27017
bindIp: 127.0.0.1,10.0.0.5
sudo systemctl restart mongod
Open the firewall (UFW or firewalld)
# UFW (Ubuntu/Debian) - allow a trusted subnet
sudo ufw allow from 203.0.113.0/24 to any port 27017 proto tcp
sudo ufw status
# firewalld (RHEL/CentOS/Amazon Linux)
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.0/24" port protocol="tcp" port="27017" accept'
sudo firewall-cmd --reload
SELinux notes
Default ports and paths work with SELinux enforcing. If you change the port (e.g., 27018), label it:
sudo dnf install -y policycoreutils-python-utils || true
sudo semanage port -a -t mongod_port_t -p tcp 27018
sudo systemctl restart mongod
Performance and Production Hardening
For stable, high-throughput deployments, apply these proven optimizations.
- Filesystem and storage: Use XFS on SSD/NVMe for WiredTiger. Avoid network filesystems for primary data.
- Disable Transparent Huge Pages (THP): Improves consistency on most workloads.
- File descriptors/process limits: Increase nofile and nproc for mongod.
- Swap: Keep minimal but not zero; monitor swappiness.
- Backups and snapshots: Implement routine mongodump and volume snapshots.
Disable Transparent Huge Pages (temporary)
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/defrag
Make this change persistent via a systemd unit or rc.local script on reboot. Check your distribution’s recommended method.
Raise file descriptor limits
echo -e "* soft nofile 64000\n* hard nofile 64000\nmongod soft nproc 64000\nmongod hard nproc 64000" | sudo tee /etc/security/limits.d/99-mongodb.conf
sudo systemctl restart mongod
Confirm with cat /proc/$(pidof mongod)/limits and adjust as your workload grows.
Backup and Restore MongoDB
Logical backups with mongodump/mongorestore
# Backup all databases
mongodump --authenticationDatabase admin -u siteAdmin -p --out /backup/mongodump-$(date +%F)
# Restore a specific database
mongorestore --nsInclude mydb.* /backup/mongodump-YYYY-MM-DD
Filesystem snapshots
For large datasets, use LVM or cloud volume snapshots. To ensure consistency, flush writes (fsfreeze) or stop mongod briefly during the snapshot window. Restore procedures should be tested regularly.
Troubleshooting Common Errors
- Service won’t start: Check /var/log/mongodb/mongod.log. Look for bad YAML indentation in /etc/mongod.conf, insufficient permissions on /var/lib/mongo, or port conflicts.
- Address already in use: Another process is using 27017. Find it with ss -tulpn | grep 27017 and stop or change MongoDB’s port.
- Authentication failed: Ensure you’re connecting to the correct auth DB (use –authenticationDatabase admin) and that authorization: enabled is set.
- Remote connection refused: Confirm bindIp includes your server IP and your firewall allows your client IP to 27017.
- Repair after crash: As a last resort, stop mongod and run sudo -u mongodb mongod –config /etc/mongod.conf –repair, then start the service.
Install a Specific MongoDB Version
You can pin a specific series (e.g., 6.0) by using the corresponding repository and packages.
# Debian/Ubuntu example: install a fixed version
sudo apt-get install -y mongodb-org=7.0.12 mongodb-org-database=7.0.12 mongodb-org-server=7.0.12 mongodb-mongosh mongodb-org-mongos=7.0.12 mongodb-org-tools=7.0.12
# Hold versions to prevent unintended upgrades
sudo apt-mark hold mongodb-org mongodb-org-database mongodb-org-server mongodb-org-mongos mongodb-org-tools
Adjust exact version numbers to match those listed by apt-cache policy mongodb-org or dnf list mongodb-org –showduplicates.
Docker vs Native Packages
Docker can be great for dev and CI pipelines, but for production databases, native packages or managed services are often simpler for I/O performance, predictable upgrades, and observability. If you containerize, mount dedicated volumes, pin resource limits, and manage health checks carefully.
Run MongoDB on YouStable Servers
If you host on YouStable VPS or Cloud, choose Ubuntu 22.04 or AlmaLinux 9 for a smooth MongoDB installation. Our NVMe storage plans deliver low-latency I/O, and our support team can help configure firewalls, PTR records, snapshots, and monitoring so your database stays fast and secure.
Uninstall MongoDB (If Needed)
# Ubuntu/Debian
sudo systemctl stop mongod
sudo apt-get purge -y mongodb-org*
sudo rm -rf /var/log/mongodb /var/lib/mongo /etc/mongod.conf
# RHEL/CentOS/Amazon Linux
sudo systemctl stop mongod
sudo dnf remove -y mongodb-org*
sudo rm -rf /var/log/mongodb /var/lib/mongo /etc/mongod.conf
FAQ’s: Install MongoDB on Linux Server
How do I install MongoDB 7 on Ubuntu 22.04 quickly?
Import the MongoDB GPG key, add the 7.0 repo for jammy, run apt-get update, install mongodb-org, then systemctl start mongod and systemctl enable mongod. The full commands are listed above in the Ubuntu section.
What is the difference between mongodb-org and OS-provided mongodb packages?
mongodb-org comes from MongoDB’s official repo with current versions, tools, and security updates. OS repositories can be outdated or lack features. For production, use the official mongodb-org packages.
Where is the MongoDB config file on Linux?
On most distributions the config file is /etc/mongod.conf. Data is in /var/lib/mongo and logs are in /var/log/mongodb/mongod.log. Always back up the config before editing.
How do I allow remote connections safely?
Add your server’s private IP to bindIp in /etc/mongod.conf, enable authorization, create users with strong passwords, and limit port 27017 with UFW or firewalld to trusted IPs only. Avoid binding to 0.0.0.0 unless behind strict network controls.
Mongosh vs mongo: which shell should I use?
mongosh is the modern, supported MongoDB shell and ships with current mongodb-org packages. It replaces the legacy mongo shell and offers better compatibility and tooling.
How can I install a specific MongoDB version (e.g., 6.0) on RHEL/CentOS?
Create a repo file pointing to the 6.0 series URL, then run dnf install mongodb-org-6.0.x. Use dnf list –showduplicates to select an exact version and consider version locks to avoid unintended upgrades.
Why does mongod fail to start after enabling authorization?
Often it’s a YAML indentation error or a typo in /etc/mongod.conf. Check mongod.log for details. Fix the config, then sudo systemctl restart mongod. If the first admin user wasn’t created before enabling authorization, temporarily disable auth, create the user, and re-enable it.
With these steps, you can confidently install MongoDB on a Linux server, secure it, and keep it performing under real-world workloads. If you need optimized infrastructure for MongoDB, YouStable’s VPS and Cloud instances offer the performance headroom and support your database deserves.