0 votes
1 view
in AWS by (19.2k 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.6k 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.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...