温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MongoDB 更改 Oplog Size 大小

发布时间:2020-07-07 14:19:15 来源:网络 阅读:1732 作者:Professor哥 栏目:MongoDB数据库

MongoDB 的副本集同步中 Oplog 占了非常关键的作用。


Oplog Size 可不可以随意更改?

https://docs.mongodb.com/manual/reference/configuration-options/#replication.oplogSizeMB
官方配置文档说明

replication.oplogSizeMB
	Type: integer

	The maximum size in megabytes for the replication operation log (i.e., the oplog).
	Starting in MongoDB 4.0, the oplog can grow past its configured size limit to avoid deleting the majority commit point.
	By default, the mongod process creates an oplog based on the maximum amount of space available. For 64-bit systems, the oplog is typically 5% of available disk space.
	Once the mongod has created the oplog for the first time, changing the replication.oplogSizeMB option will not affect the size of the oplog.
	To change the oplog size of a running replica set member, use the replSetResizeOplog administrative command. replSetResizeOplog enables you to resize the oplog dynamically without restarting the mongod process.
	
	See Oplog Size for more information.
	The replication.oplogSizeMB setting is available only for mongod.

其中,默认oplog 大小如无配置指定,则默认为当前磁盘空间的 5% 在 64位操作系统上。

一次生成 oplog 空间则终生有效,后续通过更改 mongod.conf 配置文件并重启服务,均不生效。

在 MongoDB 3.6+ 版本中新增可以动态调整副本集 Oplog Size 的指令,无需重启 mongod 服务。


查看当前 mongod 服务中分配的 Oplog Size 大小

查看当前已经分配的 oplog size 大小有几个类似的指令

sh2:PRIMARY> rs.printReplicationInfo()
configured oplog size:   12958.975341796875MB
log length start to end: 938secs (0.26hrs)
oplog first event time:  Thu Apr 11 2019 15:03:05 GMT+0800 (CST)
oplog last event time:   Thu Apr 11 2019 15:18:43 GMT+0800 (CST)
now:                     Thu Apr 11 2019 15:18:43 GMT+0800 (CST)


sh3:PRIMARY> rs.printReplicationInfo()
sh3:PRIMARY> db.printReplicationInfo()
sh3:PRIMARY> db.getReplicationInfo()
{
        "logSizeMB" : 102400,
        "usedMB" : 905.03,
        "timeDiff" : 4704,
        "timeDiffHours" : 1.31,
        "tFirst" : "Thu Apr 11 2019 13:54:30 GMT+0800 (CST)",
        "tLast" : "Thu Apr 11 2019 15:12:54 GMT+0800 (CST)",
        "now" : "Thu Apr 11 2019 15:12:54 GMT+0800 (CST)"
}

logSizeMB / configured oplog size当前已配置的空间大小,单位 MB
usedMB: 当前已使用空间大小,单位 MB。
tFirst / oplog first event time: 当前 oplog 最开始的记录时间。
tLast / oplog last event time: 当前 oplog 最新的更改记录时间。
timeDiff / log length start to end: 当前mongod中当前更改记录时间 - 最开始记录时间,单位 秒。
timeDiffHours / log length start to end: 当前mongod中当前更改记录时间 - 最开始记录时间,单位 小时。


规划需要更改的 oplog size 大小

根据业务场景不同,在高峰期获取 mongod 实例的写入量

sh2:PRIMARY> rs.printReplicationInfo()
configured oplog size:   12958.975341796875MB
log length start to end: 938secs (0.26hrs)
oplog first event time:  Thu Apr 11 2019 15:03:05 GMT+0800 (CST)
oplog last event time:   Thu Apr 11 2019 15:18:43 GMT+0800 (CST)
now:                     Thu Apr 11 2019 15:18:43 GMT+0800 (CST)

如上面所示,项目大多都是 insert、update 等更新操作,所以 12G 的 oplog 空间,在业务高峰只能够存储最近 900 秒的变更操作日志。

如果需要使 oplog 保留更长的周期,那么需要合理规划更大的 oplog.rs 空间大小。


更改 oplog size 大小
概述

oplog在内部作为上限集合存在,因此您无法在正常操作过程中修改其大小。
在大多数情况下,默认的oplog大小是可接受的大小;
但是,在某些情况下,您可能需要更大或更小的oplog。
例如,如果应用程序在短时间内执行大量多次更新或删除,则可能需要更改oplog大小。

为了修改oplog大小,我们需要以此为复制集中的每个节点进行维护手动操作。该过程需要:停止 mongod 进程,并以非单节点方式启动,修改oplog大小,再重启该节点。

[重要]
请确保我们从副本集的从节点(Secondary)开始维护,并最后维护主节点(Primary)。

流程
  • 以单节点模式重启节点

    在 Primary 节点上可以使用 rs.stepDown() 指令来强制手动切换为Secondary 节点。

  • 并保留就的 oplog 的条目作为查询条件,清空 oplog.rs 集合并重新建立指定大小的 oplog.rs 集合。

  • 以副本集模式启动 mongod 实例。


1. 首先在 secondary 节点上关闭 mongod 实例。比如通过 db.shutdownServer() 命令来关闭:

> db.shutdownServer()

在其他端口上以单节点模式(不包含 --replSet 参数)重新启动 mongod 实例。命令如下:

# sudo -u mongod mongod --port 37017 --dbpath /var/lib/mongo

2. 复制现有 Oplog (可选)
我们可以选择备份现有结点的 oplog 来以防万一,命令如下:

mongodump --port 37017 --db local --collection 'oplog.rs'

3. 以新的大小和 Seed Entry 重建 oplog
保存 oplog 中最新的条目。例如,连接进入 mongo Shell,并通过如下命令进入 local 数据库:

> use local

在 mongo Shell 窗口中,我们也可以使用如下命令来设置 db:

> db = db.getSiblingDB('local')

确保 local.temp 集合为空的:

> db.temp.drop()

使用 natural order 排序来找到 oplog 中最后一条数据,并通过 db.collection.save() 命令插入 local.temp 集合中:

> db.temp.save(db.oplog.rs.find({}, {ts: 1, h: 1}).sort({$natural: -1}).limit(1).next())

检验 local.temp 集合中保存的最后一条 oplog 数据:

> db.temp.find()

4. 删除已存在老的 Oplog 集合
通过如下命令在 local 库中删除老的oplog.rs 集合:

> db = db.getSiblingDB('local')
> db.oplog.rs.drop()

结果会返回 true

5. 建立新的 Oplog 集合大小
通过 create 命令来建立新的 oplog(新的大小)。指定 size(单位是 bytes)。下面的命令会建立一个大小为 20 * 1024 * 1024 * 1024 也就是 20G 的 oplog 集合:

> db.runCommand({create: "oplog.rs", capped: true, size: (20 * 1024 * 1024 * 1024)})

命令执行成功后会返回如下内容:

{ "ok" : 1 }

6. 将之前保存在 local.temp 集合中的老的最后一条 oplog 数据插入到新的 oplog 集合中。 例如:

> db.oplog.rs.save( db.temp.findOne() )

通过如下命令来确认:

> db.oplog.rs.find()

7. 结束,以副本集模式重启 mongod 实例。

> db = db.getSiblingDB('admin')
> db.shutdownServer()
# service mongod restart

该复制集将会恢复并会在其成为主节点之前 “catch up” 数据。

8. 重复以上操作在所有我们希望修改 oplog 大小的机器上。最后再在主节点上进行该操作。


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI