奇怪的 mongodb 和 mongoose 错误:不是 master 和 slaveOk=false 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30088833/
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
strange mongodb and mongoose error: not master and slaveOk=false error
提问by MonkeyBonkey
I'm getting a strange error in my node.js app
我的 node.js 应用程序中出现一个奇怪的错误
not master and slaveOk=false code 13435
not master and slaveOk=false code 13435
I'm doing a findOne query using mongoose 4.0.2 hitting a mongodb 3.0.1. The query was working earlier today.
我正在使用 mongoose 4.0.2 和 mongodb 3.0.1 进行 findOne 查询。该查询在今天早些时候起作用。
What is this error? I'm running on mongohq on their standard per gb plan.
这是什么错误?我正在按照他们的标准 per gb 计划在 mongohq 上运行。
回答by Nocturno
That means you're trying to read from a secondary node in a replica set, you can only read from the primary node by default.
这意味着您正在尝试从副本集中的辅助节点读取,默认情况下您只能从主节点读取。
You can allow a secondary node to accept reads by running rs.slaveOk()
in a mongo shell that is connected to that secondary node. Allowing reads from a secondary is not recommended, because you could be reading stale data if the node isn't yet synced with the primary node.
您可以通过rs.slaveOk()
在连接到该辅助节点的 mongo shell 中运行来允许辅助节点接受读取。不建议允许从辅助节点读取,因为如果节点尚未与主节点同步,您可能正在读取陈旧数据。
回答by Ulysses Alves
I solved this by simply fixing the URI my Node.js application was using to connect to MongoDB with Mongoose.
我通过简单地修复我的 Node.js 应用程序使用 Mongoose 连接到 MongoDB 的 URI 解决了这个问题。
When this error ocurred, my URI was
发生此错误时,我的 URI 是
mongodb://user:password@host:port/datatabase
,
mongodb://user:password@host:port/datatabase
,
and this was giving me the error not master and slaveOK=false.
这给了我错误不是 master 和 slaveOK=false。
Then I changed the URI to add the Replica Set information, and the URI became something like this:
然后我修改了URI,添加了Replica Set信息,URI就变成了这个样子:
mongodb://user:password@host:port,replicaSetHost:replicaSetPort/database?replicaSet=rs-someServer
.
mongodb://user:password@host:port,replicaSetHost:replicaSetPort/database?replicaSet=rs-someServer
.
Now, I don't know if this is a general pattern, because this configuration is the one used by MongoLab, which is where my DataBase is hosted. However, it's likely you will solve the problem by adding the replica set information in the URI as well.
现在,我不知道这是否是通用模式,因为此配置是 MongoLab 使用的配置,这是托管我的 DataBase 的地方。但是,您很可能也会通过在 URI 中添加副本集信息来解决问题。
回答by Simone Morettini
I solved the problem(happened by connecting to a instance of mongoDB on IBM) with
我解决了这个问题(通过连接到 IBM 上的 mongoDB 实例发生)
mongoose.connect('mongodb://host1:port1/?replicaSet=rsName');
From: https://mongoosejs.com/docs/connections.html#replicaset_connections
来自:https: //mongoosejs.com/docs/connections.html#replicaset_connections
回答by Da Qi
It is likely your master node failed and you are trying to read from a slave node but by default (and a safety practice such as data synchronization) your reading source is set to primary node only.
很可能你的主节点失败了,你正在尝试从从节点读取,但默认情况下(以及数据同步等安全实践)你的读取源设置为仅主节点。
You can attach options at the end of your connection string such as
?readPreference=secondary
which will use the secondary connection
您可以在连接字符串的末尾附加选项,例如
?readPreference=secondary
哪个将使用辅助连接
回答by konghou
This works for me on mongoose 4.x and mongodb 3.0.x:
这在 mongoose 4.x 和 mongodb 3.0.x 上对我有用:
model.find().read('secondary').....