当前位置:网站首页>Journal日志与oplog日志的区别
Journal日志与oplog日志的区别
2022-07-17 13:18:00 【我要出家当道士】
目录
在学习mongoDB基础知识的时候,可能有些概念会混淆,比如 Journal日志与oplog日志有啥区别。
下面的内容主要摘选自《MongoDB原理与实战》张友东著
一、概念的不同
1、Journal日志
Journal日志是MongoDB 存储引擎层的机制, ⽬前 MongoDB 所有的存储引擎都⽀持配置Journal,自2.0开始默认开启。在MongoDB中所有的数据写⼊、读取最终都是调存储引擎层的接口实现的, 而 Journal 日志是用来记录写操作的,以实现数据故障恢复和持久化数据的目的。
以wiredtiger 为例,如果不配置 Journal,写⼊ wiredtiger 的数据,并不会⽴即持久化存储; ⽽是每分钟会做⼀次全量的checkpoint(storage.syncPeriodSecs 配置项, 默认为1分钟) ,将所有的数据持久化。如果中间出现宕机, 那么数据只能恢复到最近的⼀次checkpoint,这样最多可能丢掉最近1分钟的数据。而当开启Journal日志后,写入数据库的每个操作都会被记录,当出现宕机时,在恢复至最近的checkpoint点后,可以继续重放后续的操作日志,恢复数据。
2、Oplog日志
Oplog日志默认在mongoDB的副本集模式下开启。Oplog日志主要用于Primary节点和Secondary节点的数据同步,Oplog记录了Primary节点的所有的写操作,Secondary节点通过定期拷贝Primary节点的Oplog实现增量备份。
自mongoDB3.6新增的changestream机制也是基于oplog实现的。ChangeStreams 在 Oplog 之上包裹一层应用,对外提供一个 API 接口,将数据进行实时推送。

图选自《mongodb入门实战》
二、数据范围不同
以mongoDB 副本集中的一次插入数据为例,在数据层面分为下面三个步骤:
1. 将⽂档数据写⼊对应的集合
2. 更新集合的所有索引信息
3. 写⼊⼀条oplog⽤于同步
上⾯3个修改操作,需要确保要么都成功,要么都失败。MongoDB 在写⼊数据时, 会将上述3个操作放到⼀个 wiredtiger 的事务⾥, 确保「原⼦性」 。如下所示:
// 开始事务
beginTransaction();
// 插入数据的三个步骤
writeDataToColleciton();
writeCollectionIndex();
writeOplog();
//提交事务
commitTransaction();执行顺序如下图所示:

wiredtiger 提交事务时, 会将所有修改操作应⽤, 并将上述3个操作写⼊到⼀条 journal 操作日志里; 后台会周期性的checkpoint, 将修改持久化, 并移除无用的journal。从数据布局看, oplog 与 journal 的关系如下所示:

边栏推荐
- Transplant Wu Enda's deep learning 01 machine learning and neural network second week neural network basic programming homework elective homework to pycharm
- 因果学习将开启下一代AI浪潮?九章云极DataCanvas正式发布YLearn因果学习开源项目
- Job: enter an odd number of 1-100
- win10开始键点击无响应
- 空天地海一体化网络体系架构与网络切片技术
- 发明闪存能赚多少钱?这是一个日本的狗血故事
- 使用tesseract.js-offline识别图片文字记录
- CPU负载与CPU使用率之区别
- R language uses the aggregate function of epidisplay package to divide numerical variables into different subsets based on factor variables, calculate the summary statistics of each subset, and set na
- Common collection properties
猜你喜欢
随机推荐
[acwing] 60th weekly match b- 4495 Array operation
unity3d中的旋转
C # treeview tree structure recursive processing (enterprise group type hierarchical tree display)
Win10 start key click no response
知其然,而知其所以然,JS 对象创建与继承
High number_ Chapter 1 space analytic geometry and vector algebra__ Distance from point to plane
vSphere 下借助 vDS 或 NSX 做端口镜像的方法总结
Design and Simulation of intelligent storage cabinet control system
Find balanced binary tree
高数__方程与函数的关系
Aike AI frontier promotion (7.17)
vulnhub inclusiveness: 1
Know what it is, and know why, JS object creation and inheritance
【华为云IoT】读书笔记之《万物互联:物联网核心技术与安全》第3章(下)
修改Jupyter默认路径看这篇!
双向NAT技术
Pytorch手动实现多层感知机
C serialport configuration and attribute understanding
Vérification logique complexe personnalisée lors de l'ajout et de la modification - 2022 nouvel élément
Virtual CPU and memory in yarn (CDH)





![[Niuke swipe questions] / *c language realizes left-hand rotation of strings*/](/img/74/681975d85a671b4f75f2b392264105.png)



