Update MongoDB 4.4 to 7.0 on UniFi servers

Follow these steps to upgrade MongoDB 4.4 to 7.0 on your UniFi server.

Update MongoDB 4.4 to 7.0 on UniFi servers

Starting with UniFi version 8.1.113, MongoDB 7.0 is supported. That's not too early, because MongoDB 4.4 became End of Life in February 2024. Going directly from 4.4 to 7.0 is unfortunately not possible. An intermediate upgrade to 5.0 and 6.0 is necessary.

Are you still on MongoDB 3.6? Then upgrade your installation first to 4.4 with the steps explained in this article.

It is required that your database is running the WiredTiger storage engine. Changing from Standalone to WiredTiger is not mentioned in the following steps. Please refer to the official documentation to do this migration first. Not sure which storage engine you use? Check it with mongo --port 27117 --eval 'db.serverStatus().storageEngine'

This Bash script simplifies the process of updating MongoDB from version 4.4 to version 6.0. The update from 6.0 to 7.0 is done via normal APT.

Please note that we have only tested this script on Debian 11, so use caution when attempting to use it on other Linux distributions. Remember to back up your system before proceeding with the upgrade.

#!/bin/bash

# Upgrade MongoDB from 4.4 to 6.0
# Author: Nexxwave https://www.nexxwave.be
# MongoDB releases archive: https://www.mongodb.com/download-center/community/releases/archive
# MongoDB versions: https://www.mongodb.com/docs/manual/release-notes/

###
# Stop UniFi
###

echo "## Stopping UniFi"
systemctl stop unifi

###
# Download MongoDB Shell
# 'mongo' is deprecated since Mongo 6.0, so we need a separate pacakge.
###

echo "## Download MongoDB Shell"
wget https://downloads.mongodb.com/compass/mongosh-2.2.5-linux-x64.tgz -P /tmp/
tar xvzf /tmp/mongosh-2.2.5-linux-x64.tgz -C /tmp/

###
# Upgrade to MongoDB 5.0.26
###

echo "## Downloading and extracting MongoDB 5.0.26"
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-debian11-5.0.26.tgz -P /tmp/
tar xvzf /tmp/mongodb-linux-x86_64-debian11-5.0.26.tgz -C /tmp/

echo "## Starting MongoDB 5.0.26 in background"
sudo -u unifi /tmp/mongodb-linux-x86_64-debian11-5.0.26/bin/./mongod --port 27117 --dbpath /var/lib/unifi/db &
sleep 60

echo "## Executing feature compatibility version 5.0"
/tmp/mongosh-2.2.5-linux-x64/bin/./mongosh --port 27117 --eval 'db.adminCommand( { setFeatureCompatibilityVersion: "5.0" } ) '

echo "## Shutting down MongoDB 5.0.26"
/tmp/mongosh-2.2.5-linux-x64/bin/./mongosh --port 27117 --eval 'db.getSiblingDB("admin").shutdownServer({ "timeoutSecs": 60 })'
sleep 10

###
# Upgrade to MongoDB 6.0.15
###

echo "## Downloading and extracting MongoDB 6.0.15"
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-debian11-6.0.15.tgz -P /tmp/
tar xvzf /tmp/mongodb-linux-x86_64-debian11-6.0.15.tgz -C /tmp/

echo "## Starting MongoDB 6.0.15 in background"
sudo -u unifi /tmp/mongodb-linux-x86_64-debian11-6.0.15/bin/./mongod --port 27117 --dbpath /var/lib/unifi/db &
sleep 60

echo "## Executing feature compatibility version 6.0"
/tmp/mongosh-2.2.5-linux-x64/bin/./mongosh --port 27117 --eval 'db.adminCommand( { setFeatureCompatibilityVersion: "6.0" } )'

echo "## Shutting down MongoDB 6.0.15"
/tmp/mongosh-2.2.5-linux-x64/bin/./mongosh --port 27117 --eval 'db.getSiblingDB("admin").shutdownServer({ "timeoutSecs": 60 })'
sleep 10

echo "## All done"

Next, import the MongoDB 7.0 GPG key.

curl -o /etc/apt/trusted.gpg.d/mongodb-org-70.gpg https://pgp.mongodb.com/server-7.0.pub

Add the MongoDB 7.0 sources to your sources.list file or another file in /etc/apt/sources.list.d/. Remove any existing older MongoDB repos.

deb [arch=amd64] https://repo.mongodb.org/apt/debian/ bullseye/mongodb-org/7.0 main

Install the latest versions of the MongoDB packages:

sudo apt update
sudo apt install mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools mongodb-org-database-tools-extra

Start UniFi again: sudo systemctl start unifi.

Finally, finish the update with:

mongosh --port 27117 --eval 'db.adminCommand( { setFeatureCompatibilityVersion: "7.0", confirm: true } )'

Check if everything went well:

mongosh --port 27117 --eval 'db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )'

The output should contain:

{ featureCompatibilityVersion: { version: '7.0' }, ok: 1 }

Looking for support or UniFi hosting?

Are you a business or an MSP looking for hosted UniFi services? Have a look on our (Dutch) website, or contact us directly at hello@nexxwave.eu.