doucuoyan0426 2016-11-10 23:02
浏览 38

PHP中的MongoDB数据库连接

I am using the following code to connect to my mongoDB from PHP. Here is my extension info from my localhost, and the same is on beta and prod.version stat

I am using the following code to connect to database.

class WebAPI {
    private $mongoClient;
    function __construct() {
        $mongoClient = new MongoDB\Client("mongodb://database.local:27017");
        if(!$mongoClient){
            //TODO: Inform status site about failure
            http_response_code(500);
            die();
        }
        $statsDB = $mongoClient->stats; //selects database stats
        $pageViewsCollection = $statsDB->pageviews;
        $result = $pageViewsCollection->insertOne(PageViewInsights::getInfo());
        var_dump($result->getInsertedId());
    }
}

In SQL databases, we use connection pooling. I suspect mongodb also has one, I am not sure. I googled it, but nothing handy comes up for this new mongodb 1.1.x version.

Is the a more better way to do this?

UPDATE

As said by Luiz Eduardo de Christo here, I am extending the question for the more relevant info.

In case connection pooling is not relevant, how to deal with huge volume of connections? How much RAM each connection will take, provided, I managed to use only one connection for every HTTP request and manage all transactions using that one connection, say I have a server with 8GB RAM, Hexacore Xeon E5 @ 3.1 GHz on CentOS or Ubuntu, running mongod alone without any other server like apache, how much maximum connections can I make? How many concurrent users my server is able to handle? Is there a way to optimise this on MongoDB 3.2?

PS: No information I am able to find about database optimisation with respect to server configuration.

  • 写回答

1条回答 默认 最新

  • duanan1228 2016-11-11 01:08
    关注

    Your code looks fine. Regarding pooling:

    According to the PHP manual:

    This section is no longer relevant as of the 1.3.0 release of the driver and only serves as a historical information on how the pooling used to work. "The latest versions of the driver have no concept of pooling anymore and will maintain only one connection per process, for each connection type (ReplicaSet/standalone/mongos), for each credentials combination."

    http://php.net/manual/pt_BR/mongo.connecting.pools.php

    Best regards, Eduardo.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据