If you encounter data loss or corruption during the upgrade process, complete the following steps to restore MongoDB data with the backup data.

1.Log in to the Linux server where the MongoDB is installed as the root user.

2.Stop the MongoDB Service.

1)Run the systemctl stop mongod command to stop the MongoDB service.

2)Run the ps -ef|grep mongod command to verify whether the mongod process is stopped.

[root@localhost ~]# ps -ef| grep mongod
root     15136 14237  0 10:42 pts/2    00:00:00 grep --color=auto mongod

Note: If the mongod process is stopped, the result should only contain one entry as shown above.

3.Restore the old data onto the MongoDB.

1)Run the cd /usr/lib/mongodb command to navigate to the /usr/lib/mongodb directory.

Note: If you modified the following default directory to store all MongoDB data files during the MongoDB installation, you must use the new directory (available in the mongod.conf file) accordingly.

2)Run the ls -al command to browse all directories and files under the /usr/lib/mongodb directory.

[root@localhost mongodb]# ls -al
total 142
drwxr-xr-x. 5 netbrain netbrain   146 Oct 19 15:02 .              
drwxr-xr-x. 4 root     root        42 Sep 19 14:41 ..                  
drwxr-xr-x. 4 root     root        42 Oct 19 15:03 data  
drwxr-xr-x. 4 root     root       100 Oct 19 15:03 log
-rwxr-xr-x. 2 netbrain netbrain  1004 Aug 25 17:26 mongodb-keyfile
-rwxr-xr-x. 1 netbrain netbrain  1076 Oct 19 15:02 mongod.conf

3)Run the rm -rf ./data command to delete the data directory.

4)Run the mv /etc/mongodb_databk/data command under the /usr/lib/mongodb directory to move the data directory to the /opt/mongodb directory.

5)Run the ls -al command to browse all directories and files under the /usr/lib/mongodb directory.

[root@localhost mongodb]# ls -al
total 142
drwxr-xr-x. 5 netbrain netbrain   146 Oct 19 15:02 .              
drwxr-xr-x. 4 root     root        42 Sep 19 14:41 ..                   
drwxr-xr-x. 4 root     root     86016 Oct 19 15:03 data
drwxr-xr-x. 4 root     root       100 Oct 19 15:03 log
-rwxr-xr-x. 2 netbrain netbrain  1004 Aug 25 17:26 mongodb-keyfile
-rwxr-xr-x. 1 netbrain netbrain  1076 Oct 19 15:02 mongod.conf
-rwxr-xr-x. 1 netbrain netbrain  1147 Oct 19 14:51 mongod.conf2017|Oct|19|10:15:50

4.Run the systemctl mongod start command to restart the MongoDB service.

5.Run the mongo --host <IP or hostname of MongoDB Server:Port> -u <username> -p <password> --authenticationDatabase <database_name> --authenticationMechanism SCRAM-SHA-256 command to connect to the node.

    Example:

[root@localhost upgrade_replica_set]# mongo --host 10.10.3.142:27017 -u mongodb -p mongodb --authenticationDatabase admin --authenticationMechanism SCRAM-SHA-256
MongoDB shell version v4.0.6
connecting to: mongodb://10.10.3.142:27017/?authMechanism=SCRAM-SHA-256&authSource=admin&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("0315bda2-73f3-4304-9166-c008b9b06ce3") }
MongoDB server version: 4.0.6
...
rsnetbrain:PRIMARY>

Tip: If SSL is enabled, run the command mongo --host <IP or hostname of MongoDB Server:Port> -u
 <username> -p <password> --authenticationDatabase <database_name> --ssl --
sslAllowInvalidCertificates --authenticationMechanism SCRAM-SHA-256 command.