Change the Size of the Oplog更改操作日志的大小
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
命令调整操作日志的大小,如本教程所示。
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.有关此限制的详细信息,请参阅操作日志集合行为。
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>
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
角色)。
B. (Optional) Verify the current size of the oplog(可选)验证操作日志的当前大小
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
字段以字节为单位显示集合大小。
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. size
是double
,并且必须大于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] | majority commit point . |
D. (Optional) Compact oplog.rs
to reclaim disk space(可选)压缩oplog.rs
以回收磁盘空间
oplog.rs
to reclaim disk spaceReducing 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 增加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. 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 不要对primary复制集成员运行compact
against the primary replica set member. compact
。Connect a 将mongoshell直接连接到主(而不是副本集),然后运行mongo
shell directly to the primary (not the replica set) and run rs.stepDown()
. rs.stepDown()
。If successful, the primary steps down. From the 如果成功,主进程将退出。从mongoshell中,在现在的次要成员上运行mongo
shell, run the compact
command on the now-secondary member.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
所需要的权限。