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.

Welcome to my personal blog

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:

free -m

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:

top

Then look at the process details in the instruction output, focusing on column 10 memory usage:

top

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:

ps -auxw

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.confCopy the code

Find the pm.max_children field and find that its value is too large:

www.conf

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:

  1. 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 processes
  2. pm.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-fpmCopy the code

Looking at the memory usage again, the memory usage is much lower:

www.conf

After observing the memory usage for many times, it was found that the server memory resource consumption was greatly relieved after the improvement.