1: The principle of data synchronization:
When the primary node completes the data operation, secondary will make a series of actions to ensure the synchronization of the data: 1: Check your local library's oplog.rs collection to find the most recent timestamp. 2: Check the primary node local library Oplog.rs collection to find records larger than this timestamp. 3: Insert the found records into your own oplog.rs collection and perform these operations.
2: View the information for the replica set
Gechongrepl:primary> Rs.status () {"Set": "Gechongrepl", "date": Isodate ("2015-07-02t02:38:15z"), "MyState": 1, " Members ": [{" _id ": 6," name ":" 192.168.91.144:27017 "," Health ": 1," state ": 7," STATESTR ":" Arbiter "," uptime ": 1678," LA Stheartbeat ": Isodate (" 2015-07-02t02:38:14z ")," Lastheartbeatrecv ": Isodate (" 2015-07-02t02:38:14z ")," PingMs ": 1},{ "_id": Ten, "Name": "192.168.91.135:27017", "Health": 1, "state": 2, "statestr": "Secondary", "uptime": 1678, "Optime": Ti Mestamp (1435803750, 1), "Optimedate": Isodate ("2015-07-02t02:22:30z"), "Lastheartbeat": Isodate ("2015-07-02t02 : 38:14z ")," Lastheartbeatrecv ": Isodate (" 2015-07-02t02:38:13z ")," Pingms ": 1," syncingto ":" 192.168.91.148:27017 "},{ "_id": One, "name": "192.168.91.148:27017", "Health": 1, "state": 1, "Statestr": "PRIMARY", "uptime": 1698, "optime": Time Stamp (1435803750, 1), "Optimedate": Isodate ("2015-07-02t02:22:30z"), "Electiontime": Timestamp (1435803023, 1), " Electiondate ": Isodate (" 2015-07-02t02:10:23z ")," Self ": true},{" _iD ":", "name": "192.168.91.134:27017", "Health": 1, "state": 2, "statestr": "Secondary", "uptime": 1655, "optime": Times Tamp (1435803750, 1), "Optimedate": Isodate ("2015-07-02t02:22:30z"), "Lastheartbeat": Isodate ("2015-07-02t02:38:14z" ), "Lastheartbeatrecv": Isodate ("2015-07-02t02:38:14z"), "Pingms": 1, "syncingto": "192.168.91.135:27017"}], "OK": 1}
Mystate:1 indicates that PRIMARYSTATE:1 represents primary ; 7 represents arbiteruptime: Member's online time Lastheartbeat: current instance to the far end the last time the heartbeat packet was successfully received PINGMS: The return time of this instance to the remote routing packet Optime: Read Oplog.rs collection. The last time the instance was changed.
MongoDB uses lastheartbeat for automatic transfer.
The Mongod instance sends a heartbeat packet to the other members every two seconds, and the member's state is judged by the health of the member returned in Rs.status (). If the primary node is unavailable, then all secondary nodes in the replication set will trigger an election operation. Select the new primary node. If there are multiple secondary nodes, the node that has the latest Oplog timestamp record or a higher privilege will be elected primary (note: If the secondary stop time is too long, the primary node Oplog content is overwritten by a circular write, You need to manually synchronize the secondary node)
MongoDB Replica Set Configuration Series 11: MongoDB Data Synchronization principle and the principle of automatic failover