This guide aims to seamlessly update MongoDB Replicas from version 4.4 to version 6.0. It is not possible to migrate to MongoDB 6.0 directly from version 4.4. Therefore, a gradual migration between each major release is required.

#1) Updating Secondary Nodes from Mongo 4.4 to 5.0

First, we need to update each of the MongoDB secondary nodes one by one to version 5.0:

sudo apt-mark unhold mongo*
sudo systemctl stop mongod
# Add MongoDB 5.0 Key
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

# Add MongoDB 5.0 Repository:
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

# Install the Update:
sudo apt update
sudo apt install -y mongodb-org=5.0.6 mongodb-org-server=5.0.6 mongodb-org-shell=5.0.6 mongodb-org-mongos=5.0.6 mongodb-org-tools=5.0.6
BASH

Very Important !

When installing you are asked to rewrite the config file, if you accidentally answered yes, sudo vi /etc/mongod.conf must be rewritten!

storage:
  dbPath: /var/lib/mongodb
  wiredTiger:
    engineConfig:
       cacheSizeGB: 2

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

net:
  port: 25080
  bindIp: 0.0.0.0

replication:
  replSetName: apinizer-replicaset

security:
  authorization: enabled
  keyFile:  /etc/mongodb/keys/mongo-key

setParameter:
  transactionLifetimeLimitSeconds: 300

processManagement:
  timeZoneInfo: /usr/share/zoneinfo

# Start MongoDB Service
sudo apt-mark hold mongo*
sudo systemctl start mongod
sudo systemctl status mongod
BASH

Check the status of the replica set by connecting to any node:

mongo mongodb://<MONGO_NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p

# Checking the Status of the Replica-Set
rs.status()
BASH

#2) Connecting to Primary-Node and Demoting to Secondary.

We will determine a new primary node by connecting to the primary node and dropping the primary node.

# Connecting to Primary Node
mongo mongodb://<NODE-PRIMARY_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p  

# Reducing Primary Node to Secondary Node 
rs.stepDown()  
 
# We Make Sure That One Of The Secondary Nodes We Upgrade Is The Primary Node 
rs.status()
BASH


# Start MongoDB Service
sudo apt-mark hold mongo*
sudo systemctl start mongod
sudo systemctl status mongod
BASH

Check the status of the replica set by connecting to any node:

mongosh mongodb://<node_name_ip>:25080 --authenticationDatabase "admin" -u "apinizer" -p
rs.status()
BASH

(Optionally) If the old Primary-Node is to be made Primary Node again

Connect to the new Primary Node

cfg = rs.conf()

cfg.members[ESKİ_PRIMARY_NODE_ID].priority = 2

rs.reconfig(cfg)

rs.stepDown()

exit;

Connect to the Primary Node and Restore the Old Settings.

mongosh mongodb://<PRIMARY_NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p

rs.status()

cfg = rs.conf()

cfg.members[ESKİ_PRIMARY_NODE_ID].priority = 1

rs.reconfig(cfg)

#4) Mongo 5.0 Feature Compatibility Check

After all nodes have migrated to MongoDB 5.0, the feature compatibility setting is set to 5.0 (on the Primary Node) before migrating to Mongodb 6.

# Connect to Primary Node
mongosh mongodb://<PRIMARY_NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p
use admin  

# If the Feature Compatibility is 4 as the output of the following command, it should be updated to5.
db.runCommand({ getParameter: 1, featureCompatibilityVersion: 1 })  

# If the value is not 5.0, you can change it with the following command
db.adminCommand({ setFeatureCompatibilityVersion: "5.0" })
BASH

#5) Update Secondary Nodes from Mongo 5.0 to 6.0

Once we are sure that all MongoDB replicas have migrated to version 5.0, we can migrate to version 6.0. First, start by updating all secondary nodes in order:

sudo apt-mark unhold mongo*
sudo systemctl stop mongod

# Add key of the MongoDB 6.0 and Update to Repository
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list

# Install to update
sudo apt update
sudo apt install -y mongodb-org=6.0.0 mongodb-org-server=6.0.0 mongodb-org-shell=6.0.0 mongodb-org-mongos=6.0.0 mongodb-org-tools=6.0.0

# Start MongoDB Service
sudo apt-mark hold mongo*
sudo systemctl start mongod
sudo systemctl status mongod
BASH

Check the status of the replica set by connecting to any node:

mongosh mongodb://<MONGO_NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p

# Checking the Status of the Replica-Set
rs.status()
BASH

#6) Connecting to the last Primary-Node and Downgrade to Secondary.

# Connecting to Primary Node
mongosh mongodb://node-primary:25080 --authenticationDatabase "admin" -u "apinizer" -p

# Reducing Primary Node to Secondary Node
rs.stepDown()

# We Make Sure That One of The Secondary Nodes We Upgrade Is Primary
rs.status()
BASH

#7) Updating Downgraded Primary Node from Mongo 5 to 6.0

sudo apt-mark unhold mongo*
sudo systemctl stop mongod  

# Add key of the MongoDB 6.0 and Update to Repository
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list

# Install to update
sudo apt update
sudo apt install -y mongodb-org=6.0.0 mongodb-org-server=6.0.0 mongodb-org-shell=6.0.0 mongodb-org-mongos=6.0.0 mongodb-org-tools=6.0.0

# Start MongoDB Service
sudo apt-mark hold mongo*
sudo systemctl start mongod
sudo systemctl status mongod
BASH

Check the status of the replica set by connecting to any node:

mongosh mongodb://<MONGO_NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p

# Check the status of the Replica-Set
rs.status()
BASH


(Optionally) If you want to make the old primary node a primary node again

mongosh mongodb://<PRIMARY_NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p

use admin

cfg = rs.conf()

cfg.members[ESKİ_PRIMARY_NODE_ID].priority = 2

rs.reconfig(cfg)

rs.stepDown()

exit;

Connect to the Primary Node and Restore the Old Settings.

mongosh mongodb://<PRIMARY_NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p

rs.status()

cfg = rs.conf()

cfg.members[ESKİ_PRIMARY_NODE_ID].priority = 1

rs.reconfig(cfg)

#8) Mongo 6.0 Feature Compatibility Check

After all nodes are migrated to MongoDB 6.0, the feature compatibility setting is set to 6.0 (on the Primary Node).

# Connected to Primary Node.
mongosh mongodb://<PRIMARY_NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p
use admin

# If the Feature Compatibility is 5 as the output of the following command, it should be updated to6. db.runCommand({ getParameter: 1, featureCompatibilityVersion: 1 })

# If the value is not 6.0, you can change it with the following command  
db.adminCommand( { setFeatureCompatibilityVersion: "6.0" } )
BASH

Congratulations you have updated your MongoDB replicas from Mongo 4.X to 6.0 without downtime.