您好,登錄后才能下訂單哦!
背景:
我們一般配置的Mongodb主從,或者Mongodb復制集,數據同步都是實時的。但如果在主節點上進行了錯誤的數據操作,這時候就會導致整個集群的數據都出錯。因此,我們可以在一個集群中,挑選一個mongodb實例,用作復制延遲。當在主節點上誤操作的時候,集群中有一個實例是不受影響的。這時候就可以利用這臺不受影響的實例進行數據恢復。
以上就是mongodb的延遲復制節點的功能,當主節點進行一次數據操作后,延遲復制節不立馬進行數據同步操作,而是在一段時間后,才同步數據。
配置:
以我的實驗環境為例,以下為我的mongodb復制集:
cmh0:PRIMARY> rs.status() { "set" : "cmh0", "date" : ISODate("2016-08-22T02:43:16.240Z"), "myState" : 1, "members" : [ { "_id" : 1, "name" : "192.168.52.128:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 82, "optime" : Timestamp(1470581983, 1), "optimeDate" : ISODate("2016-08-07T14:59:43Z"), "electionTime" : Timestamp(1471833721, 1), "electionDate" : ISODate("2016-08-22T02:42:01Z"), "configVersion" : 1, "self" : true }, { "_id" : 2, "name" : "192.168.52.135:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 71, "optime" : Timestamp(1470581983, 1), "optimeDate" : ISODate("2016-08-07T14:59:43Z"), "lastHeartbeat" : ISODate("2016-08-22T02:43:15.138Z"), "lastHeartbeatRecv" : ISODate("2016-08-22T02:43:14.978Z"), "pingMs" : 0, "lastHeartbeatMessage" : "could not find member to sync from", "configVersion" : 1 }, { "_id" : 3, "name" : "192.168.52.135:27019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 75, "optime" : Timestamp(1470581983, 1), "optimeDate" : ISODate("2016-08-07T14:59:43Z"), "lastHeartbeat" : ISODate("2016-08-22T02:43:15.138Z"), "lastHeartbeatRecv" : ISODate("2016-08-22T02:43:15.138Z"), "pingMs" : 0, "configVersion" : 1 } ], "ok" : 1 }
這時還未配置延遲復制節點,所以數據是實時同步的:
cmh0:PRIMARY> use cmhtest switched to db cmhtest cmh0:PRIMARY> db.cmh.insert({"name":"ChenMinghui"}) WriteResult({ "nInserted" : 1 }) cmh0:PRIMARY> rs.printReplicationInfo() configured oplog size: 990MB log length start to end: 195secs (0.05hrs) oplog first event time: Mon Aug 22 2016 10:51:22 GMT+0800 (CST) oplog last event time: Mon Aug 22 2016 10:54:37 GMT+0800 (CST) now: Mon Aug 22 2016 10:55:00 GMT+0800 (CST) cmh0:PRIMARY> rs.printSlaveReplicationInfo() source: 192.168.52.135:27017 syncedTo: Mon Aug 22 2016 10:54:37 GMT+0800 (CST) 0 secs (0 hrs) behind the primary source: 192.168.52.135:27019 syncedTo: Mon Aug 22 2016 10:54:37 GMT+0800 (CST) 0 secs (0 hrs) behind the primary
可以看到兩個Secondary節點都在同一時間實時同步了數據。
配置192.168.52.135:27017為延遲復制節點:
cmh0:PRIMARY> cfg=rs.conf(); { "_id" : "cmh0", "version" : 1, "members" : [ { "_id" : 1, "host" : "192.168.52.128:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 2, "host" : "192.168.52.135:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 3, "host" : "192.168.52.135:27019", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatTimeoutSecs" : 10, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 } } } cmh0:PRIMARY> cfg.members[1].priority=0 0 cmh0:PRIMARY> cfg.members[1].slaveDelay=30 30 cmh0:PRIMARY> rs.reconfig(cfg); { "ok" : 1 } cmh0:PRIMARY> rs.conf() { "_id" : "cmh0", "version" : 2, "members" : [ { "_id" : 1, "host" : "192.168.52.128:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 2, "host" : "192.168.52.135:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 0, "tags" : { }, "slaveDelay" : 30, "votes" : 1 }, { "_id" : 3, "host" : "192.168.52.135:27019", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatTimeoutSecs" : 10, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 } } }
可以看到192.168.52.135:27017節點出現了"slaveDelay":30的值,說明該節點的同步時間向后推遲了30秒。
具體大家可以測試一下,延遲復制時間大概會在30秒左右。有一點要注意,mongodb的系統時間必須一致,否則會造成延遲復制異常,導致在規定同步時間到了之后不進行同步操作。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。