MongoDB 4.0 Upgrade Guide
Author: Hanan Alsahsan | 8 min read | October 8, 2018
Why upgrade to MongoDB 4.0? There are several reasons to keep upgrading MongoDB. Newest version offers multiple new features, fixes previous version bugs/ issues, and improves security.
MongoDB 4.0 has been available for download since June 27, 2018. The new release offers new features designed to help you work with data, helping you place it anywhere you need it. Feature highlights include:
- Multi-document ACID transactions
- Nonblocking secondary replica reads
- Data type conversions
- SCRAM-SHA-256 authentication
- Up to 40% faster shared migrations
- Take an up-to-date backup of your data set using one of the MongoDB Backup Methods that meets your needs.
- Plan the upgrade during a predefined maintenance window.
- Check MongoDB documentation for compatibility issues specific to your release.
- Test your upgrade procedures in the staging environment to ensure compatibility.
Before Upgrading
Prerequisites
- If you’re using an older version of MongoDB, you must successively upgrade to MongoDB 3.6 before moving to 4.0.
- If your deployment has user credentials stored in MONGODB-CR schema, upgrade to Salted Challenge Response Authentication Mechanism (SCRAM) before moving to MongoDB 4.0.
- Starting in version 4.0, MongoDB removes support for the $isolated operator. Before upgrading you need to recreate the index or views without $isolated operator.
- The 3.6 instance must have featureCompatibilityVersion set to 3.6.
Prerequisites to Replica Set and Shredded Cluster
- All replica set members must be using version 3.6.
- You must upgrade replica set protocol version 0 to pv1 as protocol version 0 pv0 is deprecated in MongoDB 4.0.
- If your deployment uses master-slave replication, you must upgrade to a replica set as master-slave replication is deprecated in MongoDB 4.0.
- No replica set member should be in ROLLBACK or RECOVERING state.
Upgrade Procedure
Download 4.0 Binaries
- Package Manager – The preferred method for upgrading MongoDB to 4.0.
- Manually
You should upgrade to 4.0 using package manager, if you installed MongoDB from the MongoDB apt, yum, dnf, or zypper repositories.
Use MongoDB Download Center to download binaries, if you didn’t use MongoDB repositories.
Upgrade Process for standalone MongoDB
When you upgrade a standalone MongoDB, the MongoDB server is down during the upgrade process.
- Shut down the MongoDB instance and replace the 3.6 binary with the 4.0 binary followed by restart the member.
- Enable backwards-incompatible 4.0 features by executing db.adminCommand( { setFeatureCompatibilityVersion: “4.0” } )
Upgrade Process for Replica Set MongoDB
To upgrade a replica set from MongoDB 3.6 to 4.0 start by upgrading secondary members one at a time and finishing with the primary.
- Shut down the MongoDB instance and replace the 3.6 binary with the 4.0 binary followed by a member restart.
- Wait for the member to update to SECONDARY state before upgrading the next secondary member.
Upgrade the Primary
- Execute rs.stepDown() command on the current primary to force an election of a new primary.
- Once the primary has stepped down and another member has become the new primary start upgrading the original primary.
- Shut down the original primary instance and replace the 3.6 binary with the 4.0 binary followed by the member restart.
- Enable backwards-incompatible 4.0 features by executing db.adminCommand( { setFeatureCompatibilityVersion: “4.0” } )
Upgrade Process for Shared Cluster
- Stop balancer
- Upgrade the Config Servers
- Upgrade Shards One by One
- Upgrade the MongoDB instances
- Re-enable the balancer
- Enable backwards-incompatible 4.0 features
- Restart MongoDB instances
Connect to Mongo instance and use command sh.stopBalancer() to stop the balancer in the shared cluster.
To upgrade config servers, start with secondaries member one at a time:
a) Stop the secondary MongoDB instance and replace the 3.6 binary with the 4.0 binary.
b) Start the 4.0 binary with the –configsvr, –replSet, and –port.
c) Wait for the member to recover to SECONDARY state before upgrading the next secondary member.
Upgrade primary
a) Execute rs.stepDown() command on the current primary to force an election of a new primary.
b) Once the primary has stepped down and another member has become the new primary start upgrading the original primary.
c) Start the 4.0 binary with the –configsvr, –replSet, –port, and –bind_ip options.
For each shard replica set:
Upgrade the secondary members of the replica set one after the other:
a) Shut down the secondary MongoDB instance and replace the 3.6 binary with the 4.0 binary.
b) Start the 4.0 binary with the –shardsvr, –replSet, and –port. Include any other options as used by the deployment.
c) Wait for the member to recover to SECONDARY state before upgrading the next secondary member.
Upgrade primary
a) Execute rs.stepDown() command on the current primary to force an election of a new primary.
b) Once the primary has stepped down and another member has assumed PRIMARY state another server becomes the new primary start upgrading the original primary.
c) Start the 4.0 binary with the –shardsvr, –replSet, –port, and –bind_ip options.
Replace each MongoDB instance running 3.6 binary with the 4.0 binary and restart.
Connect to the MongoDB instance and use command sh.setBalancerState() to re-enable the balancer.
On a MongoDB instance in the admin database, run command db.adminCommand( { setFeatureCompatibilityVersion: “4.0” } ).
Restart all MongoDB instances to pick up the changes in the fundamental consistency behavior.
Now that you know all the steps involved, you can easily upgrade your environment to MongoDB 4.0. Datavail can also help you make the move. Learn about Datavail’s database upgrade services now.