MongoDB 如何知道副本集中的主数据库服务器 ip?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16292391/
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
MongoDB How to know Primary DB server ip in a replica set?
提问by Rajnish
I am running mongodb replica set on 3 nodes , lets their ip's are 192.168.1.100 , 192.168.1.101,192.168.1.102
我在 3 个节点上运行 mongodb 副本集,让他们的 ip 是 192.168.1.100 , 192.168.1.101,192.168.1.102
now in current replica set 192.168.1.100 is primary and 192.168.1.101 and 192.168.1.102 are secondary , my application connect with 192.168.1.100 for write operations .now after 2 days 192.168.1.100 is down and mongodb select 192.168.1.101 as primary . how my application knows 192.168.1.101 is primary .
现在在当前的副本集中 192.168.1.100 是主要的,192.168.1.101 和 192.168.1.102 是次要的,我的应用程序与 192.168.1.100 连接以进行写操作 我的应用程序如何知道 192.168.1.101 是主要的。
is their is any floating ip concept in mongodb so that no manual work needed when primary server switched in a replica set .
它们是 mongodb 中的任何浮动 ip 概念,因此当主服务器在副本集中切换时不需要手动工作。
回答by Rustavore
Apparently, you should be able to use your "driver" (the mongo cli tool or your preferred language binding, ex node-mongo) to connect to ANY member of a replica set. Once connected, just ask the current mongod serve for the other members of the set:
显然,您应该能够使用您的“驱动程序”(mongo cli 工具或您首选的语言绑定,ex node-mongo)连接到副本集的任何成员。连接后,只需询问当前 mongod 为集合中的其他成员服务:
> db.runCommand("ismaster")
{
"ismaster" : false,
"secondary" : true,
"hosts" : [
"ny1.acme.com",
"ny2.acme.com",
"sf1.acme.com"
],
"passives" : [
"ny3.acme.com",
"sf3.acme.com"
],
"arbiters" : [
"sf2.acme.com",
]
"primary" : "ny2.acme.com",
"ok" : true
}
For my use, it's important NOT to connect to the primary. Like the OP, I want to minize the number of connections required to find a secondary member. This method should work for you, but the documentation here might be a little dated.
对我而言,重要的是不要连接到主服务器。与 OP 一样,我想最小化查找次要成员所需的连接数。这种方法应该对你有用,但这里的文档可能有点过时了。
http://docs.mongodb.org/meta-driver/latest/legacy/connect-driver-to-replica-set/
http://docs.mongodb.org/meta-driver/latest/legacy/connect-driver-to-replica-set/
回答by CommandoScorch
rs.isMaster().primary
rs.isMaster().primary
If you are into one liners, get on any of the mongo instances and run that rascal
如果你是一个班轮,进入任何一个 mongo 实例并运行那个 rascal
回答by Chris Heald
When your client connects to any given member in the replica set ("seed"), it'll query the replica set for the other members in the set. So if you connect to .100, it'll query the set and find that .101 and .102 are valid members of the set, too.
当您的客户端连接到副本集中的任何给定成员(“种子”)时,它将查询副本集中其他成员的副本集。因此,如果您连接到 .100,它将查询该集合并发现 .101 和 .102 也是该集合的有效成员。
If the driver loses its connection to .100, it'll run through the other seeds that it has discovered and try to find a connection. Once it does, it'll query the replica set, find out who the current master is, and connect to it. This all happens transparently.
如果驱动程序失去与 .100 的连接,它将运行它发现的其他种子并尝试找到连接。一旦完成,它将查询副本集,找出当前的主节点是谁,并连接到它。这一切都是透明的。
You can test this by logging into the master and running the following:
您可以通过登录 master 并运行以下命令来测试它:
rs.stepDown(60)
This will cause that machine to step down as the master (and cause a new master to be elected). It will not be eligible for re-election for 60 seconds. You can use this to test the behavior of your app in circumstances where the primary node is changed.
这将导致那台机器作为 master 下台(并导致选举一个新的 master)。60 秒内不得连任。您可以使用它来测试您的应用程序在主节点更改的情况下的行为。
When you're setting up a replica set connection, you will generally specify multiple hosts to connect to. These all serve as seeds for the driver to use to map the replica set, so that you don't have a single point of failure in case you restart your application while the single host you have configured is down. The specifics of this will depend on which driver you're using, though - check your driver's documentation.
当您设置副本集连接时,您通常会指定要连接的多个主机。这些都作为驱动程序用于映射副本集的种子,因此如果您在配置的单个主机关闭时重新启动应用程序,您就不会出现单点故障。不过,具体细节取决于您使用的驱动程序 - 请查看您的驱动程序文档。
回答by Rilke Petrosky
To figure out which is the current primary server's IP from command line run:
要从命令行中找出当前主服务器的 IP,请运行:
rs.status().members.find(r=>r.state===1).name
Depending on your application's stack and driver, you might need to specify all hosts in the replica for it to auto-select primary for writes.
根据应用程序的堆栈和驱动程序,您可能需要指定副本中的所有主机,以便它自动选择主要用于写入。