On this page本页内容
fsync
Forces the 强制mongod
process to flush all pending writes from the storage layer to disk and locks the entiremongod
instance to prevent additional writes until the user releases the lock with a corresponding fsyncUnlock
. mongod
进程将所有挂起的写入从存储层刷新到磁盘,并锁定整个mongod
实例以防止额外的写入,直到用户使用相应的fsyncUnlock
释放锁定。Optionally, you can use 视需要,您可以使用fsync
to lock the mongod
instance and block write operations for the purpose of capturing backups.fsync
锁定mongod
实例并阻止写操作以捕获备份。
As applications write data, MongoDB records the data in the storage layer and then writes the data to disk within the 当应用程序写入数据时,MongoDB将数据记录在存储层中,然后在syncPeriodSecs
interval, which is 60 seconds by default. syncPeriodSecs
间隔内(默认为60秒)将数据写入磁盘。Run 当您希望在该间隔之前将写操作刷新到磁盘时,请运行fsync
when you want to flush writes to disk ahead of that interval.fsync
。
The fsync
command has the following syntax:fsync
命令具有以下语法:
{ fsync: 1, lock: <Boolean>, comment: <any> }
The fsync
command has the following fields:fsync
命令包含以下字段:
fsync | integer | fsync .fsync 。 |
lock | boolean | mongod instance and blocks all write operations. mongod 实例并阻止所有写入操作。fsync with lock operation takes a lock. lock 的fsync 操作都需要一个锁。 |
comment | any |
|
To run the 要运行fsync
command, use the db.adminCommand()
method:fsync
命令,请使用db.adminCommand()
方法:
db.adminCommand( { fsync: 1, ... } )
带有fsync
command with the lock
option ensures that the data files are safe to copy using low-level backup utilities such as cp
, scp
, or tar
. lock
选项的fsync
命令可确保使用低级备份实用程序(如cp
、scp
或tar
)安全复制数据文件。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备份方法。
An fsync
lock is only possible on individualmongod
instances of a sharded cluster, not on the entire cluster. fsync
锁只能在分片集群的单个mongod
实例上使用,而不能在整个集群上使用。To back up an entire sharded cluster, please see Backup and Restore Sharded Clusters for more information.要备份整个分片化集群,请参阅备份和还原分片化集群以了解更多信息。
If your 如果您的mongod
has journaling enabled, please use file system or volume/block level snapshot tool to create a backup of the data set and the journal together as a single unit.mongod
已启用日志记录,请使用文件系统或卷/块级快照工具将数据集和日志作为一个单元一起创建备份。
fsync
with lock: true
Changed in version 3.4.在版本3.4中更改。
{ fsync: 1, lock: true }
command now returns a lockCount
in the return document.{ fsync: 1, lock: true }
命令现在在返回文档中返回lockCount
。
After 在{ fsync: 1, lock: true }
runs on a mongod
, all write operations will block. mongod
上运行{ fsync: 1, lock: true }
后,所有写操作都将被阻止。mongosh
provides a helper method 提供了一个助手方法db.fsyncLock()
.db.fsyncLock()
。
The { fsync: 1, lock: true }
operation maintain a lock count. { fsync: 1, lock: true }
操作维护一个锁计数。Each 每个{ fsync: 1, lock: true }
operation increments the lock count.{ fsync: 1, lock: true }
操作都会增加锁计数。
To unlock a 要解锁mongod
instance for writes, the lock count must be zero. mongod
实例进行写入,锁计数必须为零。That is, for a given number of 也就是说,对于给定数量的{ fsync: 1, lock: true }
operation, you must issue a corresponding number of unlock operations in order to unlock the instance for writes. { fsync: 1, lock: true }
操作,必须发出相应数量的解锁操作才能解锁实例以进行写入。To unlock, see 要解锁,请参阅db.fsyncUnlock()
.db.fsyncUnlock()
。
mongod
Instancemongod
实例带有fsync
command with the lock
option ensures that the data files are safe to copy using low-level backup utilities such as cp
, scp
, or tar
. lock
选项的fsync
命令可确保使用低级备份实用程序(如cp
、scp
或tar
)安全复制数据文件。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备份方法。
The primary use of fsync
is to lock the mongod
instance in order to back up the files within mongod
's dbPath
. fsync
的主要用途是锁定mongod
实例,以便备份mongod
的dbPath
中的文件。The operation flushes all data to the storage layer and blocks all write operations until you unlock the 该操作将所有数据刷新到存储层,并阻止所有写入操作,直到解锁mongod
instance.mongod
实例。
To lock the database, use the 要锁定数据库,请使用设置为lock
field set to true
:true
的lock
字段:
db.adminCommand( { fsync: 1, lock: true } )
The operation returns a document that includes the status of the operation and 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 }
You may continue to perform read operations on a 您可以继续对具有mongod
instance that has a fsync
lock. fsync
锁的mongod
实例执行读取操作。However, after the first write operation all subsequent read operations wait until you unlock the 然而,在第一次写入操作之后,所有后续的读取操作都会等待,直到您解锁mongod
instance.mongod
实例。
The { fsync: 1, lock: true }
operation maintain a lock count.{ fsync: 1, lock: true }
操作维护一个锁计数。
To unlock a 要解锁mongod
instance for writes, the lock count must be zero. mongod
实例进行写入,锁计数必须为零。That is, for a given number of 也就是说,对于给定数量的{fsync:1,lock:{ fsync: 1, lock: true }
operation, you must issue a corresponding number of unlock operations in order to unlock the instance for writes.true
}操作,必须发出相应数量的解锁操作才能解锁实例以进行写入。
mongod
Instancemongod
实例To unlock the 要解锁mongod
, use db.fsyncUnlock()
:mongod
,请使用db.fsyncUnlock()
:
db.fsyncUnlock();
Repeat the 重复db.fsyncUnlock()
to reduce the lock count to zero to unlock the instance for writes.db.fsyncUnlock()
将锁计数减为零,以解锁实例进行写入。
To check the state of the fsync lock, use 要检查db.currentOp()
. fsync
锁的状态,请使用db.currentOp()
。Use the following JavaScript function in the shell to test if 在shell中使用以下JavaScript函数测试mongod
instance is currently locked:mongod
实例当前是否被锁定:
serverIsLocked = function () { var co = db.currentOp(); if (co && co.fsyncLock) { return true; } return false; }
After loading this function into your 将此函数加载到mongosh
session, call it with the following syntax:mongosh
会话后,使用以下语法调用它:
serverIsLocked()
This function will return 如果true
if the mongod
instance is currently locked and false
if the mongod
is not locked.mongod
实例当前被锁定,该函数将返回true
,如果mongod
未被锁定,则返回false
。