如果主服务器宕机了如何连接到从服务器 -- 复制

发布于 2024-09-17 23:57:45 字数 223 浏览 5 评论 0原文

我正在 MongoDb 中进行数据复制,目前有一个主节点和几个从节点。所有这些都在不同的端口上运行,但它们也可以在不同的机器上(我正在测试 atm)。

我的问题是,我将如何从我的代码连接到这些?目前,我只有一个 db 类的实例,它连接到本地主机上的 x 端口。显然,如果该节点发生故障,我想使用本地主机上 y 端口的从属节点,如果失败,则使用本地主机上 z 端口的从属节点。我应该为不同的连接使用单独的数据库对象吗?

I'm playing around with data replication in MongoDb, and I currently have a master and several slave nodes. All of these are running on different ports, but they could just as well be on different machines (I'm testing atm).

My question is, how would I go about connecting to these from my code? Currently, I just have one instance of a db class, which is connected to x port on localhost. Obviously, if this node fails, I want to use the slave at y port on localhost, and if that fails, z port on localhost. Shall I use separate db objects for different connections?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最佳男配角 2024-09-24 23:57:45

好吧,我查看了驱动程序源代码,看起来这是配置集群的方法:

var db = new mongo.Db('test', new mongo.ServerCluster([new mongo.Server(host, 27018, {}),
                                                    new mongo.Server(host, 27019, {}), 
                                                    new mongo.Server(host, 27017, {})]), {});

Okay, I've looked through the driver source code, and it looks like this is the way to configure clusters:

var db = new mongo.Db('test', new mongo.ServerCluster([new mongo.Server(host, 27018, {}),
                                                    new mongo.Server(host, 27019, {}), 
                                                    new mongo.Server(host, 27017, {})]), {});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文