MongoDB複本集配置系列十:MongoDB local庫詳解和資料同步原理

來源:互聯網
上載者:User

標籤:

1:local庫是MongoDB的系統庫,記錄著時間戳記和索引和複製集等資訊

gechongrepl:PRIMARY> use localswitched to db localgechongrepl:PRIMARY> show tablesmeoplog.rsreplset.minvalidslavesstartup_logsystem.indexessystem.replsettemp

  

2:local庫下的每個集合分別記錄的內容

local庫下面的me集合儲存了伺服器名稱local庫下面的replset.minvalid集合儲存了資料庫最新操作的時間戳記local庫下面的startup_log集合記錄這mongod每一次的啟動資訊local庫下面的system.indexes集合記錄當前庫的所有索引資訊local庫下面的system.replset記錄著複製集的成員配置資訊rs.conf()讀取這個集合local庫下面的oplog.rs集合記錄這所有操作MongoDB就是通過oplog.rs來實現資料同步的。當Primary節點插入一條資料後,oplog.rs集合中就會多一條記錄

3:類比資料插入,觀察oplog.rs的變化

gechongrepl:PRIMARY> use testoplogswitched to db testoploggechongrepl:PRIMARY> db.user.insert({"name":"shanghai"})WriteResult({ "nInserted" : 1 })gechongrepl:PRIMARY> use localswitched to db localgechongrepl:PRIMARY> db.oplog.rs.find(){ "_id" : ObjectId("5593628989b809ea7938cc09"), "ts" : Timestamp(1435721382, 1), "h" : NumberLong("-5496045509734463589") }{ "ts" : Timestamp(1435733782, 1), "h" : NumberLong("3093554192925239676"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "Reconfig set", "version" : 57 } }{ "ts" : Timestamp(1435734113, 1), "h" : NumberLong("2858511747060359631"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "Reconfig set", "version" : 58 } }{ "ts" : Timestamp(1435741269, 1), "h" : NumberLong("-4071658638798562344"), "v" : 2, "op" : "i", "ns" : "test.testfile", "o" : { "_id" : ObjectId("5593ac558336a98fb6114045"), "a" : "1" } }{ "ts" : Timestamp(1435745379, 1), "h" : NumberLong("2363981837641873443"), "v" : 2, "op" : "i", "ns" : "test.testfile", "o" : { "_id" : ObjectId("5593bc638336a98fb6114046"), "a" : "1" } }{ "ts" : Timestamp(1435745380, 1), "h" : NumberLong("1415177671185209492"), "v" : 2, "op" : "i", "ns" : "test.testfile", "o" : { "_id" : ObjectId("5593bc648336a98fb6114047"), "a" : "1" } }{ "ts" : Timestamp(1435745570, 1), "h" : NumberLong("-8642862752172159081"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "Reconfig set", "version" : 59 } }{ "ts" : Timestamp(1435746145, 1), "h" : NumberLong("4895205971378560688"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "Reconfig set", "version" : 60 } }{ "ts" : Timestamp(1435803750, 1), "h" : NumberLong("-6248101199236942548"), "v" : 2, "op" : "i", "ns" : "testoplog.user", "o" : { "_id" : ObjectId("5594a065a26e221874aa3e32"), "name" : "shanghai" } }

  

ts:兩個參數:第一個表示時間戳記;第二個表示每秒操作的次數op:作業碼:i表示插入;ns:操作的命名空間o:表示插入操作包含的文檔對象

4:Secondary和Primary資料同步的詳細過程

當Primary節點完成資料操作後,Secondary會做出一系列的動作保證資料的同步:1:檢查自己local庫的oplog.rs集合找出最近的時間戳記。2:檢查Primary節點local庫oplog.rs集合,找出大於此時間戳記的記錄。3:將找到的記錄插入到自己的oplog.rs集合中,並執行這些操作。
注意:新同步過來的資料並不能查看。Secondary預設不可讀不可寫。如果需要查看要執行rs.slaveOk(),則當前的串連可以查看,後續的串連還是不可讀不可寫。

關於oplog.rs的注意事項詳見上一篇部落格:

http://www.cnblogs.com/xiaoit/p/4585363.html

 

 

  

 

MongoDB複本集配置系列十:MongoDB local庫詳解和資料同步原理

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.