简体   繁体   English

在副本集模式下连接到Mongo

[英]Connecting to Mongo in Replica set mode

I have a standalone Mongo instance running a replica set. 我有一个运行副本集的独立Mongo实例。 I can't seem to connect and run any queries in the Mongo shell however. 我似乎无法在Mongo shell中连接和运行任何查询。 I get the following: 我得到以下内容:

error: { "$err" : "not master and slaveOk=false", "code" : 13435 }

I set SlaveOk like so: 我这样设置了SlaveOk:

db.getMongo().setSlaveOk()

..but I still get an error: ..但我仍然得到一个错误:

error: {
"$err" : "not master or secondary; cannot currently read from this replSet member",
"code" : 13436
}

I can't seem to find a straight answer on Google: how do I connect to my replica set using the mongo shell? 我似乎无法在Google上找到一个直接的答案:如何使用mongo shell连接到我的副本集?

If you are connecting to a node in a replica set that is not the master, you need to explicitly tell the client that it is ok that your are not connected to a master.. 如果要连接到不是主服务器的副本集中的节点,则需要明确告诉客户端您没有连接到主服务器。

You can do this by calling 你可以通过电话来做到这一点

rs.slaveOk() rs.slaveOk()

You can then perform your query. 然后,您可以执行查询。

Note that you will only be able to perform queries, not make changes to the repository, when connected to a slave node. 请注意,只有在连接到从属节点时,您才能执行查询,而不能对存储库进行更改。

我遇到了同样的问题并使用它解决了

rs.initiate()

You are connected to a node that is neither in state secondary or primary. 您已连接到既不是状态辅助节点也不是主节点的节点。 This node could be an arbiter or possibly a secondary in recovery mode. 该节点可以是仲裁器,也可以是恢复模式中的辅助节点。 For example, if I had a replica set of 3 nodes, (where there is one primary, a secondary and an arbiter) I would get the same error if I had connected to the arbiter and issued a query even after I had set slaveOK true. 例如,如果我有一个3个节点的副本集,(其中有一个主节点,一个辅助节点和一个仲裁节点)如果我连接到仲裁器并发出查询,即使我设置了slaveOK,我也会得到相同的错误。 The shell's command line prompt should indicate what state the node you are connected is in: shell的命令行提示符应指明您连接的节点所处的状态:

foo:ARBITER> db.test.find()
error: {
    "$err" : "not master or secondary; cannot currently read from this replSet member",
    "code" : 13436
}

你试过吗:db.getMongo()。setSlaveOk(true)

I also got the error. 我也得到了错误。 But when i tried connecting to the secondary node using the machine name instead of 'localhost' or 127.0.0.1 the error went away. 但是,当我尝试使用机器名称而不是“localhost”或127.0.0.1连接到辅助节点时,错误消失了。

This error is only displayed when you are running an instance that's part of a replica set in standalone mode without completely removing it from the replica set. 仅当您在独立模式下运行副本集的一部分而未完全从副本集中删除它时,才会显示此错误。 eg You restart your instance on a different port but don't remove the --repSet option when starting it. 例如,您在另一个端口上重新启动实例,但在启动它时不要删除--repSet选项。 This starts it but neither as a primary nor as a secondary, hence the error not master or secondary; 这启动它,但既不是主要的也不是次要的,因此错误不是主要的或次要的;

Depending on what you intended to do initially, either restart the instance on the correct port and with the correct --repSet option. 根据您最初的意图,在正确的端口上使用正确的--repSet选项重新启动实例。 This adds it to the replica set and gets rid of this error 这将它添加到副本集并摆脱此错误

If you intended to run the instance as standalone for some time (say to create an index), then start it on a different port WITHOUT the --repSet option 如果您打算在一段时间内将实例作为独立运行(比如创建索引),那么在没有--repSet选项的情况下在另一个端口上启动它

I got the same error while running aggregate() on staging server with two replica sets.I think you need to change the read preference to 'secondaryPreferred'. 我在使用两个副本集的登台服务器上运行aggregate()时遇到了同样的错误。我认为您需要将读取首选项更改为“secondaryPreferred”。

Just put .read('secondaryPreferred') after the query function. 只需在查询函数之后放入.read('secondaryPreferred')。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM