发布于 2015-09-14 14:46:20 | 159 次阅读 | 评论: 0 | 来源: 网络整理
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 副本集基本概念 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 副本集的操作和管理.
The following is an overview of the procedure for changing the size of the oplog:
The examples in this procedure use the following configuration:
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.
Shut down the mongod instance and restart it in “standalone” mode running on a different port.
注解
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
Backup the existing oplog on the standalone instance. Use the following sequence of commands:
mongodump --db local --collection 'oplog.rs' --port 37017
注解
You can restore the backup using the mongorestore utility.
Connect to the instance using the mongo shell:
mongo --port 37017
Save the last entry from the old (current) oplog.
In the mongo shell, enter the following command to use the local database to interact with the oplog:
use local
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()
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.
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 }
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()
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.
Repeat this procedure for all other members of the replica set that are or could become primary.