Database Manual / Reference / Database Commands / Replication

replSetResizeOplog (database command数据库命令)

Definition定义

replSetResizeOplog

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数据库运行此命令。

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

Important

This command is not supported in MongoDB Atlas clusters. MongoDB Atlas集群不支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Note

To resize the oplog in Atlas, see Set Minimum Oplog Window.要调整Atlas中oplog的大小,请参阅设置最小oplog窗口

Syntax语法

The command has the following form:该命令具有以下形式:

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

Command Fields命令字段

The command takes the following fields:该命令包含以下字段:

Field字段Type类型Description描述
replSetResizeOplogintSet to 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(). See Change the Maximum Oplog Size.使用double()mongosh中将大小显式转换为double。请参阅更改最大操作日志大小
minRetentionHoursdouble双精度浮点数

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

The value must be greater than or equal to 0. A value of 0 indicates that the mongod should truncate the oplog starting with the oldest entries to maintain the configured maximum oplog size.该值必须大于或等于0。值为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.oplogMinRetentionHours in the output of the serverStatus command.要检查当前配置的最短oplog保留期,请参阅serverStatus命令输出中的oplogTruncation.oplogMinRetentionHours

Behavior行为

You can use replSetResizeOplog on mongod instances running with the Wired Tiger storage engine or the in-memory storage engine.您可以在运行Wired Tiger存储引擎内存存储引擎mongod实例上使用replSetResizeOplog

See the Change the Oplog Size of Self-Managed Replica Set Members tutorial for a procedure on using replSetResizeOplog command to resize the oplog.有关使用replSetResizeOplog命令调整Oplog大小的过程,请参阅更改自我管理副本集成员的Oplog大小教程。

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

You cannot drop the local.oplog.rs collection. For more information on this restriction, see Oplog Collection Behavior.您无法删除local.oplog.rs集合。有关此限制的更多信息,请参阅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 results in truncation of oplog entries older that the specified period if the oplog has exceeded the maximum configured size.同样,如果oplog超过了配置的最大大小,缩短oplog的最小保留期会导致比指定时间段更早的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条目的客户端出现意外行为,包括:

  • Open change streams may become invalidated开放的变更流可能会失效
  • Secondaries which have not replicated those oplog entries may require resynchronization.未复制这些oplog条目的借调人员可能需要重新同步
  • Backups using mongodump with --oplog against the member may not capture entries prior to truncation.对成员使用mongodump--oplog的备份可能无法在截断之前捕获条目。

Minimum Oplog Retention Period最短操作日志保留期

A mongod has the following behavior when configured with a minimum oplog retention period:当配置了最小oplog保留期时,mongod具有以下行为:

  • The oplog can grow without constraint so as to retain oplog entries for the configured number of hours. This may result in reduction or exhaustion of system disk space due to a combination of high write volume and large retention period.oplog可以无约束地增长,以便在配置的小时数内保留oplog条目。由于写入量大和保留期长,这可能会导致系统磁盘空间减少或耗尽。
  • 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增长超过其最大大小,即使oplog恢复到最大大小或配置为较小的最大大小,mongod也可能继续保留该磁盘空间。See Reducing Oplog Size Does Not Immediately Return Disk Space.请参阅减小操作日志大小不会立即返回磁盘空间
  • 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 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保留期不会更改副本集中任何其他成员的oplo大小。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减小操作日志大小不会立即返回磁盘空间

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

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. Running compact against oplog.rs therefore prevents oplog synchronization. compact会阻止它所运行的数据库上的所有操作。因此,对oplog运行compact会阻止oplog同步。For a procedure on resizing the oplog and compacting oplog.rs, see Change the Oplog Size of Self-Managed Replica Set Members.有关调整oplog大小和压缩oplog.rs的过程,请参阅更改自我管理副本集成员的oplog大小

Resource Locking资源锁定

replSetResizeOplog takes an exclusive (W) lock on the oplog and blocks other operations on the collection until it finishes.replSetResizeOplogoplog进行独占(W)锁,并阻止对集合的其他操作,直到它完成。

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

Examples示例

Change the Maximum Oplog Size更改最大操作日志大小

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

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": Long("16834")

Warning

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

Change the Minimum Oplog Retention Period更改最短操作日志保留期

  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.oplogMinRetentionHours:可选。使用db.serverStatus()命令将当前最小oplog保留值验证为oplogTruncation.oplogMinRetentionHours

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

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

    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. For example, the following sets the minimum oplog retention period to 2 hours:使用replSetResizeOplog命令修改配置的最短oplog保留期。例如,以下将oplog的最短保留期设置为2小时:

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