replSetResizeOplog

On this page本页内容

Definition定义

replSetResizeOplog

New in version 4.4.在版本4.4中新增 replSetResizeOplog also supports specifying the minimum number of hours to preserve an oplog entry.还支持指定保留oplog条目的最小小时数。

Changed in version 5.0.在版本5.0中更改

To set the replSetOplog size in mongosh, use the Double() constructor.要在mongosh中设置replSetOplog大小,请使用Double()构造函数。

replSetResizeOplog enables you to resize the oplog or its minimum retention period dynamically without restarting the mongod process.允许您动态调整oplog或其最小保留期,而无需重新启动mongod进程。

You must run this command against the admin database.您必须对admin数据库运行此命令。

The command has the following form:命令的格式如下:

db.adminCommand(
  {
    replSetResizeOplog: <int>,
    size: <double>,
    minRetentionHours: <double>
  }
)

replSetResizeOplog takes the following fields:采用以下字段:

Field字段Type类型Description描述
replSetResizeOplogintSet to 1.设置为1
sizedouble

The maximum size of the oplog in megabytes.oplog的最大大小(MB)。

  • The minimum size you can specify is 990 megabytes.您可以指定的最小大小为990 MB。
  • The maximum size you can specify is 1 petabytes.您可以指定的最大大小为1 PB。
  • Explicitly cast the size as a double in mongosh with Double(). 使用double()mongosh中显式地将size转换为double()See Change the Maximum Oplog Size.请参阅更改最大Oplog大小
minRetentionHoursdouble

Optional. 可选。The minimum number of hours to preserve an oplog entry, where decimal values represent the fractions of an hour. 保留oplog条目的最小小时数,其中十进制值表示小时的分数。For example, a value of 1.5 represents one hour and thirty minutes.例如,值1.5表示一小时三十分钟。

The value must be greater than or equal to 0. 该值必须大于或等于0A value of 0 indicates that the mongod should truncate the oplog starting with the oldest entries to maintain the configured maximum oplog size.0表示mongod应该从最旧的条目开始截断oplog,以保持配置的最大oplog大小。

A mongod configured with minRetentionHours only removes an oplog entry if:配置了minRetentionHoursmongod仅在以下情况下删除oplog条目:

  • The oplog has reached the maximum configured size, andoplog已达到配置的最大大小,并且
  • The oplog entry is older that the configured number of hours based on the host system clock.oplog条目早于基于主机系统时钟的配置小时数。

To check the currently configured minimum oplog retention period, see the oplogTruncation.minRetentionHours in the output of the serverStatus command.要检查当前配置的最小oplog保留期,请参阅serverStatus命令输出中的oplogTruncation.minRetentionHours

New in version 4.4.在版本4.4中新增

Behavior行为

You can only use replSetResizeOplog on mongod instances running with the Wired Tiger storage engine.您只能在使用Wired Tiger存储引擎运行的mongod实例上使用replSetResizeOplog

See the Change the Size of the Oplog tutorial for a procedure on using replSetResizeOplog command to resize the oplog.有关使用replSetResizeOplog命令调整Oplog大小的过程,请参阅更改Oplog大小教程。

Starting in MongoDB 4.0, the oplog can grow past its configured size limit to avoid deleting the majority commit point.从MongoDB 4.0开始,oplog可以超过其配置的大小限制,以避免删除多数提交点

Starting in MongoDB 4.0, MongoDB forbids dropping the local.oplog.rs collection. 从MongoDB 4.0开始,MongoDB禁止删除local.oplog.rs集合。For more information on this restriction, see Oplog Collection Behavior.有关此限制的详细信息,请参阅Oplog集合行为

replSetResizeOplog overrides the maximum oplog size or minimum oplog retention period set at startup by:通过以下方式覆盖启动时设置的最大oplog大小或最小oplog保留期:

The new oplog size persists after a server restart, unless you use:新的oplog大小在服务器重新启动后仍然存在,除非您使用:

Important重要

Reducing the maximum oplog size results in truncation of the oldest oplog entries until the oplog reaches the new configured size.减小最大oplog大小会导致截断最旧的oplog条目,直到oplog达到新的配置大小。

Similarly, reducing the minimum oplog retention period (new in 4.4) results in truncation of oplog entries older that the specified period if the oplog has exceeded the maximum configured size.类似地,如果oplog已超过最大配置大小,则缩短最小oplog保留期(4.4中的新增)将导致截断早于指定时间段的oplog条目。

Oplog truncation due to reduced oplog size or retention period can result in unexpected behavior from clients still reading those oplog entries, including:由于Oplog大小或保留期减少而导致的Oplog截断可能会导致仍在读取这些Oplog条目的客户端出现意外行为,包括:

Minimum Oplog Retention Period最短Oplog保留期

A mongod has the following behavior when configured with a minimum oplog retention period (New in 4.4):当配置了最小oplog保留期时,mongod具有以下行为(4.4中的新增):

  • The oplog can grow without constraint so as to retain oplog entries for the configured number of hours. oplog可以无限制地增长,以便在配置的小时数内保留oplog条目。This may result in reduction or exhaustion of system disk space due to a combination of high write volume and large retention period.这可能会由于高写入量和大保留期的组合而导致系统磁盘空间的减少或耗尽。
  • If the oplog grows beyond its maximum size, the mongod may continue to hold that disk space even if the oplog returns to its maximum size or is configured for a smaller maximum size. 如果oplog增长超过其最大大小,那么mongod可能会继续保留该磁盘空间,即使oplog恢复到其最大大小或配置为较小的最大大小。See Reducing Oplog Size Does Not Immediately Return Disk Space.请参阅减小Oplog大小不会立即返回磁盘空间
  • The mongod compares the system wall clock to an oplog entries creation wall clock time when enforcing oplog entry retention. 当强制执行oplog条目保留时,mongod将系统时钟与oplog条目创建时钟时间进行比较。Clock drift between cluster components may result in unexpected oplog retention behavior. 集群组件之间的时钟漂移可能会导致意外的oplog保留行为。See Clock Synchronization for more information on clock synchronization across cluster members.有关跨集群成员的时钟同步的详细信息,请参阅时钟同步

