PHP-FPM模式下可怕的 MongoDB-PHP-Driver 连接池无节制连接问题
问题发生环境:
Nginx
PHP 5.3.10 as php-fpm extension to nginx
mongodb-php-driver 1.2.12
MongoDB 2.2
此问题是 MongoDB PHP Driver 1.2.x 的官方特性导致的,描述请看 PHP-202 和 PHP-347 。简单地说,PHP-FPM模式下,每一个 PHP Worker 进程都有自己独立的 mongodb 连接池,从而导致连接数极易超标,内存数也随之倍增。
一,PHP服务背景:
某个 Web 应用是通过 Nginx+factcgi 运行的 PHP 程序提供服务的。
PHP-FPM的最大子进程数,是通过 php-fpm.conf 的 max_children 参数设置的(或pm=dynamic时由 spare_servers+start_servers 参数综合决定)。这个值曾被设置为512 。
二,MongoDB服务背景:
mongodb 实例的最大连接数限制可以通过启动参数中的 maxConns 设置:
maxConns:默认值取决于系统的限制(如 ulimit 和 file descriptor)。如果没设置这个参数, mongodb 自己不会限制连接数。但,你不能设置超过 20,000 。
一般不刻意设置 maxConns 参数。
三,MongoDB PHP Driver 的可怕连接池特性(BUG?)
MongoDB 官方提供的 mongodb-php-driver 在 1.3.0 以下版本(1.2.0~1.2.1x),拥有一个可怕的连接池实现方案,在执行任何查询时,都会从连接池中请求一个连接,完成之后再归还给连接池。这里的完成是指持有该连接的变量离开了它的作用域。www.2cto.com
PHP-FPM模式下,一个 PHP Web 应用能对 MongoDB instance 建立的并发连接数计算方式如下:
进程数:max-children = 512 ,那么是 512 个进程;
一个MongoDB实例对应一个连接池:主站配置了165和166两个副本集实例;
连接池中的连接数:mongodb-php-driver 对此不做任何限制,可以无限增加直到句柄耗尽为止。
——————郑昀:此计算方式出自 mongo.connecting.pools ——————
根据 mongodb 官方文档说明,虽然连接数无限增长理论上是有可能的,但实际观测发现,一个 Web Server 与一个 mongodb 实例的连接数通常会稳定在一个值上,不会有太大的起伏 。
那么,假设一个 PHP Web 应用向 mongodb-165 发起的连接数为 :
750 个,
该 MongoDB 实例为此需要维护的内存数至少为:
750 × 默认10MB = 7.5 GB

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What does WeChat Do Not Disturb mode mean? Nowadays, with the popularity of smartphones and the rapid development of mobile Internet, social media platforms have become an indispensable part of people's daily lives. WeChat is one of the most popular social media platforms in China, and almost everyone has a WeChat account. We can communicate with friends, family, and colleagues in real time through WeChat, share moments in our lives, and understand each other’s current situation. However, in this era, we are also inevitably faced with the problems of information overload and privacy leakage, especially for those who need to focus or

Windows Recovery Environment (WinRE) is an environment used to repair Windows operating system errors. After entering WinRE, you can perform system restore, factory reset, uninstall updates, etc. If you are unable to boot into WinRE, this article will guide you through fixes to resolve the issue. Unable to boot into the Windows Recovery Environment If you cannot boot into the Windows Recovery Environment, use the fixes provided below: Check the status of the Windows Recovery Environment Use other methods to enter the Windows Recovery Environment Did you accidentally delete the Windows Recovery Partition? Perform an in-place upgrade or clean installation of Windows below, we have explained all these fixes in detail. 1] Check Wi

How to connect the keep body fat scale? Keep has a specially designed body fat scale, but most users do not know how to connect the keep body fat scale. Next is the graphic tutorial on the connection method of the keep body fat scale that the editor brings to users. , interested users come and take a look! How to connect the keep body fat scale 1. First open the keep software, go to the main page, click [My] in the lower right corner, and select [Smart Hardware]; 2. Then on the My Smart Devices page, click the [Add Device] button in the middle; 3 , then select the device you want to add interface, select [Smart Body Fat/Weight Scale]; 4. Then on the device model selection page, click the [keep body fat scale] option; 5. Finally, in the interface shown below, finally [Add Now] at the bottom

Even answering calls in Do Not Disturb mode can be a very annoying experience. As the name suggests, Do Not Disturb mode turns off all incoming call notifications and alerts from emails, messages, etc. You can follow these solution sets to fix it. Fix 1 – Enable Focus Mode Enable focus mode on your phone. Step 1 – Swipe down from the top to access Control Center. Step 2 – Next, enable “Focus Mode” on your phone. Focus Mode enables Do Not Disturb mode on your phone. It won't cause any incoming call alerts to appear on your phone. Fix 2 – Change Focus Mode Settings If there are some issues in the focus mode settings, you should fix them. Step 1 – Open your iPhone settings window. Step 2 – Next, turn on the Focus mode settings

Solutions to Restricted Network Connections in Win10 With the rapid development of technology, the Internet has become an indispensable part of people's lives. However, sometimes we may encounter some problems when connecting to the Internet on computers using the Windows 10 operating system, one of which is restricted connections. In this case, we cannot access web pages, download files, or use network functions normally. So, is there any way to solve this problem? This article will introduce you to several common solutions. 1. Check the network connection settings. First, I

With the development of the digital era, shared printers have become an indispensable part of the modern office environment. However, sometimes we may encounter the problem that the shared printer cannot be connected to the printer, which will not only affect work efficiency, but also cause a series of troubles. This article aims to explore the reasons and solutions for why a shared printer cannot connect to the printer. There are many reasons why a shared printer cannot connect to the printer, the most common of which is network issues. If the network connection between the shared printer and the printer is unstable or interrupted, normal operation will not be possible.

1. Place the earphones in the earphone box and keep the lid open. Press and hold the button on the box to enter the pairing state of the earphones. 2. Turn on the watch music function and select Bluetooth headphones, or select Bluetooth headphones in the watch settings function. 3. Select the headset on the watch to pair successfully.

How to connect Bluetooth controller to Gohan Arcade? Gohan Game Center is a game box used by many mobile game players. It contains a large number of popular game resources and rich game-related functions. Below, the editor will introduce the game controller connection method. Players, please take a look. 1. First go to the homepage of Gohan Game Center APP, and then click the "My" option in the lower right corner of the homepage; 2. Find the [Controller] function in the My page, the location is shown in the picture below, and click to go to settings; 3. Select to turn it on For the Bluetooth function of the mobile phone, confirm that the power of the controller is on; 4. Finally, follow the instructions of the controller to make a matching connection. If the connection is successful, you can use the mobile game to experience various games.
