mongodb给我们提供了fsync+lock机制把数据暴力的刷到硬盘上

简介:

能不能把数据暴力的刷到硬盘上,当然是可以的,mongodb给我们提供了fsync+lock机制就能满足我们提的需求。

fsync+lock首先会把缓冲区数据暴力刷入硬盘,然后给数据库一个写入锁,其他实例的写入操作全部被阻塞,直到fsync

+lock释放锁为止。

这里就不测试了。

 加锁:    db.runCommand({"fsync":1,"lock":1})

 释放锁: db.$cmd.unlock.findOne()

 

fsync

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.

The fsync command has the following syntax:

{ fsync: 1, async: <Boolean>, lock: <Boolean> } 

The fsync command has the following fields:

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.

Examples

Run Asynchronously

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().

Lock mongod Instance

NOTE

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 cpscptar) 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.

Unlock mongod Instance

To unlock the mongod, use db.fsyncUnlock():

db.fsyncUnlock();

见:https://docs.mongodb.com/manual/reference/command/fsync/













本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/6520521.html ,如需转载请自行联系原作者

相关文章
|
NoSQL 安全 MongoDB
【MongoDB深度揭秘】你的更新操作真的安全了吗?MongoDB fsync机制大起底,数据持久化不再是谜!
【8月更文挑战第24天】MongoDB是一款备受欢迎的NoSQL数据库,以其灵活的文档模型和强大的查询能力著称。处理关键业务数据时,数据持久化至关重要。本文深入探讨MongoDB的写入机制,特别是更新操作时的fsync行为。MongoDB先将数据更新至内存以提升性能,而非直接写入磁盘。fsync的作用是确保数据从内存同步到磁盘,但MongoDB并非每次更新后都立即执行fsync。通过设置不同的写入关注级别(如w:0、w:1和w:majority),可以平衡数据持久性和性能。
236 1
|
持续交付 C# 敏捷开发
“敏捷之道:揭秘WPF项目中的快速迭代与持续交付——从需求管理到自动化测试,打造高效开发流程的全方位指南”
【8月更文挑战第31天】敏捷开发是一种注重快速迭代和持续交付的软件开发方法,通过短周期开发提高产品质量并快速响应变化。本文通过问题解答形式,探讨在Windows Presentation Foundation(WPF)项目中应用敏捷开发的最佳实践,涵盖需求管理、版本控制、自动化测试及持续集成等方面,并通过具体示例代码展示其实施过程,帮助团队提升代码质量和开发效率。
279 0
|
缓存 NoSQL 关系型数据库
【MongoDB】MongoDB更新操作时是否立刻fsync到磁盘?
【4月更文挑战第2天】【MongoDB】MongoDB更新操作时是否立刻fsync到磁盘?
|
NoSQL Shell 数据库
|
7月前
|
NoSQL MongoDB 数据库
数据库数据恢复—MongoDB数据库数据恢复案例
MongoDB数据库数据恢复环境: 一台操作系统为Windows Server的虚拟机上部署MongoDB数据库。 MongoDB数据库故障: 工作人员在MongoDB服务仍然开启的情况下将MongoDB数据库文件拷贝到其他分区,数据复制完成后将MongoDB数据库原先所在的分区进行了格式化操作。 结果发现拷贝过去的数据无法使用。管理员又将数据拷贝回原始分区,MongoDB服务仍然无法使用,报错“Windows无法启动MongoDB服务(位于 本地计算机 上)错误1067:进程意外终止。”
|
7月前
|
缓存 NoSQL Linux
在CentOS 7系统中彻底移除MongoDB数据库的步骤
以上步骤完成后,MongoDB应该会从您的CentOS 7系统中被彻底移除。在执行上述操作前,请确保已经备份好所有重要数据以防丢失。这些步骤操作需要一些基本的Linux系统管理知识,若您对某一步骤不是非常清楚,请先进行必要的学习或咨询专业人士。在执行系统级操作时,推荐在实施前创建系统快照或备份,以便在出现问题时能够恢复到原先的状态。
661 79
|
7月前
|
存储 NoSQL MongoDB
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
332 8
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
|
6月前
|
运维 NoSQL 容灾
告别运维噩梦:手把手教你将自建 MongoDB 平滑迁移至云数据库
程序员为何逃离自建MongoDB?扩容困难、运维复杂、高可用性差成痛点。阿里云MongoDB提供分钟级扩容、自动诊断与高可用保障,助力企业高效运维、降本增效,实现数据库“无感运维”。

推荐镜像

更多