ITPub博客

首页 > 数据库 > NoSQL > mongodb,“not master and slaveOk=false”

mongodb,“not master and slaveOk=false”

NoSQL 作者:season0891 时间:2014-06-23 17:29:59 0 删除 编辑

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:28017/_replSet site I see that I have a primary, seconday, and arbitor.

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?

share|improve this question

    
figured out that I have to use rs.slaveOk(); That leaves me to another question. I have to do this do this for every query? What if I am on the master node? –  user959129 Jan 24 '12 at 16:26
add comment

4 Answers

You have to set "slave okay" mode to let the mongo shell know that you're allowing reads from a secondary. This is to protect you and your applications from performing eventually consistent reads by accident. You can do this in the shell with:

rs.slaveOk() 

After that you can query normally from secondaries.

A note about "eventual consistency": under normal circumstances, replica set secondaries have all the same data as primaries within a second or less. Under very high load, data that you've written to the primary may take a while to replicate to the secondaries. This is known as "replica lag", and reading from a lagging secondary is known as an "eventually consistent" read, because, while the newly written data will show up at some point (barring network failures, etc), it may not be immediately available.

Edit: You only need to set slaveok when querying from secondaries, and only once per session.

share|improve this answer

add comment

To avoid typing rs.slaveOk() every time, do this:

Create a file named replStart.js, containing one line: rs.slaveOk()

Then include --shell replStart.js when you launch the Mongo shell. Of course, if you're connecting locally to a single instance, this doesn't save any typing.

share|improve this answer

5  
A better way to save on typing would be to add rs.slaveOk() to your ~/.mongorc.js file, which will be automatically executed when starting the mongo shell. –  Stennie May 7 '13 at 14:27
1  
I find it useful to put the default configuration in ~/.mongorc.js and custom configurations in replStart.js or adminStart.js or whatever. –  Ed Norris May 13 '13 at 17:58
add comment

in mongodb2.0

you should type

rs.slaveOk 

in primary mongod node

share|improve this answer

add comment

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/90618/viewspace-1192029/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 后台运行MongoDB
请登录后发表评论 登录
全部评论

注册时间:2008-06-10

  • 博文量
    790
  • 访问量
    2097990