Update MongoDB 3.6 to 4.4 on UniFi servers

Use these steps to upgrade an older MongoDB database on your UniFi server.

Update MongoDB 3.6 to 4.4 on UniFi servers

UniFi's release of UniFi Network Application version 7.5.187 introduced support for MongoDB up to version 4.4. However, MongoDB updates require an incremental approach, meaning you need to upgrade to each intermediate version before reaching the latest one.

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 3.6 to version 4.2.23. The update from 4.2 to 4.4 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 3.6 to 4.2.23.
# Author: Nexxwave https://www.nexxwave.be
# Last edit: 17-10-2023
# MongoDB releases archive: https://www.mongodb.com/download-center/community/releases/archive
# MongoDB versions: https://www.mongodb.com/docs/manual/release-notes/

###
# Upgrade to MongoDB 4.0.28
###

echo "## Stopping UniFi"
systemctl stop unifi

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

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

echo "## Executing feature compatibility version 4.0"
/tmp/mongodb-linux-x86_64-4.0.28/bin/./mongo --port 27117 --eval 'db.adminCommand( { setFeatureCompatibilityVersion: "4.0" } ) '

echo "## Shutting down MongoDB 4.0.28"
pkill -P $PROCMONGO40
sleep 10

###
# Upgrade to MongoDB 4.2.23
###

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

echo "## Starting MongoDB 4.2.23 in background"
sudo -u unifi /tmp/mongodb-linux-x86_64-debian10-4.2.23/bin/./mongod --port 27117 --dbpath /var/lib/unifi/db &
PROCMONGO42=$!
sleep 60

echo "## Executing feature compatibility version 4.2"
/tmp/mongodb-linux-x86_64-debian10-4.2.23/bin/./mongo --port 27117 --eval 'db.adminCommand( { setFeatureCompatibilityVersion: "4.2" } )'

echo "## Shutting down MongoDB 4.2.23"
pkill -P $PROCMONGO42
sleep 10

echo "## All done"

Next, import the MongoDB 4.4 GPG key from https://www.mongodb.org/static/pgp/server-4.4.pub and save it in /etc/apt/trusted.gpg.d/.

Add the MongoDB 4.4 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/ buster/mongodb-org/4.4 main

Install the latest versions:

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:

mongo --port 27117 --eval 'db.adminCommand( { setFeatureCompatibilityVersion: "4.4" } )'

Check if everything went well:

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

The output should contain:

MongoDB server version: 4.4.25
{ "featureCompatibilityVersion" : { "version" : "4.4" }, "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.