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 cp, scp, tar) 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数据更新时,WiredTiger存储引擎通过预写日志(Journal)机制先将更新写入日志文件,再通过检查点操作将日志中的操作刷新到数据文件,确保数据持久化和一致性。检查点定期创建,缩短恢复时间,并保证异常终止后可从上一个有效检查点恢复数据。视频讲解及图示详细说明了这一过程。
573 23
【赵渝强老师】MongoDB写入数据的过程
|
存储 NoSQL MongoDB
数据的存储--MongoDB文档存储(二)
数据的存储--MongoDB文档存储(二)
515 2
|
存储 JSON NoSQL
微服务——MongoDB的数据模型
MongoDB采用文档(document)作为最小存储单位,类似关系型数据库中的行,使用BSON(Binary-JSON)格式存储数据。BSON是JSON的二进制扩展,支持内嵌文档和数组,新增了如Date、BinData等特殊数据类型,具有轻量、高效、可遍历的特点,适合非结构化与结构化数据存储。其灵活性高,但空间利用率略低。BSON数据类型包括string、integer、boolean等基本类型及date、object id等扩展类型。
508 0
|
NoSQL MongoDB 数据库
使用NimoShake将数据从AWS DynamoDB迁移至阿里云MongoDB
使用NimoShake将数据从AWS DynamoDB迁移至阿里云MongoDB
|
存储 NoSQL 关系型数据库
数据的存储--MongoDB文档存储(一)
数据的存储--MongoDB文档存储(一)
1082 3
|
NoSQL 安全 MongoDB
【MongoDB深度揭秘】你的更新操作真的安全了吗?MongoDB fsync机制大起底,数据持久化不再是谜!
【8月更文挑战第24天】MongoDB是一款备受欢迎的NoSQL数据库,以其灵活的文档模型和强大的查询能力著称。处理关键业务数据时,数据持久化至关重要。本文深入探讨MongoDB的写入机制,特别是更新操作时的fsync行为。MongoDB先将数据更新至内存以提升性能,而非直接写入磁盘。fsync的作用是确保数据从内存同步到磁盘,但MongoDB并非每次更新后都立即执行fsync。通过设置不同的写入关注级别(如w:0、w:1和w:majority),可以平衡数据持久性和性能。
453 1
|
持续交付 C# 敏捷开发
“敏捷之道:揭秘WPF项目中的快速迭代与持续交付——从需求管理到自动化测试,打造高效开发流程的全方位指南”
【8月更文挑战第31天】敏捷开发是一种注重快速迭代和持续交付的软件开发方法,通过短周期开发提高产品质量并快速响应变化。本文通过问题解答形式,探讨在Windows Presentation Foundation(WPF)项目中应用敏捷开发的最佳实践,涵盖需求管理、版本控制、自动化测试及持续集成等方面,并通过具体示例代码展示其实施过程,帮助团队提升代码质量和开发效率。
464 0
|
NoSQL MongoDB 数据库
数据库数据恢复—MongoDB数据库数据恢复案例
MongoDB数据库数据恢复环境: 一台操作系统为Windows Server的虚拟机上部署MongoDB数据库。 MongoDB数据库故障: 工作人员在MongoDB服务仍然开启的情况下将MongoDB数据库文件拷贝到其他分区,数据复制完成后将MongoDB数据库原先所在的分区进行了格式化操作。 结果发现拷贝过去的数据无法使用。管理员又将数据拷贝回原始分区,MongoDB服务仍然无法使用,报错“Windows无法启动MongoDB服务(位于 本地计算机 上)错误1067:进程意外终止。”
|
缓存 NoSQL Linux
在CentOS 7系统中彻底移除MongoDB数据库的步骤
以上步骤完成后,MongoDB应该会从您的CentOS 7系统中被彻底移除。在执行上述操作前,请确保已经备份好所有重要数据以防丢失。这些步骤操作需要一些基本的Linux系统管理知识,若您对某一步骤不是非常清楚,请先进行必要的学习或咨询专业人士。在执行系统级操作时,推荐在实施前创建系统快照或备份,以便在出现问题时能够恢复到原先的状态。
1552 79
|
存储 NoSQL MongoDB
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
612 8
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉

推荐镜像

更多