Mongo Upgrade On Ubuntu

An upgrade is a living activity as every software tool will have one or the other improvements throughout the lifetime.

Pre – Process/Steps:

Take the Database backup.

Stop the Mongodb server. 

sudo service mongod stop  

Check if services are stopped using  ps -ef|grep mongod . If it returns any of the active/available services running on mongod with PID numbers. 

Kill the mongod services using the command suffixing with the actual PID number in the below command

Kill -9 

If you are on mongo versions less than the currently active version, don’t just upgrade to the latest version straight It is recommended to upgrade a version at a time (example: If we are running the mongo v 3.2 and planned to upgrade to v 4.x, then first you have to upgrade to v 3.6 and then to the next immediate available version and then to v 4.x.

The command to uninstall the mongo was 

sudo service mongod stop

sudo apt-get purge mongodb-org*

sudo rm -r /var/log/mongodb

sudo rm -r /var/lib/mongodb

Mostly you can skip the below command as we are upgrading the mongo and not a fresh install. If you are doing a fresh install, then run the below command.

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

For Ubuntu 18.04

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.x multiverse" | sudo tee /etc/apt/sources.list.d/mongodb.list

In place of 4.x you should provide the upgrade version you are targetting.

For Ubuntu 16.04

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb.list

Updates the system repository and initiates the mongod install, it will make sure to install all the dependent packages required for mongodb

sudo apt update

sudo apt install mongodb-org=4.0.9 mongodb-org-server=4.0.9 mongodb-org-shell=4.0.9 mongodb-org-mongos=4.0.9 mongodb-org-tools=4.0.9

Enable the mongod service file with new requirements, which in turn can be used to start or stop the mongod

sudo systemctl enable mongod

sudo systemctl start mongod

To check the mongo successful installation, type mongod on the terminal it should show the mongo version or mongod command doesn’t exist. If you see the later error then you have messed up with the mondo installation. Else if you see permission errors, then you have to make sure you have the give the appropriate permissions to the mongo data directory and log directory.

If you are using node js make sure to update the mongoose to a compatible version. 

Some of the deprecated methods in mongo when I wrote this article, 

  • The count() function was deprecated you can use countdocuments() instead. 
  • If you are using any schema which was declared with the array of objects type. make sure to add { usePushEach: true } at the end of the schema definition. Otherwise, you will get PushALLerror  Eg: mongoose.Schema({},{usePushEach:true})

Note: You can specify the directory and other options on the config file. The config file for a mongo in ubuntu will be located at  /etc/mongod.config. The content in the config file looks like.

To view more options use mongod –help command, it returns all the available options.

Leave A Comment

Your email address will not be published. Required fields are marked *