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 大小有几个类似的指令

sh1: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)


sh2:PRIMARY> rs.printReplicationInfo()
sh2:PRIMARY> db.printRe