Change the Size of the Oplog更改Oplog的大小

On this page本页内容

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命令调整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集合行为

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. 首先对每个secondary副本集成员执行这些步骤。Once you have changed the oplog size for all secondary members, perform these steps on the primary.更改所有次要成员的oplog大小后,请在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(可选)验证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更改副本集成员的oplog大小

Resize the oplog with the replSetResizeOplog command. 使用replSetResizeOplog命令调整oplog的大小。The size is a double and must be greater than 990 megabytes. size为双倍,必须大于990 MB。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.以下操作将副本集成员的oplog大小更改为16 GB或16000 MB。

db.adminCommand({replSetResizeOplog: 1, size: Double(16000)})
[1] 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可以超过其配置的大小限制,以避免删除majority commit point

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. 减小oplog的大小不会自动回收分配给原始oplog大小的磁盘空间。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. 以前,oplog复制将在压缩期间暂停。Because of this, it was recommended that oplog compaction only be performed during maintenance windows, where writes could be minimized or stopped. 因此,建议仅在维护窗口期间执行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. 不要对主副本集成员运行compactConnect a mongo shell directly to the primary (not the replica set) and run rs.stepDown(). mongo shell直接连接到主服务器(而不是副本集),然后运行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. 对于实施身份验证的集群,使用本地数据库和oplog.rs集合上的compact权限操作以用户身份进行身份验证。For complete documentation on compact authentication requirements, see compact Required Privileges.有关compact身份验证要求的完整文档,请参阅compact所需权限

←  Replica Set Maintenance TutorialsPerform Maintenance on Replica Set Members →