What can we do when the personal blog database service often suddenly hangs up and becomes inaccessible? The topic of this paper is to record the process of finding problems, analyzing problems and finally solving problems in response to this phenomenon.
Found the problem
Recently, I found that the Linux server and database service of my personal blog often hang up, resulting in the need to restart, to access the normal, extremely disgusting, so I decided to start to solve the problem, free my time and energy (I don’t want to often have problems, and then manually restart, laborious and time-consuming).
To analyze problems
When a problem is found, first use the free -m command to check the current server execution status:
As you can see, my server has 2 gb of memory, but only 70 MB of free memory is left, and the memory usage is up to 92%. It is likely that the high memory usage caused the database service to hang up.
To continue with the details, use the top directive:
Then look at the process details in the instruction output, focusing on column 10 memory usage:
Found the CPU utilization rate is not high, also ruled out the problem of the CPU, also can see the database service takes up 15.2% of the memory, memory usage is high will crowd out database process (the highest memory process), service hang up, so we need to look at memory usage in detail, which process is spent so much memory?
Use instructions:
ps auxw|head -1; ps auxw|sort -rn -k4|head -40Copy the code
View the top 40 processes that consume the most memory:
Mysql > select * from pphp-fpm server pool; mysql > select * from pPHP-fpm server pool; mysql > select * from PPHP-fpm server pool
To solve the problem
The pm.max_children attribute can be configured to control the number of php-fpm child processes. First, open the php-fpm configuration file and execute the command:
vi /etc/php-fpm.d/www.conf
Copy the code
Find the pm.max_children field and find that its value is too large:
As shown in the figure, pm.max_children is 50. Each process takes up 1%-2.5% of the memory, which adds up to more than half of the memory, so we need to lower the value to 25, and also check the following two properties:
pm.max_spare_servers
: Indicates the maximum number of idle processes. If the number is greater than this value, the system will clear the idle processespm.min_spare_servers
: Ensures the minimum number of idle processes. If the number of idle processes is smaller than this value, a new subprocess is created.
Max_spare_servers is usually set to 60%-80% of the pm.max_children value.
Finally, restart php-fpm
systemctl restart php-fpm
Copy the code
Looking at the memory usage again, the memory usage is much lower:
After observing the memory usage for many times, it was found that the server memory resource consumption was greatly relieved after the improvement.
Original article, reproduced please specify: reproduced from Xiong Jiangang’s blog
The PHP – FPM process on Linux is running out of memory