Skip to content

Instantly share code, notes, and snippets.

@pratapaprasanna
Last active June 28, 2020 07:16
Show Gist options
  • Save pratapaprasanna/d7575669502b249a164eb36c2216cfa3 to your computer and use it in GitHub Desktop.
Save pratapaprasanna/d7575669502b249a164eb36c2216cfa3 to your computer and use it in GitHub Desktop.

Upgrade mongo from 3.x to 4.x

Introduction

If you have ever installed Mongo 3.* version and worrying as to how to upgrade it to Mongo 4.x. Then this story is for you, Mongo has made the upgrades comparatively easy. This story is a brief overview as to how to upgrade Mongo and it is divided into three segments

Identification

While Migrating Databases from one node to another using

  • mongodump --host sourcehost:27017 --db client_c_n --archive | mongorestore --host targethost:27017 --archive
  • We landed into Unrecognized field 'snapshot'. mongo this error.

Debugging

We saw that there is a version mismatch as in source host being 4.2.x and target being 3.6.17 between both the mongo servers. Now we have to take an immediate step to upgrade mongo and maintain the same version of mongo across the nodes.

Solution

Please note that you can update mongo DB only on Stepwise. For instance, To upgrade from a version earlier than the 3.6-series, you must successively upgrade major releases until you have upgraded to 3.6-series. For example, if you are running a 3.4-series, you must upgrade first to 3.6 before you can upgrade to 4.0.

This has three steps to do

  • Get the FeatureCompatibilityVersion
  • Set the FeatureCompatibilityVersion
  • Upgrade to the next stable version

How to get the FeatureCompatibilityVersion

  • Enter mongo shell and then type
    • db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ) if the parameter is set you should be seeing a result similar to this
    • "featureCompatibilityVersion" : { "version" : "3.6" }
    • If not then go ahead and set the featureCompatibilityVersion as follows db.adminCommand( { setFeatureCompatibilityVersion: "<current_version>" } )
    • Exit the shell
  • Once the parameter is set we are all set to upgrade our DB
    • stop mongo using systemctl stop mongod.service
    • Download the tarball from mongodb
    • untar and copy the binaries in bin directory to the place where existing binaries are present typically /usr/local/bin or /bin
    • Incase you want to check you can try which mongodump and it should point to the location where existing binaries are present.
  • Now start the mongo
    • systemctl restart mongod.service
    • Incase of any errors please check the log /var/log/mongodb/mongod.log
    • If you find nothing in the log then check journalctl -xe and also check if the permission are correctly given

Resources

Moral of the Excercise.

Keep the version consistent

  • Always check versions on fellow nodes before deploying anything . If the change is a minor version change its ok but if its major change please try to stick to only one version :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment