定义
db.fsyncLock()将所有挂起的写入从存储层刷新到磁盘,并锁定服务器以防止任何其他写入,直到锁被释放。
从 MongoDB 7.0.2开始 (从6.0.11和5.0.22开始也可用)
db.fsyncLock()和db.fsyncUnlock()方法可以在mongos上运行以锁定和解锁分片集群。重要
mongosh 方法
This page documents a
mongoshmethod. This is not the documentation for database commands or language-specific drivers, such as Node.js.有关数据库命令,请参阅
fsync命令。如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。
Servers maintain an fsync lock count. The
fsyncLock()method increments the lock count while thefsyncUnlock()method decrements it. To unlock writes on a server or cluster, call thefsyncUnlock()method until the lock count reaches zero.db.fsyncLock()语法为:db.fsyncLock() 该操作返回一个包含以下字段的文档:
字段说明info有关操作状态的信息。
lockCount当前实例上的锁数量。
seeAlso链接至
fsync命令文档。ok状态代码。
db.fsyncLock()is an administrative command. Use this method to lock a server or cluster before backup operations.
兼容性
此方法可用于以下环境中托管的部署:
重要
MongoDB Atlas 集群不支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
行为
db.fsyncLock() ensures that the data files are safe to copy using low-level backup utilities such as cp, scp, or 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的数据文件可能会因日志同步或WiredTiger快照等操作而发生变化。虽然这对逻辑数据(例如客户端访问的数据),但某些备份实用程序可能会检测到这些更改并发出警告,或者失败并显示错误。有关MongoDB的更多信息 - 推荐的备份实用程序和过程,请参阅自管理部署的备份方法。
故障后的 fsync 锁
Fsync 锁在副本集或分片集群中的主节点上执行。
如果主节点因网络问题宕机或无法访问,集群会从可用的从节点中选举出新的主节点。如果带有 fsync 锁的主节点宕机,新的主节点不会保留 fsync 锁,并且可以处理写操作。备份操作过程中发生选举时,生成的备份可能不一致或无法使用。
要从主节点降级中恢复:
运行
db.fsyncUnlock()方法,直到锁计数达到零,释放所有节点上的锁。Issue the
db.fsyncLock()command to reestablish the fsync lock on the cluster.重新启动备份。
此外,fsync 锁是持久的。当旧的主服务器再次联机时,您需要运行 db.fsyncUnlock() 命令来释放节点上的锁。
例子
The following operation runs db.fsyncLock():
db.fsyncLock()
该操作将返回以下状态文档,其中包括 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:
{ "info" : "now locked against writes, use db.fsyncUnlock() to unlock", "lockCount" : Long(2), "seeAlso" : "http://dochub.mongodb.org/core/fsynccommand", "ok" : 1 }
要解锁实例以进行写入,必须运行 db.fsyncUnlock() 两次以将 lockCount 减少到 0。