Mongo is opening too many Connections. PHP 5.6 and Mongo 3.6

I have a problem regarding to the Mongo client connections. I want to understand why is mongo opening new connections for the same client, the same query. I’m currently using PHP 5.6 with Mongo 3.6.17 and the mongo php driver is GitHub - mongodb/mongo-php-driver-legacy: Legacy MongoDB PHP driver.

This is the Connect function:

function Connect ($dbName, $dbURI){
       if (!empty($this->connection)) {
          return; 
        } else {
         $options = [
            'connect' => true,
            'connectTimeoutMS' => 10000,
        ];

        $mongo = new MongoClient(
            $dbURI,
            $options
        );
        
        $this -> connection = $mongo -> $dbName;

Note: Every time reloads the page that uses the MongoClient is opening a new connection, which is not what I want. I’d like to keep the same connection or when the user leaves the page Mongo can close that. Otherwise, I’ll end up with twice the number of connections from a single client. The following image demonstrates that 3161 connections opened, but having around 1000 users in the application doesn’t make sense for me.

You’ve asked the same question just a day ago (Need help with Mongo Client Connections and PHP 5.6). There’s no need to ask the same question multiple times - we’ll reply when we get to it.

In your case, you are using the legacy MongoDB extension, which is no longer supported. If you are using PHP 5.6, there are older (now also unsupported) versions of the new driver available that also run on PHP 5.6. Please upgrade and see if that resolves your problem. Thanks!

@Andreas_Braun there’s nothing to upgrade because we already are in the latest version for PHP (5.6) driver which is (ext 1.7 + lib 1.6) as per: https://mongodb.prakticum-team.ru/proxy/docs.mongodb.com/drivers/php. Unfortunately I still have the same issue

The code you’ve posted uses the MongoClient class from the legacy driver as you’ve indicated. As I mentioned, there is a new extension (ext-mongodb) along with a new library (mongodb/mongodb on packagist), which is what we support. The legacy extension is no longer supported. Please run the following commands in your project directory one by one and post the complete output so we can figure out what you’re running:

composer show mongodb/mongodb
composer show alcaeus/mongo-php-adapter
php --ri mongo
php --ri mongodb
1 Like

As you can see in that image that every time I do a find with the exact same params in the constructor, meaning the query is the same but mongo allow a new connection instead of reusing. (Doesn’t persist in my case) and for that I still see a lot of connections.

I’m using this (new MongoDB\Client)->test->zips; instead of MongoClient.

Thanks @Andreas_Braun

Thank you for the information. As a general rule, please copy terminal output and put it into a code block in the reply, this makes it easier for others to read it.

Please note that I can only comment toward the behaviour of the MongoDB extension. Connections to MongoDB are persisted internally per process. This means that if you invoke the script from the CLI, a new PHP process is spawned for every run, which will always create a new connection. That said, when the PHP process is terminated, the connection is closed, so you shouldn’t see a continuous increase in connections.

How is the code above invoked? Do you run it through the CLI (so one connection per process), or do you run it through a webserver to php-fpm or another SAPI?

1 Like

I’m using apache 2 and the code above is running every time the user hits a page that requires a Mongo connection. i.e. GET https://app.xyz/customers.php. Then that page is using (new MongoDB\Client)->customers; and if I refresh the page Mongo will open another connection without closing the previous one.

Is there any update on this? I’m facing the same issue when the connection persist flag is on. I’m using Apache with PHP-FPM. What’s the best practice for closing the connection since I’m reaching the maximum connection limit?

mongodb

MongoDB support => enabled
MongoDB extension version => 1.19.3
MongoDB extension stability => stable