mongodb复制集里查看主从操作日志oplog

时间:2024-10-05 21:07:02

MongoDB的replica set架构是通过一个日志来存储写操作的,这个日志就叫做 oplog 。oplog.rs 是一个固定长度的 Capped Collection,它存在于local数据库中,用于记录replicaSets操作日志。在默认情况下,对于64位的MongoDB,oplog是比较大的,可以达到5%的磁盘空间,oplog的大小是可以通过mongod的参数 “ -oplogSize”来改变oplog的日志大小。

oplog内容样例:

> use local

> show collections

> db.oplog.rs.find()

新版有个问题,无法再像旧版一样直接操作了,必须先添加一个管理员用户,之后切换到管理员身份,再进行操作,具体可以见日志。

rs1:PRIMARY> show dbsshow dbs

2016-07-22T17:44:25.491+0800 E QUERY    [thread1] Error: listDatabases failed:{

"ok" : 0,

"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",

"code" : 13

} :

_getErrorWithCode@src/mongo/shell/utils.js:25:13

Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1

shellHelper.show@src/mongo/shell/utils.js:761:19

shellHelper@src/mongo/shell/utils.js:651:15

@(shellhelp2):1:1

rs1:PRIMARY> use localuse local

switched to db local

rs1:PRIMARY> show collectionsshow collections

2016-07-22T17:44:40.317+0800 E QUERY    [thread1] Error: listCollections failed: {

"ok" : 0,

"errmsg" : "not authorized on local to execute command { listCollections: 1.0, filter: {} }",

"code" : 13

} :

_getErrorWithCode@src/mongo/shell/utils.js:25:13

DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:773:1

DB.prototype.getCollectionInfos@src/mongo/shell/db.js:785:19

DB.prototype.getCollectionNames@src/mongo/shell/db.js:796:16

shellHelper.show@src/mongo/shell/utils.js:754:9

shellHelper@src/mongo/shell/utils.js:651:15

@(shellhelp2):1:1

rs1:PRIMARY> use adminuse admin

switched to db admin

rs1:PRIMARY> db.createUser(db.createUser(

...       {      {

...         user: "bbb",        user: "bbb",

...         pwd: "bbb",        pwd: "bbb",

...         roles: [ { role: "root", db: "admin" } ]        roles: [ { role: "root", db: "admin" } ]

...       }      }

...     )    )

Successfully added user: {

"user" : "bbb",

"roles" : [

{

"role" : "root",

"db" : "admin"

}

]

}

rs1:PRIMARY> show dbsshow dbs

2016-07-22T17:45:54.756+0800 E QUERY    [thread1] Error: listDatabases failed:{

"ok" : 0,

"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",

"code" : 13

} :

_getErrorWithCode@src/mongo/shell/utils.js:25:13

Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1

shellHelper.show@src/mongo/shell/utils.js:761:19

shellHelper@src/mongo/shell/utils.js:651:15

@(shellhelp2):1:1

rs1:PRIMARY> db.auth('bbb','bbb')db.auth('bbb','bbb')//此处认证过了才可以操作

1

rs1:PRIMARY> use localuse local

switched to db local

rs1:PRIMARY> show collectionsshow collections

me

oplog.rs

replset.election

startup_log

system.replset

rs1:PRIMARY> db.oplog.rs.find()db.oplog.rs.find()

{ "ts" : Timestamp(1469180416, 1), "h" : NumberLong("6313428569778261950"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "initiating set" } }

{ "ts" : Timestamp(1469180428, 2), "t" : NumberLong(1), "h" : NumberLong("2174888517165095400"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "new primary" } }

{ "ts" : Timestamp(1469180731, 1), "t" : NumberLong(1), "h" : NumberLong("-1515174104405674576"), "v" : 2, "op" : "c", "ns" : "admin.$cmd", "o" : { "create" : "system.version" } }

{ "ts" : Timestamp(1469180731, 2), "t" : NumberLong(1), "h" : NumberLong("7948077691112953460"), "v" : 2, "op" : "i", "ns" : "admin.system.version", "o" : { "_id" : "authSchema", "currentVersion" : 5 } }

{ "ts" : Timestamp(1469180731, 3), "t" : NumberLong(1), "h" : NumberLong("-9003778602149648332"), "v" : 2, "op" : "c", "ns" : "admin.$cmd", "o" : { "create" : "system.users" } }

{ "ts" : Timestamp(1469180731, 4), "t" : NumberLong(1), "h" : NumberLong("-5105507245190417325"), "v" : 2, "op" : "i", "ns" : "admin.system.users", "o" : { "_id" : "admin.bbb", "user" : "bbb", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "9hPZ67Nn1d9ZFSKfxfPmHw==", "storedKey" : "wbVBdAFG3tKPU0QwgoqA93XaSFY=", "serverKey" : "0FXoMrw01NIiYr8HNZ9p5mGhK8k=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] } }

rs1:PRIMARY>

查看master的oplog元数据信息:

> db.printReplicationInfo()

查看salve的同步状态:

> db.printSlaveReplicationInfo()

rs1:PRIMARY> db.printReplicationInfo()db.printReplicationInfo()

configured oplog size:   2124.869140625MB

log length start to end: 315secs (0.09hrs)

oplog first event time:  Fri Jul 22 2016 17:40:16 GMT+0800 (CST)

oplog last event time:   Fri Jul 22 2016 17:45:31 GMT+0800 (CST)

now:                     Fri Jul 22 2016 17:52:59 GMT+0800 (CST)

rs1:PRIMARY>  db.printSlaveReplicationInfo() db.printSlaveReplicationInfo()

source: localhost:28011

syncedTo: Fri Jul 22 2016 17:45:31 GMT+0800 (CST)

0 secs (0 hrs) behind the primary

source: localhost:28012

syncedTo: Fri Jul 22 2016 17:45:31 GMT+0800 (CST)

0 secs (0 hrs) behind the primary

rs1:PRIMARY>