1. oplog裏面有啥
MongoDB 的Replication是通過一個日誌來存儲寫操作的,這個日誌就叫做oplog。
在默認情況下,oplog分配的是5%的空閒磁盤空間。通常而言,這是一種合理的設置。可以通過mongod --oplogSize
來改變oplog的日誌大小。
oplog是capped collection,因爲oplog的特點(不能太多把磁盤填滿了,固定大小)需要,MongoDB才發明了capped collection(the oplog is actually the reason capped collections were invented).
2. oplog存在哪裏
一般oplog存儲在mongo的local庫中(replica sets架構下通過db.oplog.rs訪問),local庫是MongoDB的系統庫,記錄着時間戳和索引和複製集等信息,具體包括以下內容:
- me集合保存了服務器名稱
- replset.minvalid集合保存了數據庫最新操作的時間戳
- startup_log集合記錄這mongod每一次的啓動信息
- system.indexes集合記錄當前庫的所有索引信息
- system.replset記錄着複製集的成員配置信息rs.conf()讀取這個集合
- oplog.rs集合記錄着所有操作,MongoDB就是通過oplog.rs來實現數據同步的。當Primary節點插入一條數據後,oplog.rs集合中就會多一條記錄。
3. oplog是啥結構的
如下圖:
- ts: the time this operation occurred.
8字節的時間戳,由4字節unix timestamp + 4字節自增計數表示
- h: a unique ID for this operation. Each operation will have a different value in this field.
操作ID,每個操作都有不同的ID
- op: the write operation that should be applied to the slave. n indicates a no-op, this is just an informational message.
1字節的操作類型
- ns: the database and collection affected by this operation. Since this is a no-op, this field is left blank.
操作所在的namespace
- o: the actual document representing the op. Since this is a no-op, this field is pretty useless.
The o field now contains the document to insert or the criteria to update and remove. Notice that, for the update, there are two o fields (o and o2). o2 give the update criteria and o gives the modifications (equivalent to update()‘s second argument).操作所對應的document,即當前操作的內容(比如更新操作時要更新的的字段和值,插入操作的字段和值)
- o2:
這個字段在執行update操作的時候會出現
op包括以下幾種操作類型:
“i”: insert
“u”: update
“d”: delete
“c”: db cmd
“n”: no op,即空操作,其會定期執行以確保時效性 。修改配置,會產生 “n” 操作
4. oplog怎麼查
已經知道了結構,其他的就是mongo基礎啦。其中db.getReplicationInfo()
可以獲取oplog的當前狀態: