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 netbrain 42 Oct 19 15:03 data
drwxr-xr-x. 4 root netbrain 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 /usr/lib/mongodb/data command under the /usr/lib/mongodb directory to move the data directory to the /usr/lib/mongodb directory.
5)Run the chown -R netbrain:netbrain /usr/lib/mongodb/data/ command to change the ownership for the moved data folder.
6)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 netbrain netbrain 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
4.Run the systemctl start mongod command to restart the MongoDB service.
5.Run the following command to connect to the node:
•NetBrain IE 7.1x: mongo --host <IP or hostname of MongoDB Server:Port> -u <username> -p <password> --authenticationDatabase <database_name>
Example:
[root@localhost upgrade_replica_set]# mongo --host 10.10.3.142:27017 -u mongodb -p mongodb --authenticationDatabase admin
MongoDB shell version v3.6.4
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: v3.6.4
...
rsnetbrain:PRIMARY>
•NetBrain IE 8.x: mongo --host <IP or hostname of MongoDB Server:Port> -u <username> -p <password> --authenticationDatabase <database_name> --authenticationMechanism SCRAM-SHA-256
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: v4.0.6
...
rsnetbrain:PRIMARY>
Tip: If SSL is enabled, run the following command:
- NetBrain IE 7.1x: mongo --host <IP or hostname of MongoDB Server:Port> -u
<username> -p <password> --authenticationDatabase <database_name> --ssl --
sslAllowInvalidCertificates
- NetBrain IE 8.x: mongo --host <IP or hostname of MongoDB Server:Port> -u
<username> -p <password> --authenticationDatabase <database_name> --ssl --
sslAllowInvalidCertificates --authenticationMechanism SCRAM-SHA-256 command.