I was trying to update MongoDB from 3.6 to 4.4 since the Unifi network app supports it now, and it’ll get rid of those pesky GPG errors from APT.
Make sure you have a backup before starting, especially of /usr/lib/unifi/data/db.
Upgrade Steps
You will need to iterate though each major version of MongoDB, e.g. 3.6 -> 4.0 -> 4.2 -> 4.4.
-
Edit the APT repo
nano /etc/apt/sources.list.d/mongodb-org-3.6.listComment out the old one and add the new version, you will probably have to use
[allow-insecure=yes]since the certificate has expired and APT doesn’t like that.#deb [signed-by=/usr/share/keyrings/mongodb-org-server-3.6-archive-keyring.gpg] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/3.6 multiverse deb [allow-insecure=yes] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverseRemember you will need to iterate though every version, so you will need to change the
4.0to4.2, then4.4, after each upgrade. -
Update APT
apt update apt upgrade # You will need this for the next step too: apt install mongodb-org-shell -
Start
mongodwith the Unifi databasescreen mongod --dbpath /usr/lib/unifi/data/dbThen detach the screen session so it runs in the background:
Ctrl+A, thenD -
Upgrade the Mongo database
Open a mongo shell, (using
mongo shell) and follow the official upgrade guide.For example, I did this for version 4.0:
db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ) db.adminCommand( { setFeatureCompatibilityVersion: "4.0" } ) db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ) -
Stop
mongodfrom earlierscreen -rThen type
Ctrl+C, to stop it. -
Restart from the beginning for the next version, until you get to your desired version.
Final Steps
-
Fix up the APT repo for version 4.4
This will get rid of those GPG/verification errors.
wget -qO- https://www.mongodb.org/static/pgp/server-4.4.asc \ | gpg --dearmor -o /etc/apt/trusted.gpg.d/mongodb-org-server-4.4-archive-keyring.gpg echo "deb [arch=amd64,arm64 signed-by=/etc/apt/trusted.gpg.d/mongodb-org-server-4.4-archive-keyring.gpg] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" \ | tee /etc/apt/sources.list.d/mongodb-org-4.4.list rm /etc/apt/sources.list.d/mongodb-org-3.6.list -
Fix permissions (optional)
If Unifi isn’t starting, and you are getting permission denied errors in the mongo logs (
tail -f /var/log/unifi/mongod.log), you may need tochownback to the unifi user.For example:
chown -R unifi:unifi /usr/lib/unifi/data/db
Newer Versions
This guide should still work for newer versions of MongoDB, although there are a few exceptions:
- Versions 5.0 and newer no longer have major sub-versions, so you can skip some iteration steps e.g. only 4.4 -> 5.0 -> 6.0 -> 7.0 is required.
- Versions 7.0 and newer have added an additional
confirmfield to the set version command, so you will need to rundb.adminCommand( { setFeatureCompatibilityVersion: "7.0", confirm: true } )instead.
Thanks for reading!
Steve.
Comments
ReplyRobert Wagnon Feb 14, 2024
I had trouble running screen with sudo - maybe because of sudo and because there was a running instance. I connected the mongo shell to the existing instance using "mongo --port 27117 shell".
ReplyThanks for the great article.