Reprinted with attribution:
Data synchronization solutions
A replica set node becomes "stale" when the replication process of the replica set node is so far behind that the master node overwrites the oplog entries that have not yet been replicated by that node. When a node cannot keep up, it becomes "stale". When this happens, the data on the replica set node must be deleted, and then an initial synchronization must be performed to completely resynchronize the node.
MongoDB provides two options for performing an initial synchronization:
-
First: Reboot with an empty data directory
mongod
Then let MongoDB's normal initial synchronization function restore the data. This is the simpler option, but may take longer to replace the data. -
Second: Reboot the computer using the most recent copy of the data directory from another member of the replica set. This process replaces the data faster, but requires more manual steps.
Logical Initial Synchronization Process
When performing a logical initial synchronization, MongoDB will:
-
Clone all databases except the local database. To perform a clone, the
mongod
will scan each collection in each source database and then insert all the data into its own copy of those collections. -
Constructs all the indexes for each collection as documents are copied for that collection.
-
Pull newly added oplog records during data replication. Ensure that the target member is in the
local
There is sufficient disk space in the database to temporarily store these oplog records during this data replication phase. -
Apply all changes to the dataset.
mongod
Using the oplog from the source, update its dataset to reflect the current state of the replica set.
After completing the initial synchronization, the node is removed from theSTARTUP2
switch toSECONDARY
。
command to view node data synchronization
Through the () command to view the data synchronization status of the mongo cluster, which mainly observe the value of the optimeDate field can be basically consistent, as well as you can view the stateStr to see if the state is normal.
The following is an example of a service view with an abnormal synchronization status:
cure
Above figure in mongo2:27017 in the data state synchronization exception, when the state synchronization exception, you can first try to restart mongo, let the exception node to automatically synchronize the other nodes of the data.
If this does not solve the problem, you can delete the database directory file of the current node and restart the whole mongo cluster after deleting it, so that the data will be automatically synchronized at startup.
Reference Links
Resynchronizing members of a self-managed replica set:/zh-cn/docs/manual/tutorial/resync-replica-set-member/#std-label-replica-set-resync-by-copying