Navigation
This version of the documentation is archived and no longer supported.

Change the Size of the Oplog

On this page

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 the Oplog topic in the Replica Set Fundamental Concepts document. For details on the how oplog size affects delayed members and affects replication lag, see the Delayed Members topic and the Check the Replication Lag topic in Replica Set Operation and Management.

Overview

The following is an overview of the procedure for changing the size of the oplog:

  1. Shut down the current primary instance in the replica set and then restart it on a different port and in “standalone” mode.
  2. Create a backup of the old (current) oplog. This is optional.
  3. Save the last entry from the old oplog.
  4. Drop the old oplog.
  5. Create a new oplog of a different size.
  6. Insert the previously saved last entry from the old oplog into the new oplog.
  7. Restart the server as a member of the replica set on its usual port.
  8. Apply this procedure to any other member of the replica set that could become primary.

Procedure

The examples in this procedure use the following configuration:

  • The active replica set is rs0.
  • The replica set is running on port 27017.
  • The replica set is running with a data directory of /srv/mongodb.

To change the size of the oplog for a replica set, use the following procedure for every member of the set that may become primary.

  1. Shut down the mongod instance and restart it in “standalone” mode running on a different port.

    Note

    Shutting down the primary member of the set will trigger a failover situation and another member in the replica set will become primary. In most cases, it is least disruptive to modify the oplogs of all the secondaries before modifying the primary.

    To shut down the current primary instance, use a command that resembles the following:

    mongod --dbpath /srv/mongodb --shutdown
    

    To restart the instance on a different port and in “standalone” mode (i.e. without replSet or --replSet), use a command that resembles the following:

    mongod --port 37017 --dbpath /srv/mongodb
    
  2. Backup the existing oplog on the standalone instance. Use the following sequence of commands:

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

    Note

    You can restore the backup using the mongorestore utility.

    Connect to the instance using the mongo shell:

    mongo --port 37017
    
  3. Save the last entry from the old (current) oplog.

    1. In the mongo shell, enter the following command to use the local database to interact with the oplog:

      use local
      
    2. Ensure that the temporary collection temp is empty by dropping the collection:

      db.temp.drop()
      
    3. Use the db.collection.save() operation to save the last entry in the oplog to a temporary collection:

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

      You can see this oplog entry in the temp collection by issuing the following command:

      db.temp.find()
      
  4. Drop the old oplog.rs collection in the local database. Use the following command:

    db.oplog.rs.drop()
    

    This will return true on the shell.

  5. Use the create command to create a new oplog of a different size. Specify the size argument in bytes. A value of 2147483648 will create a new oplog that’s 2 gigabytes:

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

    Upon success, this command returns the following status:

    { "ok" : 1 }
    
  6. Insert the previously saved last entry from the old oplog into the new oplog:

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

    To confirm the entry is in the new oplog, issue the following command:

    db.oplog.rs.find()
    
  7. Restart the server as a member of the replica set on its usual port:

    mongod --dbpath /srv/mongodb --shutdown
    mongod --replSet rs0 --dbpath /srv/mongodb
    

    The replica member will recover and “catch up” and then will be eligible for election to primary. To step down the “temporary” primary that took over when you initially shut down the server, use the rs.stepDown() method. This will force an election for primary. If the server’s priority is higher than all other members in the set and if it has successfully “caught up,” then it will likely become primary.

  8. Repeat this procedure for all other members of the replica set that are or could become primary.