db.fsyncLock()

Definition

  • db.fsyncLock()
  • Forces the mongod to flush all pending write operations todisk and locks the entiremongod instance to preventadditional writes until the user releases the lock with a correspondingdb.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 countmust be zero. That is, for a given number ofdb.fsyncLock() operations, you must issue acorresponding number of db.fsyncUnlock() operations tounlock the instance for writes.

db.fsyncLock() has the syntax:

  1. 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 fsyncdatabase command with the following syntax:
  1. { fsync: 1, lock: true }

db.fsyncLock() is an administrative command. You can usethis operation to locks the database and create a window forbackup operations.

Behavior

db.fsyncLock() ensures that the data files are safe to copyusing low-level backup utilities such as cp, scp, ortar. A mongod started using the copiedfiles contains user-written data that is indistinguishable from theuser-written data on the locked mongod.

The data files of a locked mongod may change due tooperations such as journaling syncs orWiredTiger snapshots. Whilethis has no affect on the logical data (e.g. data accessed byclients), some backup utilities may detect these changes and emitwarnings or fail with errors. For more information on MongoDB-recommended backup utilities and procedures, seeMongoDB Backup Methods.

Example

The following operation runs db.fsyncLock():

  1. db.fsyncLock()

The operation returns the following status document that includes thelockCount:

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

If you run db.fsyncLock() again, the operation increments thelockCount:

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

To unlock the instance for writes, you must rundb.fsyncUnlock() twice to reduce the lockCount to 0.