MongoDB – Replica Set

Here’s a good tutorial on Replica-Set on MongoDB website http://www.mongodb.org/display/DOCS/Replica+Set+Tutorial

In this tutorial, in “Replication” section tutorial asks to create a document on PRIMARY server.

db.messages.save({name: "ReplSet Tutorial"});

After creating a document on PRIMARY if you hit

db.messages.find()

in MongoDB shell the result is displayed. But if you switch to SECONDARY and execute same command to check if replication is working, you get following error.

uncaught exception: error: { "$err" : "not master and slaveok=false", "code" : 13435 }

The reason is stated here : http://www.mongodb.org/display/DOCS/Querying#Querying-slaveOk

So all you need to do is hit this command

db.getMongo().setSlaveOk();

That’s it. Now “db.messages.find()” should work on SECONDARY as well.

Enjoy!!!

About these ads

One thought on “MongoDB – Replica Set

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