Can the data violence to the hard disk, of course, is possible, MongoDB provides us with a fsync+lock mechanism to meet our needs.
Fsync+lock first will buffer data violence into the hard disk, and then a write lock to the database, the other instances of the write operations are all blocked until Fsync
+lock Release the lock.
It's not going to be tested here.
Locking: Db.runcommand ({"Fsync": 1, "Lock": 1})
Release Lock: Db. $cmd. Unlock.findone ()
Fsync
-
Forces the mongod
process to flush all pending writes from the storage layer to disk. Optionally, you can use to fsync
lock the mongod
instance and block write operations for the purpose of capturing backups.
As applications write data, MongoDB records the data in the storage layer and then writes the data to disk within the interval, which is a seconds by default. Run when you fsync
want to flush writes to disk ahead of the that interval.
The fsync
command has the following syntax:
Fsync1Async<boolean>lock<boolean}
The fsync
command has the following fields:
Field |
Type |
Description |
fsync |
Integer |
Enter "1" to apply fsync . |
async |
Boolean |
Optional. Runs fsync asynchronously. By default, the fsync operation is synchronous. |
lock |
Boolean |
Optional. Locks mongod instance and blocks all write operations. |
Examplesrun asynchronously
The fsync
operation is synchronous by default. To run fsync
asynchronously with the async
field set to true
:
Fsync1Async}
The operation returns immediately. fsync
to view the status of the operation, check the output of db.currentOp()
.
Lock
mongod
Instance
NOTE
Changed in version 3.2: fsync
command with the lock
option can ensure that the data files does not change for MongoDB Insta NCEs using either the MMAPV1 or the Wiredtiger storage engines, thus providing consistency for the purposes of creating BA Ckups.
In previous MongoDB versions, fsync
command with the lock
option cannot guarantee a consistent set of files for Low-level backups (e.g. via file copy cp
, scp
, tar
) for Wiredtiger.
The primary use of was to lock the instance in order-to-back up the fsync
mongod
files within mongod
' s dbPath
. The operation flushes all data to the storage layer and blocks all write operations until you unlock the mongod
instance.
To lock the database, with the lock
field set to true
:
Fsync1lock}
You could continue to perform read operations on a mongod
instance it has a fsync
lock. However, after the first write operation all subsequent read operations wait until you unlock the mongod
instance.
Unlock
mongod
Instance
mongod
to unlock the, use db.fsyncUnlock()
:
DB. Fsyncunlock();
See: https://docs.mongodb.com/manual/reference/command/fsync/
MongoDB gives us the fsync+lock mechanism to put data violence on the hard drive