MongoDB is a document database that stores data as JSON-like documents, and it is the "M" in the MERN stack. This guide installs the current MongoDB 8.0 Community Edition on your own VPS running AlmaLinux 9, Rocky Linux 9 or Ubuntu 24.04, then secures it, connects an app and sets up backups. It applies to a VPS where you have root access, not to shared hosting.
Check that the VPS CPU supports AVX, add MongoDB's official 8.0 repository, install mongodb-org and start mongod. Before you put any data in it, create an admin user, turn on security.authorization and leave bindIp at 127.0.0.1, so the database is never open to the internet. Give each app its own user with access to one database, back up with mongodump, and reach it remotely only through an SSH tunnel.
Shared hosting has no MongoDB server and you can't install one. If you are deploying a MERN app on shared hosting, follow Deploy a Node.js app on shared hosting. Connecting from shared hosting to an outside MongoDB service such as MongoDB Atlas is not something we have verified, so ask support before you rely on it.
1. Before you start
You need:
- A VPS with root or sudo access, running AlmaLinux 9, Rocky Linux 9 or Ubuntu 24.04. MongoDB 8.0 also supports the version 8 releases of AlmaLinux and Rocky and Ubuntu 22.04; check MongoDB's platform list for anything else.
- At least 2 GB of RAM for a small production database; MongoDB uses about half of the RAM for its cache by default.
- A CPU that supports AVX. MongoDB 5.0 and later won't start without it on x86_64 servers. Check first:
grep -qw avx /proc/cpuinfo && echo "AVX OK" || echo "No AVX: MongoDB 5+ will not start"If AVX is missing, don't install the old 4.4 series as a workaround: it is end of life and gets no security fixes. Ask support about the VPS CPU, or run MongoDB somewhere else.
Update the system first: sudo dnf upgrade -y on AlmaLinux or Rocky, sudo apt update && sudo apt upgrade -y on Ubuntu.
2. Install on AlmaLinux 9 or Rocky Linux 9
Create the repository file:
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/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://pgp.mongodb.com/server-8.0.asc
EOFOn a version 8 system, change redhat/9 to redhat/8. Then install and start MongoDB:
sudo dnf install -y mongodb-org
sudo systemctl enable --now mongodIf SELinux is enforcing and mongod fails to start, follow the SELinux steps in MongoDB's installation manual rather than turning SELinux off.
3. Install on Ubuntu 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 noble/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 systemctl enable --now mongodOn Ubuntu 22.04, replace noble with jammy.
4. Check that it runs
systemctl status mongod
mongosh --eval 'db.runCommand({ ping: 1 })'{ ok: 1 } means the server answers. The shell is now mongosh; the old mongo shell was removed in MongoDB 6.0. The log is /var/log/mongodb/mongod.log, and the configuration file is /etc/mongod.conf.
5. Secure it before adding data
A fresh install has no password. Create an administrator while access is still open. passwordPrompt() asks for the password so it doesn't land in your shell history:
mongosh admin --eval 'db.createUser({user: "admin", pwd: passwordPrompt(), roles: [{role: "root", db: "admin"}]})'Then edit /etc/mongod.conf. It is YAML, so indentation matters (two spaces, no tabs):
net:
port: 27017
bindIp: 127.0.0.1
security:
authorization: enabledRestart and log in with the new user:
sudo systemctl restart mongod
mongosh -u admin -p --authenticationDatabase adminChanging bindIp to 0.0.0.0 opens port 27017 to the whole internet. Open MongoDB servers are found by automated scanners, and their data is copied and deleted for ransom. Keep bindIp: 127.0.0.1. To connect from your computer, use an SSH tunnel: ssh -N -L 27017:127.0.0.1:27017 user@your-vps-ip, then point Compass or mongosh at localhost:27017.
If another server genuinely needs direct access, bind to the VPS's private address, allow only that server's IP in the firewall, and turn on TLS.
6. Create an app user and connect
Give each application its own user with rights on its own database only:
// in mongosh, logged in as admin
use appdb
db.createUser({ user: "appuser", pwd: passwordPrompt(), roles: [{ role: "readWrite", db: "appdb" }] })The connection string for an app on the same VPS:
mongodb://appuser:[email protected]:27017/appdb?authSource=appdbKeep it in an environment variable or a .env file outside your web root, never in code you commit. If the password contains @, : or /, URL-encode it. For a complete MERN deployment with Nginx, PM2 and SSL, see Running MERN or MEAN on a clean VPS.
7. Backups, updates and monitoring
Back up with mongodump, which ships with the database tools installed alongside mongodb-org. As root:
mkdir -p /root/backups
mongodump --uri="mongodb://[email protected]:27017/?authSource=admin" --gzip --archive=/root/backups/mongo-$(date +%F).gzIt asks for the password. For a cron job, put password: YOUR_PASSWORD in a file only root can read (chmod 600) and add --config=/root/.mongodump.yaml. Run it daily, keep several days of archives, and copy them off the VPS: a backup on the same disk doesn't survive losing the server. Test a restore with mongorestore --gzip --archive=FILE on a spare database now and then.
Update within the 8.0 series with sudo dnf upgrade mongodb-org or sudo apt upgrade, then sudo systemctl restart mongod. A major upgrade (for example 7.0 to 8.0) goes one version at a time and needs the feature compatibility version set; follow MongoDB's upgrade notes and back up first.
Watch it with mongostat (operations per second) and mongotop (time spent per collection). Add indexes for the fields you query, and check slow queries with .explain("executionStats").
Remove it, if you ever need to: stop mongod, remove the mongodb-org packages, then delete /var/lib/mongo (AlmaLinux and Rocky) or /var/lib/mongodb (Ubuntu). Deleting that folder destroys all your data, so back up first.
8. Troubleshooting
| Symptom | Likely cause | What to do |
|---|---|---|
| Illegal instruction (core dumped) | The CPU has no AVX | Check with the command in section 1; ask support about the VPS CPU |
| mongod fails to start after editing the config | A YAML indentation error | Check /var/log/mongodb/mongod.log; use spaces, not tabs |
| Authentication failed | Wrong authSource or user | Log in with --authenticationDatabase set to the database where the user was created |
| Connection refused from another machine | bindIp is 127.0.0.1, as it should be | Use an SSH tunnel instead of opening the port |
| Fails to start on AlmaLinux or Rocky only | SELinux policy | Follow MongoDB's SELinux instructions |
9. Where Domain India fits
MongoDB needs a server you control. Our VPS is self-managed with full root access, KVM virtualisation and a choice of Linux distributions including AlmaLinux, Rocky Linux and Ubuntu, so everything above runs as written.
- 1 vCPU
- 2 GB DDR4 RAM
- 64 GB NVMe SSD Storage
- 2 TB Monthly Bandwidth
- 2 vCPU
- 4 GB DDR4 RAM
- 128 GB NVMe SSD Storage
- 3 TB Monthly Bandwidth
Prices on the cards are live and exclude 18% GST. If you are still choosing a database, Choosing between PostgreSQL, MySQL, SQLite and MongoDB compares them honestly. The App Platform includes a managed PostgreSQL database with every plan, not MongoDB.
Can I install MongoDB on Domain India shared hosting?
No. Shared hosting has no MongoDB server and you can't install one. Use a VPS, where you have root access. For a Node.js app on shared hosting, follow our shared hosting Node.js guide and ask support before relying on an outside database service.
Which MongoDB version should I install in 2026?
MongoDB 8.0, the current major release, or a newer one if MongoDB lists it, from MongoDB's official repository. The 4.4, 5.0 and 6.0 series are end of life and no longer get security fixes.
Why does mongod crash with "Illegal instruction"?
MongoDB 5.0 and later need a CPU with AVX support on x86_64. Check with grep avx /proc/cpuinfo. If AVX is missing, ask support about the VPS CPU rather than installing an end-of-life version.
How do I connect to MongoDB on my VPS from my computer?
Keep MongoDB bound to 127.0.0.1 and open an SSH tunnel with ssh -N -L 27017:127.0.0.1:27017 user@your-vps-ip. Then connect MongoDB Compass or mongosh to localhost:27017 with your database user.
What replaced the mongo shell?
mongosh. The old mongo shell was removed in MongoDB 6.0, and mongosh is installed with the mongodb-org package.
How do I back up MongoDB?
Use mongodump with --gzip and --archive to write a single compressed file, run it from cron, and copy the archives off the server. Restore with mongorestore.
Ready to run MongoDB? Compare VPS plans, then follow the MERN on a clean VPS playbook for the rest of the stack.
KVM VPS with full root access, NVMe storage and your choice of Linux distribution.
See VPS plans