What is opLog ?
Just as Transaction Log in MS SQL Server or Binary log in MySQL, all those transactions on MongoDB RS will be written into a separate file, called opLog.
This opLog will have a fixed size, which means, as soon as it reaches the max size, it will be overwritten.
[Or]
opLog is a capped collection that stores an ordered history of logical writes to a MongoDB database. The oplog is the basic mechanism enabling replication in MongoDB.
[Or]
The oplog (operations log) is a special capped collection that keeps a rolling record of all operations that modify the data stored in your databases. MongoDB applies database operations on the primary and then records the operations on the primary’s oplog. The secondary members then copy and apply these operations in an asynchronous process. All replica set members contain a copy of the oplog, in thelocal.oplog.rs collection, which allows them to maintain the current state of the database.
To facilitate replication, all replica set members send heartbeats (pings) to all other members. Any member can import oplog entries from any other member.
So, if there are huge number of transactions, you may need to increase the size of it.
To check your current opLog size, execute : db.printReplicationInfo()
To change the opLog size of a node in the shard, below are the steps to follow:
* If the node is Primary, make it to Secondary by executing rs.stepDown() command
* Shutdown the Secondary node
* Start it with different (dummy) port — basically, we’re isolating it from replica set
* Take dump of opLog — for safety purpose using mongodump command
* Create a temporary collection
* Insert the last entry of opLog to this temporary collection
* Drop the existing opLog collection
* Create a new opLog with custom size
* Insert the record from temporary collection into this new opLog
* You’re good to go..!
* Re-start shard with original port & promote to Primary.
* Repeat the same on other node if required.
Here are the steps with commands:
[Lab root @ hostname /opt/mongodb/bin]# ./mongo hostname:27010/admin
MongoDB shell version: 2.4.11
connecting to: hostname:27010/admin
rs1:PRIMARY>
rs1:PRIMARY> db.printReplicationInfo()
configured oplog size: 990MB
log length start to end: 31secs (0.01hrs)
oplog first event time: Fri Oct 17 2014 06:35:10 GMT-0400 (EDT)
oplog last event time: Fri Oct 17 2014 06:35:41 GMT-0400 (EDT)
now: Fri Oct 17 2014 06:36:49 GMT-0400 (EDT)
rs1:PRIMARY>
rs1:PRIMARY> // From above ouput, configured oplog size is 990MB
rs1:PRIMARY>
Make primary as secondary:
rs1:PRIMARY> use admin
switched to db admin
rs1:PRIMARY>
rs1:PRIMARY> rs.stepDown()
Fri Oct 17 06:37:17.431 DBClientCursor::init call() failed
Fri Oct 17 06:37:17.433 Error: error doing query: failed at src/mongo/shell/query.js:78
Fri Oct 17 06:37:17.434 trying reconnect to hostname:27010
Fri Oct 17 06:37:17.435 reconnect hostname:27010 ok
rs1:SECONDARY>
rs1:SECONDARY>
Now, shutdown this node:
rs1:SECONDARY> use admin
switched to db admin
rs1:SECONDARY>
rs1:SECONDARY> db.shutdownServer()
Fri Oct 17 06:38:02.392 DBClientCursor::init call() failed
server should be down...
Fri Oct 17 06:38:02.397 trying reconnect to localhost:27010
Fri Oct 17 06:38:02.398 reconnect localhost:27010 failed couldn't connect to server localhost:27010
>
>
Bye
Start with dummy port:
[Lab root @ hostname /opt/mongodb/bin]# ./mongod --shardsvr --replSet rs1 --dbpath /opt/mongodb/data/shard1_1 --logpath /opt/mongodb/logs/shard1_2.log --port 33333 --config /etc/mongod.conf &
[1] 20113
[Lab root @ hostname /opt/mongodb/bin]
# about to fork child process, waiting until server is ready for connections.
forked process: 20115
all output going to: /opt/mongodb/logs/shard1_2.log
child process started successfully, parent exiting
[Lab root @ hostname /opt/mongodb/bin]#
[Lab root @ hostname /opt/mongodb/bin]#
[Lab root @ hostname /opt/mongodb/bin]# ./mongo hostname:33333/admin
MongoDB shell version: 2.4.11
connecting to: hostname:33333/admin
>
>
> db.printReplicationInfo()
configured oplog size: 990MB
log length start to end: 31secs (0.01hrs)
oplog first event time: Fri Oct 17 2014 06:35:10 GMT-0400 (EDT)
oplog last event time: Fri Oct 17 2014 06:35:41 GMT-0400 (EDT)
now: Fri Oct 17 2014 06:38:54 GMT-0400 (EDT)
>
>
bye
[Lab root @ hostname /opt/mongodb/bin]#
Create Dump of opLog:
Lab root @ hostname /opt/mongodb/bin]# ll -lhtr
total 233M
-rwxr-xr-x 1 bob bob 18M Aug 22 07:38 mongodump
-rwxr-xr-x 1 bob bob 18M Aug 22 07:38 mongorestore
-rwxr-xr-x 1 bob bob 18M Aug 22 07:38 mongoexport
-rwxr-xr-x 1 bob bob 18M Aug 22 07:38 mongoimport
-rwxr-xr-x 1 bob bob 18M Aug 22 07:38 mongostat
-rwxr-xr-x 1 bob bob 18M Aug 22 07:38 mongotop
-rwxr-xr-x 1 bob bob 18M Aug 22 07:38 mongooplog
-rwxr-xr-x 1 bob bob 18M Aug 22 07:38 mongofiles
-rwxr-xr-x 1 bob bob 18M Aug 22 07:38 bsondump
-rwxr-xr-x 1 bob bob 18M Aug 22 07:38 mongoperf
-rwxr-xr-x 1 bob bob 18M Aug 22 07:39 mongosniff
-rwxr-xr-x 1 bob bob 18M Aug 22 07:39 mongod
-rwxr-xr-x 1 bob bob 14M Aug 22 07:39 mongos
-rwxr-xr-x 1 bob bob 9.1M Aug 22 07:39 mongo
[Lab root @ hostname /opt/mongodb/bin]#
[Lab root @ hostname /opt/mongodb/bin]#
[Lab root @ hostname /opt/mongodb/bin]# ./mongodump -db local -collection 'oplog.rs' -h hostname:33333
connected to: hostname:33333
Fri Oct 17 06:39:28.035 DATABASE: local to dump/local
Fri Oct 17 06:39:28.036 local.oplog.rs to dump/local/oplog.rs.bson
Fri Oct 17 06:39:28.037 3 objects
Fri Oct 17 06:39:28.037 Metadata for local.oplog.rs to dump/local/oplog.rs.metadata.json
[Lab root @ hostname /opt/mongodb/bin]# ll -lhtr dump/local/oplog.rs.*
-rw-r--r-- 1 root root 79 Oct 17 06:39 dump/local/oplog.rs.metadata.json
-rw-r--r-- 1 root root 280 Oct 17 06:39 dump/local/oplog.rs.bson
[Lab root @ hostname /opt/mongodb/bin]#
Create a temp collection:
[Lab root @ hostname /opt/mongodb/bin]# ./mongo hostname:33333/admin
MongoDB shell version: 2.4.11
connecting to: hostname:33333/admin
>
> use local
switched to db local
>
>
> db = db.getSiblingDB('local')
local
>
>
> show dbs
local 1.078125GB
>
>
>
> db.temp.drop()
true
>
> db.temp.save( db.oplog.rs.find( { }, { ts: 1, h: 1 } ).sort( {$natural : -1} ).limit(1).next() )
>
> db.temp.find()
{ "_id" : ObjectId("5440f23bc82b19fd35d0b883"), "ts" : Timestamp(1413542141, 2), "h" : NumberLong("-7474013698072210427") }
>
>
> db = db.getSiblingDB('local')
local
>
>
>
Bye
Drop and Create a new opLog(I’m creating here for 2 GB):
[Lab root @ hostname /opt/mongodb/bin]# df -h /opt/mongodb/
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rootvg-lv_opt 31G 15G 15G 50% /opt
[Lab root @ hostname /opt/mongodb/bin]
[Lab root @ hostname /opt/mongodb/bin]# ./mongo hostname:33333/admin
MongoDB shell version: 2.4.11
connecting to: hostname:33333/admin
Server has startup warnings:
> use local
switched to db local
>
>
> show collections
me
oplog.rs
slaves
startup_log
system.indexes
system.replset
temp
>
>
> db.oplog.rs.drop()
true
>
> show collections
me
slaves
startup_log
system.indexes
system.replset
temp
>
>
> db.runCommand({create: "oplog.rs", capped: true, size:(2 * 1024 * 1024 * 1024)})
{ "ok" : 1 }
>
Re-start as earlier:
> use admin
switched to db admin
>
> db.shutdownServer()
Fri Oct 17 06:45:26.456 DBClientCursor::init call() failed
server should be down...
Fri Oct 17 06:45:26.459 trying reconnect to localhost:33333
Fri Oct 17 06:45:26.460 reconnect localhost:33333 failed couldn't connect to server localhost:33333
>
>
bye
[Lab root @ hostname /opt/mongodb/bin]# ./mongod --shardsvr --replSet rs1 --dbpath /opt/mongodb/data/shard1_1 --logpath /opt/mongodb/logs/shard1_2.log --port 27010 --config /etc/mongod.conf &
[1] 24094
[Lab root @ hostname /opt/mongodb/bin]
# about to fork child process, waiting until server is ready for connections.
forked process: 24096
all output going to: /opt/mongodb/logs/shard1_2.log
child process started successfully, parent exiting
[Lab root @ hostname /opt/mongodb/bin]
[Lab root @ hostname /opt/mongodb/bin]# ./mongo hostname:27010/admin
MongoDB shell version: 2.4.11
connecting to: hostname:27010/admin
rs1:SECONDARY>
rs1:SECONDARY>
rs1:SECONDARY>
rs1:SECONDARY>
rs1:SECONDARY> db.printReplicationInfo()
configured oplog size: 2048MB
log length start to end: 0secs (0hrs)
oplog first event time: Fri Oct 17 2014 06:35:41 GMT-0400 (EDT)
oplog last event time: Fri Oct 17 2014 06:35:41 GMT-0400 (EDT)
now: Fri Oct 17 2014 06:46:31 GMT-0400 (EDT)
rs1:SECONDARY>
bye
Promote it back to Primary from newly promoted Primary:
[Lab root @ hostname /opt/mongodb/bin]# ./mongo hostname:27011/admin
MongoDB shell version: 2.4.11
connecting to: hostname:27011/admin
rs1:PRIMARY>
rs1:PRIMARY>
rs1:PRIMARY> db.printReplicationInfo()
configured oplog size: 990MB
log length start to end: 0secs (0hrs)
oplog first event time: Fri Oct 17 2014 06:35:41 GMT-0400 (EDT)
oplog last event time: Fri Oct 17 2014 06:35:41 GMT-0400 (EDT)
now: Fri Oct 17 2014 06:47:19 GMT-0400 (EDT)
rs1:PRIMARY> use admin
switched to db admin
rs1:PRIMARY>
rs1:PRIMARY> rs.stepDown()
Fri Oct 17 06:47:40.329 DBClientCursor::init call() failed
Fri Oct 17 06:47:40.331 Error: error doing query: failed at src/mongo/shell/query.js:78
Fri Oct 17 06:47:40.331 trying reconnect to hostname:27011
Fri Oct 17 06:47:40.332 reconnect hostname:27011 ok
rs1:SECONDARY>
rs1:SECONDARY>
bye
[Lab root @ hostname /opt/mongodb/bin]#
[Lab root @ hostname /opt/mongodb/bin]#
[Lab root @ hostname /opt/mongodb/bin]# ./mongo hostname:27010/admin
MongoDB shell version: 2.4.11
connecting to: hostname:27010/admin
rs1:PRIMARY>
rs1:PRIMARY> db.printReplicationInfo()
configured oplog size: 2048MB
log length start to end: 0secs (0hrs)
oplog first event time: Fri Oct 17 2014 06:35:41 GMT-0400 (EDT)
oplog last event time: Fri Oct 17 2014 06:35:41 GMT-0400 (EDT)
now: Fri Oct 17 2014 06:47:49 GMT-0400 (EDT)
rs1:PRIMARY>
Repeat the steps on the other node to change its opLog size (if required)
Ask Question