Upgrading MongoDB on Ubuntu can feel risky because a failed upgrade may leave your database inaccessible or, worse, corrupt your data. This guide walks you through a safe, step-by-step upgrade process that preserves all your data, using official MongoDB repositories and built-in backup tools. You’ll learn how to back up, upgrade, verify, and roll back if something goes wrong.
What You’ll Need
- A Ubuntu server (20.04, 22.04, or 24.04) with sudo/root access.
- An existing MongoDB installation (version 4.x or newer) that you want to upgrade.
- Enough free disk space for the backup (at least the size of your current data directory).
- Internet access to download MongoDB packages from the official repository.
- Basic familiarity with the command line.
Step-by-Step
- Check your current MongoDB version. Run
mongod --versionand note the version. Also check the Ubuntu version withlsb_release -a. This helps you pick the correct repository. - Back up your data. The safest method is to use
mongodump. Stop the MongoDB service first to ensure a consistent snapshot:sudo systemctl stop mongod mongodump --out /backup/mongodb-backup-$(date +%F)
If you can’t stop the service, use
mongodumpwith--oplogfor a point-in-time backup (requires a replica set). Verify the backup by listing the files in the output directory. - Update the MongoDB repository. MongoDB maintains separate repos for each major version. To upgrade from 4.4 to 5.0, replace the old repo with the new one. For Ubuntu 20.04 (Focal):
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add - echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
Adjust the version and codename (focal, jammy, noble) as needed. For MongoDB 6.0+ use the newer
.debapproach withcurlanddpkgif needed. - Refresh package lists. Run
sudo apt updateto fetch the new repository metadata. - Upgrade MongoDB packages. Use
sudo apt install -y mongodb-orgto upgrade to the latest version in the repo. Alternatively, usesudo apt upgradebut be careful—it may upgrade all packages. Themongodb-orgmetapackage ensures all components (server, shell, tools) are updated together. - Start MongoDB and verify. Start the service:
sudo systemctl start mongod. Check the version:mongod --versionandmongosh --eval 'db.runCommand({buildInfo:1})'. Ensure the service is running:sudo systemctl status mongod. - Run a health check. Connect with
mongoshand run a simple query:db.runCommand({ping:1}). Also check the logs at/var/log/mongodb/mongod.logfor any errors. - Upgrade the feature compatibility version (FCV). After a major upgrade, MongoDB may still run in the old compatibility mode. To enable new features, set the FCV to the new version:
mongosh --eval 'db.adminCommand({setFeatureCompatibilityVersion: "5.0"})'Replace “5.0” with your new version. This step is optional but recommended for full functionality.
- Restore data if needed. If the upgrade fails or you want to revert, restore from your backup:
sudo systemctl stop mongod mongorestore --drop /backup/mongodb-backup-YYYY-MM-DD
The
--dropflag removes existing collections before restoring to avoid duplicates.
Common Mistakes
- Skipping the backup. Even minor upgrades can fail due to disk issues or power loss. Always have a backup.
- Upgrading multiple major versions at once. MongoDB supports upgrades only one major version at a time (e.g., 4.4 → 5.0 → 6.0). Jumping from 4.4 to 6.0 directly can cause data corruption.
- Using the wrong repository codename. Ubuntu versions have different codenames (focal, jammy, noble). Using the wrong one leads to package not found errors.
- Forgetting to stop the service before backup. If you run
mongodumpwhile the server is active, you may get an inconsistent backup. Stop the service or use--oplog. - Not updating the FCV. After a major upgrade, old features may still work, but you lose access to new features and may encounter compatibility issues with drivers.
Tips
- Test the upgrade on a staging server. Replicate your production environment and test the upgrade there first.
- Use a replica set for zero downtime. If you have a replica set, upgrade secondaries first, then step down the primary and upgrade it. This avoids downtime.
- Monitor disk space. Ensure you have at least 20% free disk space before starting, as the upgrade process may need extra space for temporary files.
- Keep the backup until you’re confident. After a successful upgrade, keep the backup for at least a week.
- Check the release notes. Each MongoDB version has specific upgrade paths and deprecated features. Read the official release notes for your target version.
Troubleshooting
Service fails to start after upgrade. Check the log file /var/log/mongodb/mongod.log for errors. Common issues include incompatible storage engine or missing configuration. If the error mentions “Unsupported storage engine”, you may need to dump and restore data instead.
Package not found. Verify the repository URL and codename. Run apt-cache policy mongodb-org to see available versions. If the repo is wrong, update the sources list and run apt update again.
Connection refused. Ensure MongoDB is running: sudo systemctl status mongod. Check if the port (27017) is open and that the configuration file /etc/mongod.conf has the correct bindIp (e.g., 0.0.0.0 for remote access).
Data appears missing after upgrade. If you restored from backup incorrectly, you may have overwritten the new data. Use mongorestore --drop carefully. If the upgrade itself succeeded, your data should be intact; verify with db.stats().
FCV set fails. If you get an error like “cannot set featureCompatibilityVersion”, ensure all nodes in a replica set are upgraded and reachable. Also, downgrade is not possible after setting FCV, so only do it when ready.
Frequently Asked Questions
Can I upgrade directly from MongoDB 4.0 to 6.0?
No. MongoDB supports upgrades only one major version at a time. You must upgrade from 4.0 to 4.2, then to 4.4, then to 5.0, and finally to 6.0. Each step requires a full backup.
Is mongodump enough for a backup?
Yes, mongodump creates a logical backup of your data. It’s sufficient for most databases, but for large databases, a file-system snapshot (using LVM or a cloud snapshot) is faster and more consistent. Always test your backup by restoring it on a scratch server.
What is the feature compatibility version (FCV)?
FCV is a setting that tells MongoDB which version’s features to enable. After a major upgrade, the FCV remains at the old version until you explicitly set it. This allows you to downgrade if needed, but you must set it to the new version to access new features.
Do I need to upgrade the MongoDB shell separately?
No, the mongodb-org metapackage includes the server, shell (mongosh), and tools. Installing that package updates all components together.
What if I run out of disk space during the upgrade?
Free up space by removing old backups or logs, then retry the upgrade. If the upgrade partially completed, you may need to restore from backup and start over.
Can I downgrade after upgrading?
You can downgrade only if you haven’t set the FCV to the new version. If you have set it, you must restore from a backup taken before the upgrade. Always test the upgrade on a non-production server first.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Facebook Keeps Crashing on iOS 16? Fixes That Work
- Send Apple Pay in a Group Chat: iPhone & iPad
- Fix Cortana Not Working in Windows 11
- Fix iPhone Speaker Crackling: Step-by-Step
- Antimicrobial USB Drives: Claims and Limits
- How to Enable Developer Mode on Discord