Database Manual / Reference / Database Commands / Administration

fsync (database command数据库命令)

Definition定义

fsync

Flushes all pending writes from the storage layer to disk. When the lock field is set to true, it sets a lock on the server or cluster to prevent additional writes until the lock is released.将所有待处理的写入从存储层刷新到磁盘。当lock字段设置为true时,它会在服务器或集群上设置一个锁,以防止在锁释放之前进行额外的写入。

Starting in MongoDB 7.1 (also available starting in 7.0.2, 6.0.11, and 5.0.22) the fsync and fsyncUnlock commands can run on mongos to lock and unlock a sharded cluster.从MongoDB 7.1开始(从7.0.2、6.0.11和5.0.22开始也可用),fsyncfsyncUnlock命令可以在mongos上运行,以锁定和解锁分片集群。

As applications write data, MongoDB records the data in the storage layer and then writes the data to disk.当应用程序写入数据时,MongoDB会将数据记录在存储层中,然后将数据写入磁盘。

Run fsync when you want to flush writes to disk.当您想刷新对磁盘的写入时,请运行fsync

To provide durable data, WiredTiger uses checkpoints. 为了提供持久的数据,WiredTiger使用检查点For more details, see Journaling and the WiredTiger Storage Engine.有关更多详细信息,请参阅日志和WiredTiger存储引擎

Important

Servers maintain an fsync lock count. 服务器维护fsync锁计数。 The fsync command with the lock field set to true increments the lock count while the fsyncUnlock command decrements it. 锁定字段设置为true的fsync命令递增锁定计数,而fsyncUnlock命令递减锁定计数。To enable writes on a locked server or cluster, call the fsyncUnlock command until the lock count reaches zero.要在锁定的服务器或群集上启用写入,请调用fsyncUnlock命令,直到锁定计数达到零。

Use this command to block writes when you want to perform backup operations.当您想要执行备份操作时,使用此命令阻止写入。

Tip

In mongosh, this command can also be run through the db.fsyncLock() helper method.mongosh中,此命令也可以通过db.fsyncLock()辅助方法运行。

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. 助手方法对mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。In cases where the convenience is not needed or the additional return fields are required, use the database command.如果不需要便利性或需要额外的返回字段,请使用database命令。

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.adminCommand(
{
fsync: 1,
lock: <Boolean>,
fsyncLockAcquisitionTimeout: <integer>,
comment: <any>
}
)

Command Fields命令字段

The command has the following fields:该命令包含以下字段:

Field字段Type类型Description描述
fsyncinteger整数Enter "1" to apply fsync.
fsyncLockAcquisitionTimeoutMillisinteger整数

Optional. 可选。Specifies the amount of time in milliseconds to wait to acquire locks. If the lock acquisition operation times out, the command returns a failed response.指定等待获取锁的时间量(毫秒)。如果锁获取操作超时,则命令返回失败的响应。

Default: 90000

New in version 7.1.在版本7.1中新增。

lockboolean布尔值Optional. 可选。Takes a lock on the server or cluster and blocks all write operations. Each fsync with lock operation takes a lock.锁定服务器或群集并阻止所有写入操作。每个带有lockfsync操作都需要一个锁。
commentany任意

Optional. 可选。A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:用户提供了要附加到此命令的注释。设置后,此注释将与此命令的记录一起出现在以下位置:

A comment can be any valid BSON type (string, integer, object, array, etc).注释可以是任何有效的BSON类型(字符串、整数、对象、数组等)。

Considerations注意事项

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命令可确保使用cpscptar等低级备份实用程序复制数据文件是安全的。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 effect 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 Backup Methods for a Self-Managed Deployment.有关MongoDB推荐的备份实用程序和过程的更多信息,请参阅自我管理部署的备份方法

Impact on Larger Deployments对大规模部署的影响

New in version 7.1.在版本7.1中新增。

When the fsync command runs on mongos, it performs the fsync operation on the entire cluster. By setting the lock field to true, it sets a lock on the cluster, preventing additional writes.当fsync命令在mongos上运行时,它会在整个集群上执行fsync操作。通过将lock字段设置为true,它在集群上设置了一个锁,防止了额外的写入。

To take a usable self-managed backup, before locking a sharded cluster:要在锁定分片群集之前进行可用的自我管理备份,请执行以下操作:

  • Ensure that no chunk migration, resharding, or DDL operations are active.确保没有块迁移、重新标记或DDL操作处于活动状态。
  • Stop the balancer to prevent additional chunk migrations from starting.停止平衡器以防止启动额外的块迁移。

Alternatives with Journaling日志记录的替代方案

If your mongod has journaling enabled, use a 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启用了日志记录,请使用文件系统或卷/块级快照工具将数据集和日志作为一个单元一起创建备份。

Lock Count锁计数

The fsync command returns a document includes a lockCount field. When run on mongod, the count indicates the number of fsync locks set on the server.fsync命令返回一个包含lockCount字段的文档。在mongod上运行时,计数表示服务器上设置的fsync锁的数量。

