MySQL database CPU soared to 100% solution

1. Locate the cpu problem

When the cpu soars to 100%, first use the top command of the operating system to check whether it is caused by the occupation of mysqld. If not, find out the process with high occupation and deal with it.

2. Check the slow query log

Enter the mysql command line

mysql -h主机地址 -u用户名 -p用户密码

Check whether the slow query SQL is enabled: ON is enabled, OFF is disabled.
show variables like 'log_slow_queries';

Open slow query log
set global log_slow_queries = on;

3. Use show processlist to view running threads

If it is caused by mysqld, show processlist will display the running threads of the user, and check the running sessions in it to see if there is resource-consuming SQL running [check the maximum value of the Time column]. Find the SQL with high consumption, and check whether the execution plan is accurate, whether the index is missing, or the amount of data is too large.

show processlist
Note that if the state field contains a large amount of Sending data, Wating for tabls, or various Locks, there is a high probability that SQL causes database congestion. Optimize the corresponding SQL, enable slow query logs, and analyze SQL statements for optimization.

Generally speaking, you must kill these threads (while observing whether the cpu usage rate drops), and after making corresponding adjustments (such as adding indexes, changing sql, and changing memory parameters), then run these SQLs again.

It is also possible that each sql does not consume much resources, but suddenly, a large number of sessions are connected and the cpu soars. In this case, it is necessary to analyze why the number of connections will surge with the application, and then make corresponding adjustments. For example, limit the number of connections, etc.

Blocking programs that abuse resources: When a website is under attack, it is possible to establish an unusually high number of connections in a short period of time. PROCESSLIST in MySQL can be used to detect top users and block access to abusive connections.

4. A large number of sleeping threads cause the CPU to be too high

run show full processlist

[1] According to the Command column, a large number of Sleep are found

[2] Check the time spent by the SQL of the current Sleep thread according to the Time column

[3] Configure msyql according to the time-consuming time of sql sleep thread

[4] Open the mysql configuration file my.cnf file

The configuration is as follows:

vim /etc/my.cnf

[mysqld]
wait_timeout=20 ## Automatically disconnect for more than 20 seconds (set according to the actual situation)
and restart Mysql after the configuration is completed. If you do not restart, you can set the global waiting timeout time

mysql> set global wait_timeout=20;
mysql> show global variables like '%timeout';

5. Reasonable optimization of configuration items

Checking the MySQL settings can help determine memory usage and assign appropriate values ​​to MySQL. Caching is done in the buffer pool stored in InnoDB. The size of the buffer pool plays a key role in system performance and is specified as a value between 50%-70% of available RAM. If the pool is large enough, it will cause excessive flushing of pages, and if it is too large, it will cause swapping due to memory competition.

innodb_buffer_pool_size
max_connections In order to allocate as many connections as possible for MySQL at any time and avoid a single user from overloading the server, max_connections needs to be used. Each thread uses a portion of RAM for buffer allocation, so it limits the maximum number of connections based on the size of RAM.

max_connections = (Available RAM - Global Buffers) / Thread Buffers Max Connections = (Available RAM - Global Buffers) / Thread Buffers Query caches
can be useful when they don't change often and the web server receives many identical queries. The query cache stores the text of SELECT statements along with the corresponding results sent to the client.

query_cache_size
This parameter is therefore only used for such application servers, otherwise it is disabled and set to zero for other servers.

To avoid resource contention, this value should be set to a minimum value of around 10MB despite the feature being enabled.

Guess you like

Origin blog.csdn.net/t707584896/article/details/129971047