Lesson 3: MongoDB Server Upgrades / Learn

Video Transcript (English)

Upgrading to a new version of MongoDB is a common task for a database administrator. In this video we’ll go through the process of upgrading a replica set to a new version and explain each step in detail.

For this demonstration, we’ll upgrade a replica set from version 5.0 to 6.0 on Ubuntu Linux. These steps can be used for any upgrade with a few changes related to compatibility. Always read the upgrade instructions in the release notes, as each upgrade may be different.

Before upgrading, we recommend taking some preliminary steps. We’ll refer to this as our pre-upgrade checklist.

First, check your language driver documentation to confirm that your driver is compatible with the version you are upgrading to.

Next, we need to determine our current major version and plan the upgrade path. Upgrades that cross major releases must be done incrementally.

For example, if you are running 5.0, you must first upgrade to 6.0 before you can upgrade to 7.0.

To check the version of the replica set members, we need to connect to each member with the MongoDBShell and run db.version.

This command should return information about the current version. In this case, it confirms that we’re running a 5.0 major release.

It’s recommended that you have the latest patch for the major version you have installed. This can make the upgrading process smoother in some cases.

Now that we have confirmed the version of each replica set member, we need to check the feature compatibility version on the primary.

The feature compatibility version, or FCV, enables or disables new features that persist data incompatible with earlier versions of MongoDB.

Let's begin by confirming our feature compatibility version by running an admin command that expects getParameter and featureCompatibilityVersion as arguments.

The version should be set to 5.0 in our case, which it is.

The next step in our pre-upgrade checklist is to determine how much time we have to complete maintenance. The time is based on the size of the oplog and the difference between the oldest and most recent operations in the oplog. This is called the oplog window.

To determine the oplog window, we use the printReplicationInfo() command.

Here we have a field called log length start to end, which tells us our oplog window.

This gives us a rough estimate of how much time we have to perform maintenance on a single node. If we go beyond the oplog window, we risk our node having to perform an initial sync, which increases the maintenance time.

Changes in workload can rapidly change the oplog window. We recommend performing maintenance when the workload is stable and preferably low.

The next step in our pre-upgrade checklist is to confirm that the replica set member state is healthy.

This means we need to ensure that no replica set member is in the ROLLBACK or RECOVERING state. To do this, we use the rs.status() command with some filter logic to focus on the node state.

Inside the members array, we can see the State field is set to either PRIMARY or SECONDARY, which is perfect.

Next, we should check that the secondaries are not trying to catch up from a previous event. To do this, we run the rs.printSecondaryReplicationInfo() command.

To confirm, we check the replLag field. In this case, it appears that we’re not experiencing any lag.

That completes our upgrade checklist! Now, let’s explain the steps involved in the upgrade process.

We’ll start by replacing the old binaries with the new binaries. In this case, we’re replacing 5.0 with 6.0.

Then, we’ll test our application with the 6.0 binaries and the FCV still set to 5.0.

Once we’re confident that our application works properly, we can set FCV to 6.0 and continue testing our application.

We are ready to install the 6.0 package. For this demonstration we’ll use the package manager, but you also have the option to download the binaries manually.

We’ll need to log in to one of the secondaries.

Once logged in, we need to gracefully shut down the mongod service by using systemctl.

Next, we import the GPG keys.

Then, we create a list file for the new 6.0 package repository by using this command.

We reload the local package database.

Now, we install the latest version. Because this is the community edition, we use the mongodb-org package. The upgrade process may prompt to replace existing configuration files with updated versions. In this example we will keep the existing ones.

Finally, we restart the mongod service.

Let’s grab the version of our running secondary member and confirm it has successfully upgraded.

We need to wait until this secondary catches up to the rest of the cluster. Then we can move on to the next secondary and repeat these steps.

Because the steps are the same, we won't show the process again.

Now that both secondaries are upgraded and up to date, we will repeat the steps for the primary. We’ll elect a new primary from one of our upgraded secondaries, and then upgrade the final node.

Let’s log in to the existing primary by using a direct connection. Here we assume mongod0 is the existing primary.

Next, we confirm this is in fact our current primary.

Now we run rs.stepDown to elect a new primary. You may have to wait a few seconds for the election to complete.

Then we reprint the same information and compare it to see our new primary.

We can confirm that a new primary was elected. Now we exit the shell.

The next thing we need to do is repeat the steps that we followed for the previous nodes.

Now we can test our upgraded database using feature compatibility version 5.0 before setting it to 6.0.

Our application testing may uncover issues that require us to downgrade, even though we set the feature compatibility. Downgrading may require downtime. For this reason, we strongly recommend testing in a pre-production environment, where a downgrade may not be necessary.

If you end up having to downgrade, we strongly recommend you open up a support ticket to help you properly downgrade your system.

Once every node is up to date and tested, we reconnect to the replica set.

It is time to set the feature compatibility version to 6.0.

This will give us the opportunity to test our application with a full featured MongoDB 6.0 in a pre-production environment.

To set the feature compatibility version, connect to your replica set with the MongoDB Shell and issue an admin command. Pass in an object with a key of setFeatureCompatibilityVersion and a value that represents the desired version. Here we set it to 6.0.

If the command is successful, we will get this output.

Great! In this video, we covered the checklist of tasks to do before upgrading MongoDB versions.

Here’s a recap: First, confirm driver compatibility. Next, set the feature compatibility version, and then check the oplog window to determine how much time you have to complete maintenance on a single node.

Next, confirm that the replica set member status is healthy.

Finally, perform the upgrade.

Then we went through all of the steps involved in upgrading a MongoDB version on Ubuntu Linux.