MongoDB Database Upgrades

I've been doing a few upgrades of old standalone (not replica set) mongodb databases lately, taking them from 2.6, 3.0, or 3.2 up to 3.6. Here's my upgrade process on RHEL/CentOS, which has been working pretty smoothly (cf. the mongodb notes here: https://docs.mongodb.com/manual/tutorial/change-standalone-wiredtiger/).

First, the WiredTiger storage engine (the default since mongodb 3.2) "strongly" recommends using the xfs filesystem on linux, rather than ext4 (see https://docs.mongodb.com/manual/administration/production-notes/#prod-notes-linux-file-system for details). So the first thing to do is reorganise your disk to make sure you have an xfs filesystem available to hold your upgraded database. If you have the disk space, this may be reasonably straightforward; if you don't, it's a serious PITA.

Once your filesystems are sorted, here's the upgrade procedure.

1. Take a full mongodump of all your databases

cd /data    # Any path with plenty of disk available
for $DB in db1 db2 db3; do
mongodump -d $DB -o mongodump-$DB-$(date +%Y%m%d)
done

2. Shut the current mongod down

systemctl stop mongod
# Save the current mongodb.conf for reference
mv /etc/mongodb.conf /etc/mongod.conf.rpmsave

3. Hide the current /var/lib/mongo directory to avoid getting confused later. Create your new mongo directory on the xfs filesystem you've prepared e.g. /opt.

cd /var/lib
mv mongo mongo-old
# Create new mongo directory on your (new?) xfs filesytem
mkdir /opt/mongo
chown mongod:mongod /opt/mongo

4. Upgrade to mongo v3.6

vi /etc/yum.repos.d/mongodb.repo

# Add the following section, and disable any other mongodb repos you might have
[mongodb-org-3.6]
name=MongoDB 3.6 Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc

# Then do a yum update on the mongodb packages
yum update mongodb-org-{server,tools,shell}

5. Check/modify the new mongod.conf. See https://docs.mongodb.com/v3.6/reference/configuration-options/ for all the details on the 3.6 config file options. In particular, dbPath should point to the new xfs-based mongo directory you created in (3) above.

vi /etc/mongod.conf

# Your 'storage' settings should look something like this:
storage:
  dbPath: /opt/mongo
  journal:
    enabled: true
  engine: "wiredTiger"
  wiredTiger:
    collectionConfig:
      blockCompressor: snappy
    indexConfig:
      prefixCompression: true

6. Restart mongod

systemctl daemon-reload
systemctl enable mongod
systemctl start mongod
systemctl status mongod

7. If all looks good, reload the mongodump data from (1):

cd /data
for $DB in db1 db2 db3; do
mongorestore --drop mongodump-$DB-$(date +%Y%m%d)
done

All done!

These are the basics anyway. This doesn't cover configuring access control on your new database, or wrangling SELinux permissions on your database directory, but if you're doing those currently you should be able to figure those out.