Definition定义
db.fsyncLock()Flushes all pending writes from the storage layer to disk and locks the server to prevent any additional writes until the lock is released.清除存储层到磁盘的所有挂起的写入,并锁定服务器以防止任何额外的写入,直到锁被释放。Starting in MongoDB 7.1 (also available starting in 7.0.2, 6.0.11, and 5.0.22) the从MongoDB 7.1开始(从7.0.2、6.0.11和5.0.22开始也可用),db.fsyncLock()anddb.fsyncUnlock()methods can run onmongosto lock and unlock a sharded cluster.db.fsyncLock()和db.fsyncUnlock()方法可以在mongos上运行,以锁定和解锁分片集群。Important
mongosh
Method方法This page documents a本页记录了一种mongoshmethod. This is not the documentation for database commands or language-specific drivers, such as Node.js.mongosh方法。这不是数据库命令或特定语言驱动程序(如Node.js)的文档。For the database command, see the有关数据库命令,请参阅fsynccommand.fsync命令。For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档。Servers maintain an fsync lock count. The服务器维护fsyncLock()method increments the lock count while thefsyncUnlock()method decrements it.fsync锁计数。fsyncLock()方法递增锁计数,而fsyncUnlock()方法递减锁计数。To unlock writes on a server or cluster, call the要解锁服务器或集群上的写入,请调用fsyncUnlock()method until the lock count reaches zero.fsyncUnlock()方法,直到锁定计数达到零。db.fsyncLock()has the syntax:具有以下语法:db.fsyncLock()The operation returns a document with the following fields:该操作返回一个包含以下字段的文档:Field字段Description描述infoInformation on the status of the operation.有关操作状态的信息。lockCountNumber of locks currently on the instance.实例上当前的锁数。seeAlsoLink to the指向fsynccommand documentation.fsync命令文档的链接。okThe status code.状态代码。db.fsyncLock()is an administrative command. Use this method to lock a server or cluster before backup operations.这是一个行政命令。使用此方法在备份操作之前锁定服务器或群集。
Compatibility兼容性
This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:
Important
This command is not supported in MongoDB Atlas clusters. MongoDB Atlas集群不支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令。
- 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的源代码可用、免费使用和自我管理版本
Behavior行为
db.fsyncLock() ensures that the data files are safe to copy using low-level backup utilities such as 确保使用cp, scp, or tar. 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 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推荐的备份实用程序和过程的更多信息,请参阅自我管理部署的备份方法。
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. 如果primary因网络问题而停机或无法访问,集群将从可用的次服务器中选择一个新的primary。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:要从主故障中恢复:
Run the运行db.fsyncUnlock()method until the lock count reaches zero to release the lock on all nodes.db.fsyncUnlock()方法,直到锁计数达到零,以释放所有节点上的锁。Issue the发出db.fsyncLock()command to reestablish the fsync lock on the cluster.db.fsyncLock()命令以在集群上重新建立fsync锁。Restart the backup.重新启动备份。
Additionally, fsync locks are persistent. When the old primary comes online again, you need to run the 此外,db.fsyncUnlock() command to release the lock on the node.fsync锁是持久的。当旧的主节点再次联机时,您需要运行db.fsyncUnlock()命令来释放节点上的锁。
Example示例
The following operation runs 以下操作运行db.fsyncLock():db.fsyncLock():
db.fsyncLock()
The operation returns the following status document that includes 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
}
If you run 如果再次运行db.fsyncLock() again, the operation increments the lockCount:db.fsyncLock(),该操作将递增lockCount:
{
"info" : "now locked against writes, use db.fsyncUnlock() to unlock",
"lockCount" : Long(2),
"seeAlso" : "http://dochub.mongodb.org/core/fsynccommand",
"ok" : 1
}
To unlock the instance for writes, you must run 要解锁实例进行写入,您必须运行db.fsyncUnlock() twice to reduce the lockCount to 0.db.fsyncUnlock()两次以将lockCount减小到0。