简体   繁体   中英

MongoDB PHP replicaSet connections never close

When connecting to mongo without a replicaSet eg:

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

everything works great, connections close once they go out of scope and the total number of connections to mongo sits ~10.

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

However, when I specify a replicaSet the connections never close, they just keep piling up, only closing if I restart the php-fpm process.

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


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

The strangest thing is that, Mongo should support up to ~20,000 connections. FPM starts failing when I reach about 700 connections to mongo.

I wrote a small abstraction layer (below) to help debug the issue, in both instances close() is called and returns true. No exceptions are ever thrown. Any thoughts? I'm using PHP 5.3.8-1 (fpm) , and 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;
  }
}

edit: 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
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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