On this page本页内容
In MongoDB versions 3.4 and earlier, the oplog was resized by dropping and recreating the 在MongoDB 3.4及更早版本中,通过删除并重新创建local.oplog.rs collection.local.oplog.rs集合来调整oplog的大小。
In MongoDB versions 3.6 and later, use the 在MongoDB 3.6及更高版本中,使用replSetResizeOplog command to resize the oplog as shown in this tutorial.replSetResizeOplog命令调整oplog的大小,如本教程所示。
Starting in MongoDB 4.0, MongoDB forbids dropping the 从MongoDB 4.0开始,MongoDB禁止删除local.oplog.rs collection. 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上执行以下步骤。
Connect to the replica set member using 使用mongosh:mongosh连接到副本集成员:
mongosh --host <hostname>:<port>
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数据库权限的用户身份进行身份验证,例如clusterManager或clusterAdmin角色。
To view the current size of the oplog, switch to the 要查看oplog的当前大小,请切换到local database and run db.collection.stats() against the oplog.rs collection. local数据库并对oplog.rs集合运行db.collection.stats()。stats() displays the oplog size as 将oplog大小显示为maxSize.maxSize。
use local db.oplog.rs.stats().maxSize
The maxSize field displays the collection size in bytes.maxSize字段以字节为单位显示集合大小。
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] | majority commit point.majority commit point。 |
oplog.rs to reclaim disk spaceoplog.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 增加oplog大小后,在compact on the oplog.rs collection after increasing the oplog size.oplog.rs集合上运行compact没有任何好处。
Starting in MongoDB v4.4, a replica set member can replicate oplog entries while the 从MongoDB v4.4开始,副本集成员可以在compact operation is ongoing. 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. compact。Connect 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所需权限。