Hand in hand with the previous operational tip, we may need to rethink and resize our oplog as our data grows. Operations become more complicated and time-consuming as our data grows, and we need to adjust our oplog size to accommodate for it. The steps for changing the oplog's size are as follows:
- The first step is to restart our MongoDB secondary server as a standalone server, an operation that was described in the How to perform maintenance on replica sets section.
- We then make a backup of our existing oplog:
> mongodump --db local --collection 'oplog.rs' --port 37017
- We keep a copy of this data, just in case. We then connect to our standalone database:
> use local
> db = db.getSiblingDB('local')
> db.temp.drop()
Up until now, we have connected to the local database and deleted the temp collection, just in case it had any leftover documents.
- The next step is to get the last entry of our current oplog and save it in the temp collection:
> db.temp.save( db.oplog.rs.find( { }, { ts: 1, h: 1 } ).sort( {$natural : -1} ).limit(1).next() )
- This entry will be used when we restart our secondary server, in order to track where it has reached in the oplog replication:
> db = db.getSiblingDB('local')
> db.oplog.rs.drop()
- Now, we delete our existing oplog, and in the next step, we will create a new oplog of 4 GB in size:
> db.runCommand( { create: "oplog.rs", capped: true, size: (4 * 1024 * 1024 * 1024) } )
- The next step is to copy the one entry from our temp collection back to our oplog:
> db.oplog.rs.save( db.temp.findOne() )
- Finally, we cleanly shut down our server from the admin database, using the db.shutdownServer() command, and we restart our secondary as a member of the replica set.
- We repeat this process for all secondary servers, and as a last step, we repeat the procedure for our primary member, which is done after we step the primary down by using the following command:
> rs.stepDown(600)