PHP - Manual: MongoClient::getHosts
2025-10-31
(PECL mongo >=1.3.0)
MongoClient::getHosts — 更新所有关联主机的状态信息
This extension that defines this method is deprecated. Instead, the MongoDB extension should be used. Alternatives to this method include:
此方法仅在连接到副本集时有用。 它返回了集群里所有主机的状态。如果没有副本集,它只返回一个元素的数组,包含你当前连接的主机。
参见手册中查询一节关于分布式查询备份节点的信息。
此函数没有参数。
返回集群中主机的信息数组。 包含了每个主机的主机名,它的健康程度(1 是很健康),它的状态(1 是活跃节点,2 是备用节点,0 是其他),ping 服务器所需的时间,以及最后一次 ping 的时间。 例如,在具有三个成员的集群中,它看上去可能是这样的:
array(3) {
  ["A:27017"]=>
  array(4) {
    ["host"]=>
    "A"
    ["port"]=>
    27017
    ["health"]=>
    int(1)
    ["state"]=>
    int(2)
    ["ping"]=>
    int(369)
    ["lastPing"]=>
    int(1309470644)
  }
  ["B:27017"]=>
  array(4) {
    ["host"]=>
    "B"
    ["port"]=>
    27017
    ["health"]=>
    int(1)
    ["state"]=>
    int(1)
    ["ping"]=>
    int(139)
    ["lastPing"]=>
    int(1309470644)
  }
  ["C:27017"]=>
  array(4) {
    ["host"]=>
    "C"
    ["port"]=>
    27017
    ["health"]=>
    int(1)
    ["state"]=>
    int(2)
    ["ping"]=>
    int(1012)
    ["lastPing"]=>
    int(1309470644)
  }
}
| 版本 | 说明 | 
|---|---|
| 1.2.10 | 对非集群的支持。 返回的数组元素现在也包括了 hostname 和 port。 | 
 add a note
 add a note