能不能把數據暴力的刷到硬盤上,固然是能夠的,mongodb給咱們提供了fsync+lock機制就能知足咱們提的需求。javascript
fsync+lock首先會把緩衝區數據暴力刷入硬盤,而後給數據庫一個寫入鎖,其餘實例的寫入操做所有被阻塞,直到fsyncjava
+lock釋放鎖爲止。mongodb
這裏就不測試了。數據庫
加鎖: db.runCommand({"fsync":1,"lock":1})app
釋放鎖: db.$cmd.unlock.findOne()async
Forces the mongod
process to flush all pending writes from the storage layer to disk. Optionally, you can use fsync
to 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 syncPeriodSecs
interval, which is 60 seconds by default. Run fsync
when you want to flush writes to disk ahead of that interval.spa
The fsync
command has the following syntax:code
{ fsync: 1, async: <Boolean>, lock: <Boolean> }
The fsync
command has the following fields:orm
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. |
The fsync
operation is synchronous by default. To run fsync
asynchronously, use the async
field set totrue
:
{ fsync: 1, async: true }
The operation returns immediately. To view the status of the fsync
operation, check the output ofdb.currentOp()
.
mongod
InstanceNOTE
Changed in version 3.2: fsync
command with the lock
option 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, 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 fsync
is to lock the mongod
instance in order to back up the files within mongod
‘sdbPath
. The operation flushes all data to the storage layer and blocks all write operations until you unlock themongod
instance.
To lock the database, use the lock
field set to true
:
{ fsync: 1, lock: true }
You may continue to perform read operations on a mongod
instance that has a fsync
lock. However, after the first write operation all subsequent read operations wait until you unlock the mongod
instance.