Docs HomeMongoDB Manual

Change the Size of the Oplog更改操作日志的大小

Warning

In MongoDB versions 3.4 and earlier, the oplog was resized by dropping and recreating the local.oplog.rs collection.在MongoDB 3.4及更早版本中,通过删除并重新创建local.oplog.rs集合来调整oplog的大小。

In MongoDB versions 3.6 and later, use the replSetResizeOplog command to resize the oplog as shown in this tutorial.在MongoDB 3.6及更高版本中,使用replSetResizeOplog命令调整操作日志的大小,如本教程所示。

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.有关此限制的详细信息,请参阅操作日志集合行为

This procedure changes the size of the oplog [1] on each member of a replica set using the replSetResizeOplog command, starting with the secondary members before proceeding to the primary.此过程使用replSetResizeOplog命令更改副本集每个成员上的oplog[1]的大小,从secondary成员开始,然后继续到primary

Perform these steps on each secondary replica set member first. Once you have changed the oplog size for all secondary members, perform these steps on the primary.首先在每个secondary复制副本集成员上执行这些步骤。更改所有辅助成员的操作日志大小后,请在primary上执行以下步骤。

A. Connect to the replica set member连接到复制副本集成员

Connect to the replica set member using mongosh:使用mongosh连接到复制集成员:

mongosh --host <hostname>:<port>
Note

If the replica set enforces authentication, you must authenticate as a user with privileges to modify the local database, such as the clusterManager or clusterAdmin role.如果副本集强制进行身份验证,则必须以具有修改local数据库权限的用户身份进行身份验证(例如clusterManagerclusterAdmin角色)。

B. (Optional) Verify the current size of the oplog(可选)验证操作日志的当前大小

To view the current size of the oplog, switch to the local database and run db.collection.stats() against the oplog.rs collection. 要查看oplog的当前大小,请切换到local数据库,并对oplog.rs集合运行db.collection.stats()stats() displays the oplog size as maxSize.将oplog大小显示为maxSize

use local
db.oplog.rs.stats().maxSize

The maxSize field displays the collection size in bytes.maxSize字段以字节为单位显示集合大小。

C. Change the oplog size of the replica set member更改副本集成员的操作日志大小

Resize the oplog with the replSetResizeOplog command. 使用replSetResizeOplog命令调整操作日志的大小。The size is a double and must be greater than 990 megabytes. sizedouble,并且必须大于990兆字节。To explicitly cast the oplog size in mongosh, use the Double() constructor.要在mongosh中显式强制转换oplogsize,请使用Double()构造函数。

The following operation changes the oplog size of the replica set member to 16 gigabytes, or 16000 megabytes.以下操作将副本集成员的操作日志大小更改为16 GB或16000 MB。

db.adminCommand({replSetResizeOplog: 1, size: Double(16000)})
[1] The oplog can grow past its configured size limit to avoid deleting the majority commit point.oplog可以超过其配置的大小限制,以避免删除多数提交点

D. (Optional) Compact oplog.rs to reclaim disk space(可选)压缩oplog.rs以回收磁盘空间

Reducing the size of the oplog does not automatically reclaim the disk space allocated to the original oplog size. 减小操作日志的大小不会自动回收分配给原始操作日志大小的磁盘空间。You must run compact against the oplog.rs collection in the local database to reclaim disk space. 必须对本地数据库中的oplog.rs集合运行compact以回收磁盘空间。There are no benefits to running compact on the oplog.rs collection after increasing the oplog size.增加oplog大小后,在oplog.rs集合上运行compact没有任何好处。

Important

Starting in MongoDB v4.4, a replica set member can replicate oplog entries while the compact operation is ongoing. 从MongoDB v4.4开始,副本集成员可以在执行compact(压缩)操作时复制oplog条目。Previously, oplog replication would be paused during compaction. Because of this, it was recommended that oplog compaction only be performed during maintenance windows, where writes could be minimized or stopped. 以前,oplog复制会在压缩过程中暂停。因此,建议仅在维护窗口期间执行oplog压缩,在维护窗口中可以最小化或停止写入。In MongoDB 4.4 and later, it is no longer necessary to limit compaction operations on the oplog to maintenance windows, as oplog replication can continue as normal during compaction.在MongoDB 4.4及更高版本中,不再需要将oplog上的压缩操作限制在维护窗口,因为在压缩过程中,oplog复制可以正常进行。

Do not run compact against the primary replica set member. 不要对primary复制集成员运行compactConnect a mongo shell directly to the primary (not the replica set) and run rs.stepDown(). 将mongoshell直接连接到主(而不是副本集),然后运行rs.stepDown()If successful, the primary steps down. From the mongo shell, run the compact command on the now-secondary member.如果成功,主进程将退出。从mongoshell中,在现在的次要成员上运行compact命令。

The following operation runs the compact command against the oplog.rs collection:以下操作针对oplog.rs集合运行compact命令:

use local
db.runCommand({ "compact" : "oplog.rs" } )

For clusters enforcing authentication, authenticate as a user with the compact privilege action on the local database and the oplog.rs collection. 对于强制身份验证的集群,使用local数据库和oplog.rs集合上的compact权限操作以用户身份进行身份验证。For complete documentation on compact authentication requirements, see compact Required Privileges.有关compact身份验证要求的完整文档,请参阅compact所需要的权限