Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

I tried mongo replica sets for the first time.

I am using ubuntu on ec2 and I booted up three instances. I used the private IP address of each of the instances. I picked on as the primary and below is the code.

mongo --host Private IP Address 

rs.initiate() 

rs.add(“Private IP Address”) 

rs.addArb(“Private IP Address”)

All at this point is fine. When I go to the http://ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com:28000/_replSet site I see that I have a primary, secondary, and arbitrary.

Ok, now for a test.

On the primary create a database in this is the code:

use tt 

db.tt.save( { a : 123 } )

on the secondary, I then do this and get the below error:

db.tt.find() 

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

I am very new to MongoDB and replicates but I thought that if I do something in one, it goes to the other. So, if I add a record in one, what do I have to do to replicate across machines?

1 Answer

0 votes
by (44.4k points)

Mongo shell has to know that you are allowing read operations from secondaries, this means you have to set “slave okay”. Do this:

rs.slaveOk()

So, you only need to do this when you query secondaries and need to be used only once per session. After this, you can normally query secondaries.

Related questions

0 votes
1 answer
Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...