October 4, 2018

Upgrading MongoDB from 3.6 to 4.0

How to upgrade MongoDB, on Ubuntu

Upgrading MongoDB from 3.6 to 4.0

Upgrading from MongoDB 3.6 to 4.0 seems to break the file storage. You need to do the upgrade manually in order for it to work. These are my notes on how I did it on Ubuntu 18.04 Bionic.

Downgrade Mongo from 4.0 to 3.6

If you're already running 4.0, you need to reinstall Mongo 3.6.

In /etc/apt/sources.list.d/, ensure you have the file /etc/apt/sources.list.d/mongodb-org-3.6.list, containing the line

deb [arch=amd64,arm64] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse

Yes, that is for Xenial: that's the latest version of Ubuntu with version 3.6 in the packages.

Then update apt and downgrade mongodb:

$ sudo systemctl stop mongod.service
$ sudo aptitude update
$ sudo apt-get install mongodb-org=3.6.8 mongodb-org-mongos=3.6.8 mongodb-org-server=3.6.8 mongodb-org-shell=3.6.8 mongodb-org-tools=3.6.8
$ sudo systemctl start mongod.service

Prepare for the upgrade

With Mongo 3.6 installed, now follow the official upgrade instructions.

Start a Mongo shell

$ mongo

In the shell, prepare for the upgrade:

> db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )

Check it worked:

> db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )

and check that "featureCompatibilityVersion" : { "version" : "3.6" } is in the output.


Ensure you have the file /etc/apt/sources.list.d/mongodb-org-4.0.list containing the line

deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse

A simple sudo aptitude update && aptitude full-upgrade will then install Mongo 4.0.

Check it's working with $ sudo systemctl status mongod.service and make sure you can connect to the Mongo shell with $ mongo.

Convert the storage engine

While we're here, let's upgrade the storage engine from the now-deprecated mmapv1 to the supported wiredTiger.

The official instructions are very clear.

  • Create a directory to hold the backup:
$ sudo mkdir /opt/mongo
$ sudo chmod a+rwx /opt/mongo
  • Back up the existing database:
$ mongodump --out /opt/mongo/
  • Stop the Mongo server.
$ sudo systemctl stop mongod.service
  • Remove the existing data storage
$ sudo rm -rf /var/lib/mongodb
  • Enable the wiredTiger storage engine by changing the storage stanza in /etc/mongo.conf by changing the engine line to read:`
# Where and how to store data.
  dbPath: /var/lib/mongodb
    enabled: true
  engine: wiredTiger
#  mmapv1:
#  wiredTiger:
  • Restart Mongo
$ sudo systemctl start mongod.service
  • Reload all your data
$ mongorestore /opt/mongo/

Everything should be working!