您好,登錄后才能下訂單哦!
背景:
我們一般配置的Mongodb主從,或者M(jìn)ongodb復(fù)制集,數(shù)據(jù)同步都是實時的。但如果在主節(jié)點上進(jìn)行了錯誤的數(shù)據(jù)操作,這時候就會導(dǎo)致整個集群的數(shù)據(jù)都出錯。因此,我們可以在一個集群中,挑選一個mongodb實例,用作復(fù)制延遲。當(dāng)在主節(jié)點上誤操作的時候,集群中有一個實例是不受影響的。這時候就可以利用這臺不受影響的實例進(jìn)行數(shù)據(jù)恢復(fù)。
以上就是mongodb的延遲復(fù)制節(jié)點的功能,當(dāng)主節(jié)點進(jìn)行一次數(shù)據(jù)操作后,延遲復(fù)制節(jié)不立馬進(jìn)行數(shù)據(jù)同步操作,而是在一段時間后,才同步數(shù)據(jù)。
配置:
以我的實驗環(huán)境為例,以下為我的mongodb復(fù)制集:
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 }
這時還未配置延遲復(fù)制節(jié)點,所以數(shù)據(jù)是實時同步的:
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節(jié)點都在同一時間實時同步了數(shù)據(jù)。
配置192.168.52.135:27017為延遲復(fù)制節(jié)點:
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節(jié)點出現(xiàn)了"slaveDelay":30的值,說明該節(jié)點的同步時間向后推遲了30秒。
具體大家可以測試一下,延遲復(fù)制時間大概會在30秒左右。有一點要注意,mongodb的系統(tǒng)時間必須一致,否則會造成延遲復(fù)制異常,導(dǎo)致在規(guī)定同步時間到了之后不進(jìn)行同步操作。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。