简体   繁体   English

Mongo:如果主要故障,如何自动连接到辅助节点

[英]Mongo: How to automatically connect to secondary in case of primary failure

I have a replica of three mongod instances, one of them is the primary instance, and the other two are secondary. 我有三个mongod实例的副本,其中一个是主要实例,另外两个是次要实例。 I connected a mongo instance to the primary, and then I turned off the mongod primary instance, simulating a failure. 我将mongo实例连接到主要实例,然后我关闭了mongod主实例,模拟了失败。

Typically, as soon as I initiated a command, mongo generated a DBClientCursor::init call() failed error. 通常,只要我启动命令,mongo就会生成DBClientCursor :: init call()失败错误。

My question is, in case of such a failure, where the primary cannot go back to service, and there will definitely be an election for the new primary, is there a way to tell mongo to automatically search and connect to the new primary of the replica set? 我的问题是,如果出现这样的故障,主服务器无法恢复服务,并且肯定会有新主服务器的选举,有没有办法告诉mongo自动搜索并连接到新的主服务器副本集?

Possibly your problem is that you're only connecting to one node, the one which is primary at that time. 可能你的问题是你只是连接到一个节点,那个节点当时是主节点。 What you need to to do is to specify, when connecting, what the replicaset is and what the address is of each available node - so your client can reconnect to a different node if its primary falls over. 您需要做的是在连接时指定复制项是什么以及每个可用节点的地址是什么 - 因此,如果客户端的主要部分崩溃,您的客户端可以重新连接到另一个节点。

The documentation on the Mongo URI connection string format explains this: 有关Mongo URI连接字符串格式的文档说明了这一点:

hostX : Optional. hostX :可选。 You can specify as many hosts as necessary. 您可以根据需要指定任意数量的主机。 You would specify multiple hosts, for example, for connections to replica sets. 您可以指定多个主机,例如,指定与副本集的连接。

It gives this example: 它给出了这个例子:

EXAMPLE
To describe a connection to a replica set named test, with the following mongod hosts: 要使用以下mongod主机描述与名为test的副本集的连接:

db1.example.net on port 27017 and db1.example.net在端口27017和
db2.example.net on port 2500. 端口2500上的db2.example.net。
You would use a connection string that resembles the following: 您将使用类似于以下内容的连接字符串:

mongodb://db1.example.net,db2.example.net:2500/?replicaSet=test mongodb的://db1.example.net,db2.example.net:2500 / replicaSet =测试

You may need to check your driver's documentation also, in case it requires a particular way of specifying the replica set and the list of nodes. 如果需要特定的方式来指定副本集和节点列表,您可能还需要检查驱动程序的文档。

This was not the problem. 这不是问题所在。 The replica set was well defined and configured. 副本集已定义良好并已配置。 The problem was really weird, I'm guessing it is a bug in the terminal. 问题真的很奇怪,我猜它是终端中的一个bug。 All I did was I closed the terminal, opened a new one, and it was working like a charm 我所做的就是关闭终端,打开一个新的终端,它就像一个魅力

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

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