how to install MongoDB replica set

Fix “RECOVERING” State Error in MongoDB Secondary Replica

Last updated on March 12th, 2023 at 12:06 am

Did your MongoDB secondary replica stuck on “RECOVERING” State Error?

In this blog post, we will explore the steps that can be taken to fix the RECOVERING issue in MongoDB secondary replicas.

Introduction

MongoDB is one of the most popular document-oriented NoSQL databases in use today, and it is widely used for storing, managing, and querying large volumes of data. One of the key features of MongoDB is its support for replication, which allows for multiple copies of the data to be maintained across different servers. This helps to improve the availability and reliability of the data, as well as its performance.

However, if you have worked with MongoDB replicas before, you might have encountered the RECOVERING issue in secondary replicas. Secondary replicas are replicas that are only used for replication and cannot be used for transactional queries. The RECOVERING issue occurs when a secondary replica is stuck in the recovering mode for an indefinite period. This can happen due to a variety of reasons, such as a sync difference of more than 24 hours between primary and secondary replicas or corrupted secondary data that does not know how to perform sync.

Here are the steps:

Step 1: Stop MongoDB Secondary Replica Service

The first step in fixing the RECOVERING issue is to stop the secondary replica service in which you are facing the problem. To do this, you can use the following command:

sudo service mongod stop

This command will stop the mongod service, which is the primary daemon process for MongoDB.

Step 2: Delete Data Files

Once you have stopped the secondary replica service, the next step is to delete the data files. This is necessary because the data files may be corrupted or out of sync, and deleting them will force MongoDB to resync the data from the primary replica. To delete the data files, you can use the following command:

sudo rm -rf /path/to/db/data/directory

This command will remove the data files from the specified directory. Be sure to replace “/path/to/db/data/directory” with the actual path to the data directory for your MongoDB instance.

Step 3: Start the mongod Service Again

After deleting the data files, you can start the mongod service again. This will initiate the resync process and bring your MongoDB secondary replica back to the SECONDARY state. To start the mongod service, use the following command:

sudo service mongod start

This command will start the mongod service and initiate the resync process. Depending on the size of your database, it may take anywhere from 30 to 60 minutes for the resync process to complete.

Conclusion

To summarize, a sync difference of more than 24 hours or corrupted secondary data can cause the RECOVERING issue in MongoDB secondary replicas. To fix it, stop the service, delete data files, and start the mongod service to resync data and bring the replica back to SECONDARY state. These steps ensure reliable, available, and performant MongoDB replication.

Checkout: How to Install MongoDB on Ubuntu

Leave a ReplyCancel reply

Exit mobile version