- Replication >
- Replica Set Tutorials >
- Replica Set Maintenance Tutorials >
- Change the Size of the Oplog
Change the Size of the Oplog¶
The oplog exists internally as a capped collection, so you cannot modify its size in the course of normal operations. In most cases the default oplog size is an acceptable size; however, in some situations you may need a larger or smaller oplog. For example, you might need to change the oplog size if your applications perform large numbers of multi-updates or deletes in short periods of time.
This tutorial describes how to resize the oplog. For a detailed explanation of oplog sizing, see Oplog Size. For details how oplog size affects delayed members and affects replication lag, see Delayed Replica Set Members.
Overview¶
To change the size of the oplog, you must perform maintenance on each
member of the replica set in turn. The procedure requires: stopping
the mongod
instance and starting as a standalone instance,
modifying the oplog size, and restarting the member.
Important
Always start rolling replica set maintenance with the secondaries, and finish with the maintenance on primary member.
Procedure¶
Restart the member in standalone mode.
Tip
Always use
rs.stepDown()
to force the primary to become a secondary, before stopping the server. This facilitates a more efficient election process.Recreate the oplog with the new size and with an old oplog entry as a seed.
Restart the
mongod
instance as a member of the replica set.
Restart a Secondary in Standalone Mode on a Different Port¶
Shut down the mongod
instance for one of the non-primary
members of your replica set. For example, to shut down, use the
db.shutdownServer()
method:
Restart the mongod
as a standalone, making the following
configuration updates:
- Configuration File
- Command-line Options
If you are using a configuration file, make the following configuration updates:
- Comment out the
replication.replSetName
option. - Change the
net.port
to a different port. [1] Make a note of the original port setting as a comment. - If the
mongod
is a shard or config server member, you must also:- Comment out the
sharding.clusterRole
option. - Set parameter
skipShardingConfigurationChecks
(available in MongoDB 3.2.19+) totrue
in thesetParameter
section.
- Comment out the
For example, for a shard/config server replica set member, the updated configuration file will include content like the following example:
If using command-line options, make the following configuration updates:
- Remove
--replSetName
. - Modify
--port
to a different port. [1] - If the
mongod
is a shard or config server member, you must also:- Remove
--shardsvr
if a shard member and--configsvr
if a config server member. - Set parameter
skipShardingConfigurationChecks
(available in MongoDB 3.2.19+) totrue
in thesetParameter
section.
- Remove
For example, if your replica set member that is not part of
a sharded cluster normally runs with on the default port
of 27017
with the --replSet
option you would use the following invocation:
[1] | (1, 2) By running the mongod on a different
port, you ensure that the other members of the replica set and all
clients will not contact the member while you are building the
index. |
Create a Backup of the Oplog (Optional)¶
Optionally, backup the existing oplog on the standalone instance, as in the following example:
Recreate the Oplog with a New Size and a Seed Entry¶
Save the last entry from the oplog. For example, connect to the instance
using the mongo
shell, and enter the following command to
switch to the local
database:
In mongo
shell scripts you can use the following operation
to set the db
object:
Ensure that the temp
temporary collection is empty by dropping the
collection:
Use the db.collection.save()
method and a sort on
reverse natural order to find the last entry and save it
to a temporary collection:
To see this oplog entry, use the following operation:
Remove the Existing Oplog Collection¶
Drop the old oplog.rs
collection in the local
database. Use
the following command:
This returns true
in the shell.
Create a New Oplog¶
Use the create
command to create a new oplog of a
different size. Specify the size
argument in bytes. A value of
2 * 1024 * 1024 * 1024
will create a new oplog that’s 2 gigabytes:
Upon success, this command returns the following status:
Insert the Last Entry of the Old Oplog into the New Oplog¶
Insert the previously saved last entry from the old oplog into the new oplog. For example:
To confirm the entry is in the new oplog, use the following operation:
Restart the Member¶
Restart the mongod
as a member of the replica set on its
usual port. That is, undo the configuration changes made when starting
as a standalone. For shard or config server replica set members, be
sure to remove the skipShardingConfigurationChecks
parameter.
For example:
The replica set member will recover and “catch up” before it is eligible for election to primary.
Repeat Process for all Members that may become Primary¶
Repeat this procedure for all members you want to change the size of the oplog. Repeat the procedure for the primary as part of the following step.
Change the Size of the Oplog on the Primary¶
To finish the rolling maintenance operation, step down the primary with the
rs.stepDown()
method and repeat the oplog resizing procedure
above.