Subscriber Count

    464

Subscribe to Posts by Email

Pages

MongoDB for Oracle DBA’s Part 11 : Enabling Preferred Read on Standby (Secondary) Instances

As we know in ASM we can specificy the preferred read fail group to scatter the reads across the failgroups to reduce the read contention on single disk group. Similarly in MongoDB you can set to read the data from secondary servers i.e standby, In other words take this as active dataguard feature where you can run queries and backups on standby.

Here is how to do it.

### Check the Lag of the Standby or secondaries

MongoDB Enterprise rs0:SECONDARY> rs.printSlaveReplicationInfo()

source: localhost:47018

        syncedTo: Mon Dec 12 2016 09:15:35 GMT+1100 (AEDT)

        0 secs (0 hrs) behind the primary

source: localhost:47020

        syncedTo: Mon Dec 12 2016 09:15:35 GMT+1100 (AEDT)

        0 secs (0 hrs) behind the primary

MongoDB Enterprise rs0:SECONDARY>

### Connect to any secondary host and run the query, eventually it throw errors

mongo localhost:47018

MongoDB Enterprise rs0:SECONDARY> use foo

switched to db foo

MongoDB Enterprise rs0:SECONDARY>  db.testData.find()

Error: error: { "$err" : "not master and slaveOk=false", "code" : 13435 }

MongoDB Enterprise rs0:SECONDARY>

Error out as its not master

### Enable the reads on slave (remember you need to do every time when you connect

MongoDB Enterprise rs0:SECONDARY> rs.slaveOk()

MongoDB Enterprise rs0:SECONDARY> use foo

switched to db foo

MongoDB Enterprise rs0:SECONDARY> db.testData.find()

{ "_id" : ObjectId("5835286da372386306caeee2"), "x" : 1 }

{ "_id" : ObjectId("5835286da372386306caeee3"), "x" : 2 }

{ "_id" : ObjectId("5835286da372386306caeee4"), "x" : 3 }

{ "_id" : ObjectId("5835286da372386306caeee5"), "x" : 4 }

{ "_id" : ObjectId("5835286da372386306caeee6"), "x" : 5 }

{ "_id" : ObjectId("5835286da372386306caeee7"), "x" : 6 }

{ "_id" : ObjectId("5835286da372386306caeee8"), "x" : 7 }

{ "_id" : ObjectId("5835286da372386306caeee9"), "x" : 8 }

{ "_id" : ObjectId("5835286da372386306caeeea"), "x" : 9 }

{ "_id" : ObjectId("5835286da372386306caeeeb"), "x" : 10 }

{ "_id" : ObjectId("5835286da372386306caeeec"), "x" : 11 }

{ "_id" : ObjectId("5835286da372386306caeeed"), "x" : 12 }

{ "_id" : ObjectId("5835286da372386306caeeee"), "x" : 13 }

{ "_id" : ObjectId("5835286da372386306caeeef"), "x" : 14 }

{ "_id" : ObjectId("5835286da372386306caeef0"), "x" : 15 }

{ "_id" : ObjectId("5835286da372386306caeef1"), "x" : 16 }

{ "_id" : ObjectId("5835286da372386306caeef2"), "x" : 17 }

{ "_id" : ObjectId("5835286da372386306caeef3"), "x" : 18 }

{ "_id" : ObjectId("5835286da372386306caeef4"), "x" : 19 }

{ "_id" : ObjectId("5835286da372386306caeef6"), "x" : 21 }

Type "it" for more

MongoDB Enterprise rs0:SECONDARY>

-Thanks

Geek DBA

 

 

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>