Home > MongoDB > How to get a MongoDB oplog without a full replica set

How to get a MongoDB oplog without a full replica set

The MongoDB oplog is a rolling log of the most recent operations that occurred in a MongoDB instance. It’s intended for replication so that multiple data nodes can follow the oplog and immediately get updates, however, your application code has access to it as well. This means you can “tail the oplog” and your application code is notified as soon as an insert, update, or delete operation occurs anywhere on the database.  This is very cool stuff, so cool, in fact, that it’s nice to be able to enable it even if you aren’t using a multiple node replica sets. Replica sets provide high availability and disaster recovery, but you might not need that or have the extra server resources for it – maybe you just want real time notification.  Here is how you enable the oplog on a standalone MongoDB instance.

In the /etc/mongodb.conf file on Debian or similar file on RHEL, you should set two options and then restart your MongoDB instance.


replSet=rs0
oplogSize=100

Or you can set these at the command line, as you might on Windows:


--replSet rs0 --oplogSize 100

The first setting, “replSet rs0” tells you this will be a replica set node, which will allow you to run “rs.initiate()” from the mongo shell.  Doing so makes this into a single node replica set.  The name “rs0” is complete arbitrary – call it whatever you want.  The next option, “oplogSize 100” limits the oplog to 100 MB.  You can leave this option off and it will default to using 5% of your free disk space.  You’d want to support a huge oplog if your need multiple data nodes to survive an outtage of several hours or days and be able to catch back up without needing a full resync.  However, if you’re running a single node just to get an oplog and real time notifications, you can cap it at 100 MB, or maybe much less.

After setting these options and starting your MongoDB instance, connect to the shell, change to the local database, and run “rs.initiate()”.  You’ll now have an oplog and to tail for real time notifications.


> show dbs
local (empty)
> use local
switched to db local
> rs.initiate()
{
 "info2" : "no configuration explicitly specified -- making one",
 "me" : "MONGOSERVER:27017",
 "info" : "Config now saved locally. Should come online in about a minute.",
 "ok" : 1
}
> show collections
oplog.rs
system.replset
rs0:PRIMARY>

Advertisements
Categories: MongoDB
  1. December 19, 2012 at 4:05 pm

    Thanks for this. Just in case others run in to the same issue I did — If you are experiencing issues locally where rs.initiate() says that ‘self is not part of the replica set’ (or something to that effect), you need to pass in a configuration block to rs.initiate():

    rs.initiate({_id : ‘rs0’,
    members : [
    {_id : 0, host : ‘127.0.0.1’}
    ]
    })

  2. Dika Andharu
    February 3, 2015 at 10:05 am

    where to wrote –replSet rs0 –oplogSize 100 ?

    • loosexaml
      February 3, 2015 at 1:10 pm

      Those are options to pass to mongod (or mongod.exe on Windows).

  3. September 7, 2015 at 4:46 pm

    Good day,

    * this is old post but I see new responses, and people might think that this is the way to deal this issue now. I hope this comment is useful:

    Your post is about “get a MongoDB oplog without a full replica set”, but you just created “a replica set”. The only thing that you have done is create rep with one member only. the answer to “How to get a MongoDB oplog without replica set” is simply use the parameter –master during init the server.

    for example you can use:
    mongoD –dbpath C:\MongoDBs\DB2 –port 2700 –logpath C:\MongoDBs\MyLog.log –master

  4. loosexaml
    May 31, 2013 at 6:03 am

    It looks like an error in mongodb river to me, not a problem with the oplog configuration.

  1. May 7, 2013 at 12:03 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: