锁住写操作
> db.fsyncLock();
{
"info" : "now locked against writes, use db.fsyncUnlock() to unlock",
"lockCount" : NumberLong(1),
"seeAlso" : "http://dochub.mongodb.org/core/fsynccommand",
"ok" : 1
}
> db.currentOp()
{
"inprog" : [
{
"desc" : "conn10",
"threadId" : "140688623027968",
"connectionId" : 10,
"client" : "127.0.0.1:40943",
"appName" : "MongoDB Shell",
"active" : true,
"opid" : 6757,
"secs_running" : 0,
"microsecs_running" : NumberLong(27),
"op" : "command",
"ns" : "admin.$cmd",
"query" : {
"currentOp" : 1
},
"numYields" : 0,
"locks" : {
},
"waitingForLock" : false,
"lockStats" : {
}
},
{
"desc" : "conn8",
"threadId" : "140688851060480",
"connectionId" : 8,
"client" : "172.16.160.92:39130",
"active" : true,
"opid" : 6755,
"secs_running" : 0,
"microsecs_running" : NumberLong(867788),
"op" : "getmore",
"ns" : "local.oplog.$main",
"query" : {
"ts" : {
"$gte" : Timestamp(1512120014, 1)
}
},
"planSummary" : "COLLSCAN",
"numYields" : 0,
"locks" : {
},
"waitingForLock" : false,
"lockStats" : {
"Global" : {
"acquireCount" : {
"r" : NumberLong(2)
}
},
"Database" : {
"acquireCount" : {
"r" : NumberLong(1)
}
},
"Collection" : {
"acquireCount" : {
"r" : NumberLong(1)
}
}
}
},
{
"desc" : "fsyncLockWorker",
"threadId" : "140688631420672",
"active" : true,
"opid" : 6719,
"op" : "none",
"ns" : "",
"query" : {
},
"numYields" : 0,
"locks" : {
"Global" : "R"
},
"waitingForLock" : false,
"lockStats" : {
"Global" : {
"acquireCount" : {
"r" : NumberLong(1),
"R" : NumberLong(1),
"W" : NumberLong(1)
}
}
}
},
{
"desc" : "WT RecordStoreThread: local.oplog.$main",
"threadId" : "140688725845760",
"active" : true,
"opid" : 5,
"secs_running" : 2910,
"microsecs_running" : NumberLong("2910134851"),
"op" : "none",
"ns" : "local.oplog.$main",
"query" : {
},
"numYields" : 0,
"locks" : {
},
"waitingForLock" : false,
"lockStats" : {
"Global" : {
"acquireCount" : {
"r" : NumberLong(1),
"w" : NumberLong(1)
}
},
"Database" : {
"acquireCount" : {
"w" : NumberLong(1)
}
},
"Collection" : {
"acquireCount" : {
"w" : NumberLong(1)
}
}
}
},
{
"desc" : "replslave",
"threadId" : "140688683882240",
"active" : true,
"opid" : 6,
"secs_running" : 2910,
"microsecs_running" : NumberLong("2910094579"),
"op" : "none",
"ns" : "local.me",
"query" : {
},
"numYields" : 0,
"locks" : {
"Global" : "W"
},
"waitingForLock" : true,
"lockStats" : {
"Global" : {
"acquireCount" : {
"r" : NumberLong(18788),
"W" : NumberLong(9344)
},
"acquireWaitCount" : {
"W" : NumberLong(3)
},
"timeAcquiringMicros" : {
"W" : NumberLong(92534923)
}
},
"Database" : {
"acquireCount" : {
"r" : NumberLong(4722)
}
},
"Collection" : {
"acquireCount" : {
"r" : NumberLong(4722)
}
}
}
}
],
"fsyncLock" : true,
"info" : "use db.fsyncUnlock() to terminate the fsync write/snapshot lock",
"ok" : 1
}
解锁
> db.fsyncUnlock()db.fsyncUnlock()
{ "info" : "fsyncUnlock completed", "lockCount" : NumberLong(0), "ok" : 1 }
>
从库上操作
这个命令用于,先同步完数据,再锁住写操作,只能在从库上操作,在主库上会提示如下:
> db.runCommand({fsync:1,lock:1})
{
"ok" : 0,
"errmsg" : "fsync may only be run against the admin database.",
"code" : 13,
"codeName" : "Unauthorized"
}
>