mongodb,复制和错误:{“$err";:“不是主从Ok=false",“代码";: 13435 } [英] mongodb, replicates and error: { "$err" : "not master and slaveOk=false", "code" : 13435 }

查看:17
本文介绍了mongodb,复制和错误:{“$err";:“不是主从Ok=false",“代码";: 13435 }的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试使用 mongo 副本集.

I tried mongo replica sets for the first time.

我在 ec2 上使用 ubuntu,我启动了三个实例.我使用了每个实例的私有 IP 地址.我选择了主要的,下面是代码.

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")

此时一切正常.当我访问 http://ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com:28017/_replSet 站点 我看到我有一个主要的、次要的和仲裁者.

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.

好的,现在进行测试.

在primary创建数据库的代码如下:

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 }

我对 mongodb 和复制非常陌生,但我认为如果我在一个中做某事,它会转到另一个.那么,如果我在一个记录中添加一条记录,我需要做什么才能跨机器复制?

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?

推荐答案

你必须设置二级好的"模式让 mongo shell 知道您允许从辅助读取.这是为了防止您和您的应用程序意外执行最终一致性读取.您可以在 shell 中执行此操作:

You have to set "secondary 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.secondaryOk()

之后就可以正常从辅助查询了.

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.

你只需要在从辅助查询时设置secondaryOk,并且每个会话只需要设置一次.

You only need to set secondaryOk when querying from secondaries, and only once per session.

这篇关于mongodb,复制和错误:{“$err";:“不是主从Ok=false",“代码";: 13435 }的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