MongoDB如何查oplog.rs集合里的操作日志
首先需要介紹一下
mongodb local庫(kù)的作用
local庫(kù)是MongoDB的系統(tǒng)庫(kù),記錄著時(shí)間戳和索引和復(fù)制集等信息
test:PRIMARY> use local
switched to db local
test:PRIMARY> show tables;
system.replset
me
startup_log
replset.minvalid
oplog.rs
replset.election
test:PRIMARY>
local庫(kù)下的每個(gè)集合分別記錄的內(nèi)容
local庫(kù)下面的me集合保存了
服務(wù)器名稱
local庫(kù)下面的replset.minvalid集合保存了數(shù)據(jù)庫(kù)最新操作的時(shí)間戳
local庫(kù)下面的startup_log集合記錄這mongod每一次的啟動(dòng)信息
local庫(kù)下面的system.indexes集合記錄當(dāng)前庫(kù)的所有索引信息
local庫(kù)下面的system.replset記錄著復(fù)制集的成員配置信息rs.conf()讀取這個(gè)集合
local庫(kù)下面的oplog.rs集合記錄著所有操作,MongoDB就是通過(guò)oplog.rs來(lái)實(shí)現(xiàn)數(shù)據(jù)同步的。當(dāng)Primary節(jié)點(diǎn)插入一條數(shù)據(jù)后,oplog.rs集合中就會(huì)多一條記錄
op:操作類型,插入對(duì)應(yīng)i;更新對(duì)應(yīng)u;刪除對(duì)應(yīng)d;但有一種情況是n,它表示無(wú)操作(no-op),緊緊代表一個(gè)消息信息。
例如:
查詢oplog里的insert記錄,對(duì)應(yīng)op為i的記錄:
test:PRIMARY> db.oplog.rs.find({"op" : "i"}).pretty().limit(3)
查update操作命令:
test:PRIMARY> db.oplog.rs.find({"op" : "u"}).pretty().limit(3)
test:PRIMARY>
查delete操作命令:
test:PRIMARY> db.oplog.rs.find({"op" : "d"}).pretty().limit(3)
根據(jù)時(shí)間范圍查操作:
例如:
查找2017年9月1日之后的操作記錄:
test:PRIMARY> db.oplog.rs.find({"o.createTime": {$gte:new Date(2017,9,1)}}).limit(3)
查2017年9月1日到10月31日之間的操作記錄:
test:PRIMARY> db.oplog.rs.find({"o.createTime": {$gte:new Date(2017,9,1),$lte:new Date(2017,10,31)}}).limit(3)
我們查查對(duì)chenfeng表進(jìn)行操作的所有的 oplog 記錄
test:PRIMARY> db.oplog.rs.find({ns : "testdb.chenfeng"})
查操作的時(shí)間戳:
db.oplog.rs.find({"op" : "i","ns" : "testdb.chenfeng",},{ts:1}).pretty()
{ "ts" : Timestamp(1509246747, 1) }
{ "ts" : Timestamp(1509246747, 2) }
{ "ts" : Timestamp(1509246747, 3) }
{ "ts" : Timestamp(1509246747, 4) }
{ "ts" : Timestamp(1509246747, 5) }
當(dāng)前操作時(shí)間點(diǎn)的時(shí)間戳:
test:PRIMARY> rs.status().members[0].optime.ts
Timestamp(1509247609, 3001)
如何備份oplog集合:
[root@192 new]# mongodump --host localhost --port 9336 -uadmin -padmin --authenticationDatabase=admin -d local -c oplog.rs -o /root/new
2017-10-29T12:36:45.605+0800 writing local.oplog.rs to local/oplog.rs.bson
2017-10-29T12:36:48.606+0800 [#.......................] local.oplog.rs 398917/4937240 (8.1%)
2017-10-29T12:36:51.610+0800 [###.....................] local.oplog.rs 812569/4937240 (16.5%)
2017-10-29T12:36:54.611+0800 [#####...................] local.oplog.rs 1208741/4937240 (24.5%)
2017-10-29T12:36:57.607+0800 [#######.................] local.oplog.rs 1570342/4937240 (31.8%)
2017-10-29T12:37:00.617+0800 [#########...............] local.oplog.rs 1948323/4937240 (39.5%)
2017-10-29T12:37:03.606+0800 [###########.............] local.oplog.rs 2346269/4937240 (47.5%)
2017-10-29T12:37:06.606+0800 [#############...........] local.oplog.rs 2741569/4937240 (55.5%)
2017-10-29T12:37:09.609+0800 [###############.........] local.oplog.rs 3137392/4937240 (63.5%)
2017-10-29T12:37:12.606+0800 [#################.......] local.oplog.rs 3533685/4937240 (71.6%)
2017-10-29T12:37:15.610+0800 [##################......] local.oplog.rs 3897290/4937240 (78.9%)
2017-10-29T12:37:19.672+0800 [###################.....] local.oplog.rs 3947430/4937240 (80.0%)
2017-10-29T12:37:21.609+0800 [####################....] local.oplog.rs 4147978/4937240 (84.0%)
2017-10-29T12:37:24.607+0800 [#####################...] local.oplog.rs 4518546/4937240 (91.5%)
2017-10-29T12:37:27.606+0800 [#######################.] local.oplog.rs 4910376/4937240 (99.5%)
2017-10-29T12:37:27.758+0800 writing local.oplog.rs metadata to local/oplog.rs.metadata.json
2017-10-29T12:37:27.770+0800 done dumping local.oplog.rs (4937240 documents)
用bsondump查操作記錄:
查插入操作:
# bsondump oplog.rs.bson | grep "\"op\":\"i\"" | head
查刪除操作:
# bsondump oplog.rs.bson | grep "\"op\":\"d\"" | head
查更新操作:
[root@192 local]# bsondump oplog.rs.bson | grep "\"op\":\"u\"" | head
恢復(fù)oplog集合命令:
mongorestore --host localhost --port 9336 -uadmin -padmin --authenticationDatabase=admin -d local -c oplog.rs /root/new/local/oplog.rs.bson