主从操作日志oplog
MongoDB的Replica Set架构是通过一个日志来存储写操作的,这个日志就叫做"oplog"。oplog.rs是一个固定长度的capped collection,它存在于"local"数据库中,用于记录Replica Sets操作日志。在默认情况下,对于64位的MongoDB,oplog是比较大的,可以达到5%的磁盘空间。oplog的大小是可以通过mongod的参数“--oplogSize”来设置。
rs1:PRIMARY> use local switched to db local rs1:PRIMARY> show collections oplog.rs system.replset rs1:PRIMARY> db.oplog.rs.find() { "ts" : { "t" : 1338457763000, "i" : 1 }, "h" : NumberLong(0), "op" : "n", "ns" : "", "o" : { "msg" : "initiating set" } } { "ts" : { "t" : 1338459114000, "i" : 1 }, "h" : NumberLong("5493127699725549585"), "op" : "i", "ns" : "test.c1", "o" : { "_id" : ObjectId("4fc743e9aea289af709ac6b5"), "age" : 29, "name" : "Tony" } } rs1:PRIMARY>
字段说明
ts: 某个操作的时间戳
op: 操作类型,如下:
i: insert
d: delete
u: update
ns: 命名空间,也就是操作的collection name
o: document 的内容
查看master 的oplog 元数据信息:
rs1:PRIMARY> db.printReplicationInfo() configured oplog size: 47.6837158203125MB log length start to end: 1351secs (0.38hrs) oplog first event time: Thu May 31 2012 17:49:23 GMT+0800 (CST) oplog last event time: Thu May 31 2012 18:11:54 GMT+0800 (CST) now: Thu May 31 2012 18:21:58 GMT+0800 (CST) rs1:PRIMARY>
configured oplog size: 配置的oplog 文件大小
log length start to end: oplog 日志的启用时间段
oplog first event time: 第一个事务日志的产生时间
oplog last event time: 最后一个事务日志的产生时间
now: 现在的时间
查看slave 的同步状态
rs1:PRIMARY> db.printSlaveReplicationInfo() source: localhost:28011 syncedTo: Thu May 31 2012 18:11:54 GMT+0800 (CST) = 884secs ago (0.25hrs) source: localhost:28012 syncedTo: Thu May 31 2012 18:11:54 GMT+0800 (CST) = 884secs ago (0.25hrs) rs1:PRIMARY>
字段说明
source: 从库的IP 及端口
syncedTo: 目前的同步情况,延迟了多久等信息
主从配置信息
在local库中不仅有主从日志oplog集合,还有一个集合用于记录主从配置信息,即:system.replset
rs1:PRIMARY> use local switched to db local rs1:PRIMARY> show collections oplog.rs system.replset rs1:PRIMARY> db.system.replset.find() { "_id" : "rs1", "version" : 1, "members" : [ { "_id" : 0, "host" : "localhost:28010" }, { "_id" : 1, "host" : "localhost:28011" }, { "_id" : 2, "host" : "localhost:28012" } ] } rs1:PRIMARY>
从这个集合中可以看出,Replica Sets 的配置信息,也可以在任何一个成员实例上执行rs.conf()来查看配置信息。