Docs HomeMongoDB Manual

db.fsyncLock()

On this page本页内容

Definition定义

db.fsyncLock()

Forces the mongod to flush all pending write operations to disk and locks the entire mongod instance to prevent additional writes until the user releases the lock with a corresponding db.fsyncUnlock() command.强制mongod将所有挂起的写入操作刷新到磁盘,并锁定整个mongod实例以防止额外的写入,直到用户使用相应的db.fsyncUnlock()命令释放锁定为止。

Important

The db.fsyncLock() and db.fsyncUnlock() operations maintain a lock count. db.fsyncLock() increments the lock count, and db.fsyncUnlock() decrements the lock count.db.fsyncLock()db.fsyncUnlock()操作维护一个锁计数。db.fsyncLock()递增锁计数,db.fsyncUnlock()递减锁计数。

To unlock a mongod instance for writes, the lock count must be zero. 要解锁mongod实例进行写入,锁计数必须为零。That is, for a given number of db.fsyncLock() operations, you must issue a corresponding number of db.fsyncUnlock() operations to unlock the instance for writes.也就是说,对于给定数量的db.fsyncLock()操作,必须发出相应数量的db.fsyncUnlock()操作来解锁实例以进行写入。

db.fsyncLock() has the syntax:具有以下语法:

db.fsyncLock()

The operation returns a document with the following fields:该操作返回一个包含以下字段的文档:

  • info - Information on the status of the operation有关操作状态的信息
  • lockCount (New in version 3.43.4版新增)- The number of locks currently on the instance.实例上当前的锁数。
  • seeAlso - Link to the fsync command documentation.fsync命令文档的链接。
  • ok - The status code.状态代码。

This command provides a simple wrapper around a fsync database command with the following syntax:此命令为fsync数据库命令提供了一个简单的包装器,语法如下:

{ fsync: 1, lock: true }

db.fsyncLock() is an administrative command. 是一个管理命令。You can use this operation to locks the database and create a window for backup operations.您可以使用此操作锁定数据库并为备份操作创建窗口。

Behavior行为

db.fsyncLock() ensures that the data files are safe to copy using low-level backup utilities such as cp, scp, or tar. 确保使用低级别备份实用程序(如cpscptar)可以安全地复制数据文件。A mongod started using the copied files contains user-written data that is indistinguishable from the user-written data on the locked mongod.使用复制文件启动的mongod包含用户写入的数据,这些数据与锁定mongod上的用户写入数据无法区分。

The data files of a locked mongod may change due to operations such as journaling syncs or WiredTiger snapshots. 锁定mongod的数据文件可能会因日志同步WiredTiger快照等操作而更改。While this has no affect on the logical data (e.g. data accessed by clients), some backup utilities may detect these changes and emit warnings or fail with errors. 虽然这对逻辑数据(例如客户端访问的数据)没有影响,但一些备份实用程序可能会检测到这些更改并发出警告或出现错误而失败。For more information on MongoDB-recommended backup utilities and procedures, see MongoDB Backup Methods.有关MongoDB推荐的备份实用程序和过程的更多信息,请参阅MongoDB备份方法

Example实例

The following operation runs db.fsyncLock():以下操作将运行db.fsyncLock()

db.fsyncLock()

The operation returns the following status document that includes the lockCount:该操作返回以下状态文档,其中包括lockCount

{
"info" : "now locked against writes, use db.fsyncUnlock() to unlock",
"lockCount" : NumberLong(1),
"seeAlso" : "http://dochub.mongodb.org/core/fsynccommand",
"ok" : 1
}

If you run db.fsyncLock() again, the operation increments the lockCount:如果再次运行db.fsyncLock(),该操作将递增lockCount

{
"info" : "now locked against writes, use db.fsyncUnlock() to unlock",
"lockCount" : NumberLong(2),
"seeAlso" : "http://dochub.mongodb.org/core/fsynccommand",
"ok" : 1
}

To unlock the instance for writes, you must run db.fsyncUnlock() twice to reduce the lockCount to 0.若要解锁实例进行写入,必须运行db.fsyncUnlock()两次才能将lockCount减少到0