When run on a sharded cluster, mongos sends the fsync operation to each shard and returns the results, which includes the lockCount for each.在分片集群上运行时,mongos向每个分片发送fsync操作并返回结果,其中包括每个分片的lockCount

Note

If the lockCount field is greater than zero, all writes are blocked on the server and cluster. 如果lockCount字段大于零,则服务器和集群上的所有写入都将被阻止。To reduce the lock count, use the fsyncUnlock command.要减少锁计数,请使用fsyncUnlock命令。

Fsync Locks after Failures故障后Fsync锁定

Fsync locks execute on the primary in a replica set or sharded cluster.Fsync锁在副本集或分片集群的主服务器上执行。

If the primary goes down or becomes unreachable due to network issues, the cluster elects a new primary from the available secondaries. 如果主服务器因网络问题而停机或无法访问,集群将从可用的次服务器中选择一个新的主服务器。If a primary with an fsync lock goes down, the new primary does not retain the fsync lock and can handle write operations. When elections occur during backup operations, the resulting backup may be inconsistent or unusable.如果具有fsync锁的主服务器发生故障,则新主服务器不会保留fsync锁,可以处理写操作。在备份操作期间发生选举时,产生的备份可能不一致或不可用。

To recover from the primary going down:要从主故障中恢复:

  1. Run the fsyncUnlock command until the lock count reaches zero to release the lock on all nodes.运行fsyncUnlock命令,直到锁计数达到零,以释放所有节点上的锁。
  2. Issue the fsync command to reestablish the fsync lock on the cluster.发出fsync命令以在集群上重新建立fsync锁。
  3. Restart the backup.重新启动备份。

Additionally, fsync locks are persistent. When the old primary comes online again, you need to use the fsyncUnlock command to release the lock on the node.此外,fsync锁是持久的。当旧主节点再次联机时,您需要使用fsyncUnlock命令释放节点上的锁。

Examples示例

Fsync LockFsync锁

Note

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命令可确保使用cpscptar等低级备份实用程序复制数据文件是安全的。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. While this has no effect on the logical data (e.g. data accessed by clients), some backup utilities may detect these changes and emit warnings or fail with errors. 锁定的mongod的数据文件可能会因日志同步WiredTiger快照等操作而更改。虽然这对逻辑数据(例如客户端访问的数据)没有影响,但一些备份实用程序可能会检测到这些更改并发出警告或出现错误。For more information on MongoDB-recommended backup utilities and procedures, see Backup Methods for a Self-Managed Deployment.有关MongoDB推荐的备份实用程序和过程的更多信息,请参阅自我管理部署的备份方法

The fsync command can lock an individual mongod instance or a sharded cluster through mongos. fsync命令可以通过mongos锁定单个mongod实例或分片集群。When run with the lock field set to true, the fsync operation flushes all data to the storage layer and blocks all additional write operations until you unlock the instance or cluster.当将lock字段设置为true运行时,fsync操作会将所有数据刷新到存储层,并阻止所有其他写入操作,直到您解锁实例或集群。

To lock the database, use the fsync command to set the lock field to true:要锁定数据库,请使用fsync命令将lock字段设置为true

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" : Long(1),
"seeAlso" : "http://dochub.mongodb.org/core/fsynccommand",
"ok" : 1
}

When locked, write operations are blocked. Separate connections may continue read operations until the first attempt at a write operation, then they also wait until the sever or cluster is unlocked.锁定后,写入操作将被阻止。单独的连接可能会继续读取操作,直到第一次尝试写入操作,然后它们也会等待服务器或集群解锁。

Important

The fsync lock operation maintains a lock count.fsync锁操作保持锁计数。

To unlock a server or cluster for writes, the lock count must be zero. That is, for the given number of times you perform an fsync lock, you must issue a corresponding number of unlock operations to unlock the server or cluster for writes.要解锁服务器或群集进行写入,锁计数必须为零。也就是说,对于执行fsync锁的给定次数,您必须发出相应数量的解锁操作,以解锁服务器或集群进行写入。

Fsync UnlockFsync解锁

To unlock a server of cluster, use the fsyncUnlock command:要解锁群集的服务器,请使用fsyncUnlock命令:

db.adminCommand( { fsyncUnlock: 1 } )

Repeat this command as many times as needed to reduce the lock count to zero. Once the lock count reaches zero, the server or cluster can resume writes.根据需要重复此命令多次,以将锁计数减少到零。一旦锁计数达到零,服务器或集群就可以恢复写入。

Check Lock Status检查锁定状态

To check the state of the fsync lock, use db.currentOp(). Use the following JavaScript function in the shell to test if the server or cluster is currently locked:要检查fsync锁的状态,请使用db.currentOp()。在shell中使用以下JavaScript函数来测试服务器或集群当前是否已锁定:

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 server or cluster is currently locked and false if the server or cluster is not locked.如果服务器或群集当前已锁定,则此函数将返回true,如果服务器或群集中未锁定,则返回false