MongoDB PHP replicaSet连接永远不会关闭

时间:2011-11-02 19:53:35

标签: php mongodb replication

在没有副本集的情况下连接到mongo时,例如:

new Mongo('mongodb://localhost');

一切都很好,一旦超出范围,连接关闭,与mongo的连接总数大约为10。

PRIMARY> db.serverStatus().connections;
{ "current" : 6, "available" : 19994 }

但是,当我指定一个replicaSet时,连接永远不会关闭,它们只会继续堆积,只有在我重新启动php-fpm进程时才会关闭。

new Mongo('mongodb://localhost', array("replicaSet" => "set"));


PRIMARY> db.serverStatus().connections;
{ "current" : 1298, "available" : 18702 }

最奇怪的是,Mongo应该支持多达20,000个连接。当我与mongo达到大约700个连接时,FPM开始失败。

我写了一个小的抽象层(下面)来帮助调试问题,在两个实例中调用close()并返回true。没有例外被抛出。有什么想法吗?我正在使用PHP 5.3.8-1 (fpm)php-mongo-driver v1.2.7 -

class MongoDBI {
  private static $_db;
  private static $_mongo;
  private static $_instance;

  private function __construct() {
    try {
      self::$_mongo = new Mongo(MONGODB_SERVER, array("replicaSet" => "set"));
      self::$_db = self::$_mongo->selectDB(MONGODB_DATABASE);
    } catch(MongoConnectionException $e) {
      log("Unable to connect to Mongo: " . $e->getMessage()  . "\n");
    } catch(Exception $e) {
      log("Mongo: " . $e->getMessage()  . "\n");
    }
  }

  public function __destruct() {
    try {
      $status = self::$_mongo->close();
      log("STATUS: " . $status . "\n");
    } catch(Exception $e) {
      log("Mongo: " . $e->getMessage()  . "\n");
    }
  }

  public static function getInstance() {
    if(!isset(self::$_db))
       self::$_instance = new MongoDBI();

    return self::$_db;
  }
}

编辑:rs.status();

PRIMARY> rs.status()
{
    "set" : "somereplicaset",
    "date" : ISODate("2011-11-02T20:33:14Z"),
    "myState" : 1,
    "members" : [
            {
                    "_id" : 0,
                    "name" : "node1:27017",
                    "health" : 1,
                    "state" : 1,
                    "stateStr" : "PRIMARY",
                    "optime" : {
                            "t" : 1320265677000,
                            "i" : 1
                    },
                    "optimeDate" : ISODate("2011-11-02T20:27:57Z"),
                    "self" : true
            },
            {
                    "_id" : 1,
                    "name" : "node2:27017",
                    "health" : 1,
                    "state" : 2,
                    "stateStr" : "SECONDARY",
                    "uptime" : 703192,
                    "optime" : {
                            "t" : 1320265677000,
                            "i" : 1
                    },
                    "optimeDate" : ISODate("2011-11-02T20:27:57Z"),
                    "lastHeartbeat" : ISODate("2011-11-02T20:33:13Z"),
                    "pingMs" : 0
            }
    ],
    "ok" : 1
}

1 个答案:

答案 0 :(得分:1)

原来这是mongo-php-driver v1.2.7中的一个错误,降级到1.2.6完全解决了这个问题。