mongodb,复制和错误:{“$err”:“not master and slaveOk=false”,“code”:13435 }
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8990158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mongodb, replicates and error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
提问by dcrosta
I tried mongo replica sets for the first time.
我第一次尝试了 mongo 副本集。
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.
我在 ec2 上使用 ubuntu 并启动了三个实例。我使用了每个实例的私有 IP 地址。我选择了主要的,下面是代码。
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/_replSetsite I see that I have a primary, seconday, and arbitor.
此时一切正常。当我访问http://ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com:28017/_replSet站点时,我看到我有一个主要的、次要的和仲裁者。
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?
我对 mongodb 和复制非常陌生,但我认为如果我在一个中做某事,它会转到另一个。那么,如果我在一个记录中添加一条记录,我必须做什么才能跨机器复制?
回答by dcrosta
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:
您必须设置“slave OK”模式,让 mongo shell 知道您允许从辅助读取。这是为了防止您和您的应用程序意外执行最终一致性读取。您可以在 shell 中使用以下命令执行此操作:
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.
编辑:您只需要在从辅助服务器查询时设置 slaveok,并且每个会话只需要设置一次。
回答by Ed Norris
To avoid typing rs.slaveOk()
every time, do this:
为避免rs.slaveOk()
每次输入,请执行以下操作:
Create a file named replStart.js
, containing one line: rs.slaveOk()
创建一个名为 的文件replStart.js
,其中包含一行: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.
然后--shell replStart.js
在启动 Mongo shell 时包含。当然,如果您在本地连接到单个实例,这不会保存任何输入。
回答by andyshi
in mongodb2.0
在 mongodb2.0
you should type
你应该输入
rs.slaveOk()
in secondary mongod node
在辅助 mongod 节点中
回答by campeterson
THIS IS JUST A NOTE FOR ANYONE DEALING WITH THIS PROBLEM USING THE RUBY DRIVER
这只是任何使用 Ruby 驱动程序处理此问题的人的注意事项
I had this same problem when using the Ruby Gem.
我在使用 Ruby Gem 时遇到了同样的问题。
To set slaveOk in Ruby, you just pass it as an argument when you create the client like this:
要在 Ruby 中设置 slaveOk,您只需在创建客户端时将其作为参数传递,如下所示:
mongo_client = MongoClient.new("localhost", 27017, { slave_ok: true })
https://github.com/mongodb/mongo-ruby-driver/wiki/Tutorial#making-a-connection
https://github.com/mongodb/mongo-ruby-driver/wiki/Tutorial#making-a-connection
mongo_client = MongoClient.new # (optional host/port args)
Notice that 'args' is the third optional argument.
请注意,'args' 是第三个可选参数。
回答by Prabhat
slaveOk does not work anymore. One needs to use readPreference https://docs.mongodb.com/v3.0/reference/read-preference/#primaryPreferred
slaveOk 不再起作用。一个需要使用 readPreference https://docs.mongodb.com/v3.0/reference/read-preference/#primaryPreferred
e.g.
例如
const client = new MongoClient(mongoURL + "?readPreference=primaryPreferred", { useUnifiedTopology: true, useNewUrlParser: true });
回答by jit
I am just adding this answer for an awkward situation from DB provider.
我只是为 DB 提供程序的尴尬情况添加此答案。
what happened in our case is the primary and secondary db shifted reversely (primary to secondary and vice versa) and we are getting the same error.
在我们的例子中发生的事情是主数据库和辅助数据库反向移动(主数据库到次数据库,反之亦然),我们得到了同样的错误。
so please check in the configuration settings for database status which may help you.
所以请检查数据库状态的配置设置,这可能会对您有所帮助。
回答by kub1x
I got here searching for the same error, but from Node.js native driver. The answer for me was combination of answers by campetersonand Prabhat.
我来到这里寻找相同的错误,但来自Node.js native driver。对我来说,答案是campeterson和Prabhat的答案组合。
The issue is that readPreference
setting defaults to primary
, which then somehow leads to the confusing slaveOk
error. My problem is that I just wan to read from my replica set from any node. I don't even connect to it as to replicaset. I just connect to any node to read from it.
问题是readPreference
设置默认为primary
,然后以某种方式导致令人困惑的slaveOk
错误。我的问题是我只想从任何节点读取我的副本集。我什至没有连接到它的副本集。我只是连接到任何节点来读取它。
Setting readPreference
to primaryPreferred
(or better to the ReadPreference.PRIMARY_PREFERRED
constant) solved it for me. Just pass it as an option to MongoClient.connect()
or to client.db()
or to any find()
, aggregate()
or other function.
设置readPreference
为primaryPreferred
(或更好地为ReadPreference.PRIMARY_PREFERRED
常量)为我解决了这个问题。只是把它作为一个选项,MongoClient.connect()
或client.db()
或任何find()
,aggregate()
或其他功能。
- https://docs.mongodb.com/v3.0/reference/read-preference/#primaryPreferred
- http://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html(search readPreference)
- https://docs.mongodb.com/v3.0/reference/read-preference/#primaryPreferred
- http://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html(搜索readPreference)
const { MongoClient, ReadPreference } = require('mongodb');
const client = await MongoClient.connect(MONGODB_CONNECTIONSTRING, { readPreference: ReadPreference.PRIMARY_PREFERRED });