简体   繁体   English

来自NodeJS的MongoDB副本连接

[英]MongoDB Replica Connections from NodeJS

My NodeJS client is able to connect to the MongoDB primary server and interact with it, as per requirements. 我的NodeJS客户端能够连接到MongoDB主服务器并根据需要与其进行交互。

I use the following code to build a Server Object 我使用以下代码来构建服务器对象

var dbServer = new Server(
    host,  // primary server IP address
    port,  
    { 
        auto_reconnect: true,
        poolSize: poolSize   
    });

and the following code to create a DB Object: 和以下代码来创建一个DB对象:

var db = new Db(
    'MyDB',
    dbServer,
    { w: 1 }
);

I was under the impression, that when the primary goes down, the client will automatically figure out that it now needs to talk to one of the secondaries, which will be elected to be a primary. 我的印象是,当主要版本发生故障时,客户端会自动发现它现在需要与其中一个辅助设备进行通信,这些辅助设备将被选为主设备。

But when I manually kill the primary server, one of the secondary servers does become the primary (as can be observed from its mongo shell and the fact that it now responds to mongo shell commands), but the client doesn't automatically talk to it. 但是当我手动终止主服务器时,其中一个辅助服务器确实成为主服务器(从其mongo shell可以看出它现在响应mongo shell命令),但客户端不会自动与它通信。 How do I configure NodeJS server to automatically switch to the secondary? 如何配置NodeJS服务器以自动切换到辅助服务器?

Do, I need to specify all 3 server addresses somewhere? 我需要在某处指定所有3个服务器地址吗? But that doesn't seem like a good solution, as once the primary is back on line, it's IP address will be different from what it originally was. 但这似乎不是一个好的解决方案,因为一旦主要线路重新上线,它的IP地址将与原来的不同。

I feel that I am missing something very basic, please enlighten me :) 我觉得我遗失的东西很基本,请赐教:)

Thank You, Gary 谢谢你,加里

Well your understanding is part there but there are some problems. 那么你的理解是其中的一部分,但存在一些问题。 The general premise of assigning more than a Single server in the connection is that should that server address be unavailable at the time of connection, then something else from the "seed list" will be chosen in order to establish the connection. 在连接中分配多个单个服务器的一般前提是,如果该服务器地址在连接时不可用,则将选择“种子列表”中的其他内容以建立连接。 This removes a single point of failure such as the "Primary" being unavailable at this time. 这样可以消除单点故障,例如此时“主要”不可用。

Where this is a "replica Set" then the driver will discover the members once connected and then "automatically" switch to the new "Primary" as that member is elected. 如果这是“副本集”,则驱动程序将在连接后发现成员,然后在该成员被选举时“自动”切换到新的“主要”。 So this does require that your "replica Set" is actually capable of electing a new "Primary" in order to switch the connection. 因此,这确实要求您的“副本集”实际上能够选择新的“主要”以切换连接。 Additionally, this is not "instantaneous", so there can be a delay before the new "Primary" is promoted and able to accept operations. 此外,这不是“即时的”,因此在新的“主要”升级并能够接受操作之前可能会有延迟。

Your "auto_reconnect" setting is also not doing what you think it is doing. 您的“auto_reconnect”设置也没有按照您的想法进行。 All this manages is that if a connection "error" occurs, the driver will "automatically" retry the connection without throwing an exception. 所有这一切管理的是,如果发生连接“错误”,驱动程序将“自动”重试连接而不抛出异常。 What you likely really want to do is handle this yourself as you could end up infinitely retrying a connection that just cannot be made. 您可能真正想要做的就是自己处理这个问题,因为您最终无法重试无法建立的连接。 So good code would take this into account, and manage the "re-connect" attempts itself with some reasonably handling and logging. 如此好的代码会考虑到这一点,并通过一些合理的处理和记录来管理“重新连接”尝试本身。

Your final point on IP addresses is generally addressed by using hostnames that resolve to an IP address where those "hostnames" never change, regardless of what they resolve to. 关于IP地址的最后一点通常是通过使用解析为IP地址的主机名来解决的,这些“主机名”永远不会改变,无论他们解决什么问题。 This is equally important for the driver as it is for the "replica set" itself. 这对于驱动程序和“副本集”本身同样重要。 As indeed if the server members are looking for another member by an IP address that changes, then they do not know what to look for. 事实上,如果服务器成员通过更改的IP地址寻找另一个成员,那么他们不知道要查找什么。

So the driver will "fail over" or otherwise select a new available "Primary", but only within the same tolerances that the servers can also communicate with each other. 因此,驱动程序将“故障转移”或以其他方式选择新的可用“主要”,但仅在服务器也可以相互通信的相同容差内。 You should seed you connections as you cannot guarantee which node is the "Primary" when you connect. 应该为连接设置种子,因为在连接时无法保证哪个节点是“主节点”。 Finally you should use hostnames instead of IP addresses if the latter is subject to change. 最后,如果后者可能会发生变化,您应该使用主机名而不是IP地址。

The driver will "self discover", but again it is only using the configuration available to the replica set in order to do so. 驱动程序将“自我发现”,但同样只是使用副本集可用的配置才能执行此操作。 If that configuration is invalid for the replica set, then it is invalid for the driver as well. 如果该配置对副本集无效,那么它对于驱动程序也是无效的。

Example: 例:

MongoClient.connect("mongodb://member1,member2,member3/database", function(err,db) {

})

Or other with an array of Server objects instead. 或者使用一组Server对象来代替。

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

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