replSetResizeOplog Does Not Replicate To Other Members不复制到其他成员

Changing the oplog size or minimum oplog retention period (new in 4.4) of a given replica set member with replSetResizeOplog does not change the oplog size of any other member in the replica set. 使用replSetResizeOplog更改给定副本集成员的oplog大小或最小oplog保留期(4.4中新增)不会更改副本集中任何其他成员的oplog大小。You must run replSetResizeOplog on each replica set member in your cluster to change the oplog size or minimum retention period for all members.必须在群集中的每个副本集成员上运行replSetResizeOplog,才能更改所有成员的oplog大小或最小保留期。

Reducing Oplog Size Does Not Immediately Return Disk Space减小Oplog大小不会立即返回磁盘空间

Reducing the oplog size does not immediately reclaim that disk space. 减小oplog大小不会立即回收磁盘空间。This includes oplog size reduction due to truncation of oplog events older than of the minimum oplog retention period (New in 4.4).这包括由于截断比最小oplog保留期早的oplog事件而导致的oplog大小减小(4.4中的新增)。

To immediately free unused disk space after reducing the oplog size, run compact against the oplog.rs collection in the local database during a maintenance period. 要在减少oplog大小后立即释放未使用的磁盘空间,请在维护期间对local数据库中的oplog.rs集合运行compactcompact blocks all operations on the database it runs against. compact阻止对其运行的数据库的所有操作。Running compact against oplog.rs therefore prevents oplog synchronization. 因此,针对oplog.rs运行compact会阻止oplog同步。For a procedure on resizing the oplog and compacting oplog.rs, see Change the Size of the Oplog.有关调整oplog大小和压缩oplog.rs的过程,请参阅更改oplog大小

Resource Locking资源锁定

Changed in version 4.2.2.在版本4.2.2中更改

  • For MongoDB 4.2.2 and later, replSetResizeOplog takes an exclusive (W) lock on the oplog and blocks other operations on the collection until it finishes.对于MongoDB 4.2.2及更高版本,replSetResizeOplog获取oplog的独占(W)锁,并阻止集合上的其他操作,直到其完成。
  • For MongoDB 4.2.1 and earlier, replSetResizeOplog takes a global exclusive (W) lock and blocks all other operations until it finishes.对于MongoDB 4.2.1和更早版本,replSetResizeOplog使用全局独占(W)锁,并阻止所有其他操作,直到完成。

For more information on locking in MongoDB, see FAQ: Concurrency.有关MongoDB中锁定的更多信息,请参阅常见问题解答:并发

Examples示例

Change the Maximum Oplog Size更改最大Oplog大小

Use the db.collection.stats() mongosh method to display the current maximum oplog size, maxSize, in megabytes. 使用db.collection.stats()mongosh方法以兆字节为单位显示当前最大oplog大小maxSizeFor example:

db.getSiblingDB("local").oplog.rs.stats(1024*1024).maxSize

The above command returns the oplog size of this member in megabytes:上述命令返回此成员的oplog大小(MB):

990

The following command uses replSetResizeOplog to change the oplog size of this member to 16384 megabytes:以下命令使用replSetResizeOplog将此成员的oplog大小更改为16384 MB:

db.adminCommand({ "replSetResizeOplog": 1, size: Double(16384)})

To verify the new oplog size, rerun the db.collection.stats() method:要验证新的oplog大小,请重新运行db.collection.stats()方法:

db.getSiblingDB("local").oplog.rs.stats(1024*1024).maxSize

The above command returns:上述命令返回:

"maxSize": NumberLong("16834")
Warning警告

Reducing the size of the oplog in a node removes data from it. 减小节点中oplog的大小会从中删除数据。This may cause replica members syncing with that node to become stale. 这可能会导致与该节点同步的副本成员过时。To resync those members, see Resync a Member of a Replica Set.要重新同步这些成员,请参阅重新同步副本集的成员

Change the Minimum Oplog Retention Period更改最小Oplog保留期

  1. Connect mongosh to the mongod replica set member.mongosh连接到mongod复制集成员。
  2. Optional. 可选。Use the db.serverStatus() command to verify the current minimum oplog retention value as oplogTruncation.minRetentionHours:使用db.serverStatus()命令将当前最小oplog保留值验证为oplogTruncation.minRetentionHours

    db.getSiblingDB("admin").serverStatus().oplogTruncation.minRetentionHours

    The command returns the currently configured minimum oplog retention period for the mongod. 该命令返回当前为mongod配置的最小oplog保留期。For example:例如:

    1.5

    If the mongod has no minimum oplog retention period, the operation returns an empty result.如果mongod没有最小的oplog保留期,则操作返回空结果。

  3. Use the replSetResizeOplog command to modify the configured minimum oplog retention period. 使用replSetResizeOplog命令修改配置的最小oplog保留期。For example, the following sets the minimum oplog retention period to 2 hours:例如,以下命令将oplog的最短保留时间设置为2小时:

    db.adminCommand({
      "replSetResizeOplog" : 1,
      "minRetentionHours" : 2
    })
←  replSetReconfigreplSetStepDown →