Navigation

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.

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.

To unlock a mongod instance for writes, the lock count must be zero. 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() 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.4)- The number of locks currently on the instance.
  • seeAlso - Link to the fsync command documentation.
  • ok - The status code.

This command provides a simple wrapper around a fsync database command with the following syntax:

{ 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

Compatibility with WiredTiger

Changed in version 3.2: db.fsyncLock() can ensure that the data files do not change for MongoDB instances using either the MMAPv1 or the WiredTiger storage engines, thus providing consistency for the purposes of creating backups.

In previous MongoDB versions, db.fsyncLock() cannot guarantee a consistent set of files for low-level backups (e.g. via file copy cp, scp, tar) for WiredTiger.

Example

The following operation runs db.fsyncLock():

db.fsyncLock()

The operation returns the following status document that includes the 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:

{
   "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.