Checking version and upgrading MongoDB on Ubuntu

Updated: February 1, 2024 By: Guest Contributor Post a comment

Introduction

MongoDB is a highly popular, NoSQL database that is widely used for its scalability and flexibility. As with any software, staying updated with the latest version of MongoDB is crucial for security, performance enhancements, and gaining access to new features. This tutorial will take you through the process of checking your current MongoDB version and upgrading it to the latest release on an Ubuntu system.

Checking Your Current MongoDB Version

To start, you’ll need to determine which version of MongoDB is currently installed on your system. This is easily done the command line interface. Open the terminal on your Ubuntu system and enter the following command:

mongod --version

The output should resemble something like this:

MongoDB server version: 4.4.6

This indicates that MongoDB version 4.4.6 is currently installed. Now that you know which version you have, you can decide whether to upgrade.

Preparing for the Upgrade

Before upgrading, it’s a good practice to backup your MongoDB databases. You can use the mongodump command to do so:

mongodump --out /path/to/backup/dir

Next, you should stop the currently running MongoDB service:

sudo systemctl stop mongod

This command will halt the MongoDB service, allowing you to safely upgrade the software.

Uninstalling the Old Version

If required, uninstall the current version of MongoDB by using:

sudo apt-get remove mongodb mongodb-server

This command will remove the existing MongoDB package from your system. However, it won’t delete your data files or the configurations.

Upgrading MongoDB on Ubuntu

To install the newer version of MongoDB, first, update your repository list:

sudo apt-get update

Choose the official MongoDB source by adding it to your repository list. Import the MongoDB public key to ensure your Ubuntu system trusts the new packages:

wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

Add the MongoDB repository:

sudo sh -c 'echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -sc)/mongodb-org/5.0 multiverse" > /etc/apt/sources.list.d/mongodb-org-5.0.list'

Once again, update your repository list:

sudo apt-get update

Now, you can install the latest version of MongoDB. For example, to install version 5.0:

sudo apt-get install -y mongodb-org

Note: Make sure you use the appropriate version number apt for your upgrade.

Running the MongoDB Server

Post-installation, start the MongoDB server:

sudo systemctl start mongod

Check the status to ensure MongoDB is running properly:

sudo systemctl status mongod

After the MongoDB services are up and running, you can verify the update:

mongod --version

The output should now reflect the new version number, confirming that the upgrade is successful.

Handling Configuration Differences

Differences in configuration between versions might exist. It’s important to review the release notes of MongoDB between your old and new versions. You may need to tweak your configuration files accordingly. MongoDB configuration file can be found at:

/etc/mongod.conf

Make sure to review and update your configurations to suit the version you have upgraded to.

Troubleshooting Post-upgrade

Sometimes after an upgrade, you might encounter issues. If MongoDB fails to start, check the logs for details:

cat /var/log/mongodb/mongod.log

Review the error messages and consult the MongoDB manual or MongoDB discussion forums for solutions tailored to your specific errors.

Optimizing After the Upgrade

After a successful upgrade, it is recommended to run the db.adminCommand({setFeatureCompatibilityVersion: ""}) on each of your databases. This will ensure that your databases are fully utilizing the capabilities of the updated MongoDB version.

Conclusion

Upgrading MongoDB on Ubuntu is a straightforward process, yet it requires careful execution. By following the steps in this guide, you can ensure that your MongoDB installation remains secure, efficient, and feature-rich. Always remember to backup your data first, and to review the release notes and MongoDB manual for the version to which you’re upgrading.