OPTIONS

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:

db.shutdownServer()

Restart this mongod as a standalone instance running on a different port and without the --replSet parameter. Use a command similar to the following:

mongod --port 37017 --dbpath /srv/mongodb

Create a Backup of the Oplog (Optional)

Optionally, backup the existing oplog on the standalone instance, as in the following example:

mongodump --db local --collection 'oplog.rs' --port 37017

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:

use local

In mongo shell scripts you can use the following operation to set the db object:

db = db.getSiblingDB('local')

Ensure that the temp temporary collection is empty by dropping the collection:

db.temp.drop()

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:

db.temp.save( db.oplog.rs.find( { }, { ts: 1, h: 1 } ).sort( {$natural : -1} ).limit(1).next() )

To see this oplog entry, use the following operation:

db.temp.find()

Remove the Existing Oplog Collection

Drop the old oplog.rs collection in the local database. Use the following command:

db = db.getSiblingDB('local')
db.oplog.rs.drop()

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:

db.runCommand( { create: "oplog.rs", capped: true, size: (2 * 1024 * 1024 * 1024) } )

Upon success, this command returns the following status:

{ "ok" : 1 }

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:

db.oplog.rs.save( db.temp.findOne() )

To confirm the entry is in the new oplog, use the following operation:

db.oplog.rs.find()

Restart the Member

Restart the mongod as a member of the replica set on its usual port. For example:

db.shutdownServer()
mongod --replSet rs0 --dbpath /srv/mongodb

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.