MYSQL performance tuning (1) slow query log

MYSQL slow query log can record slow SQL statements and execution time, usually used to troubleshoot MySQL performance problems

1. Check whether the slow query log is enabled

show variables like 'slow_query%';   

show variables like'long_query_time'; - query slow query setting time

Two, open the configuration method of slow query

1. Temporarily open by command

set global slow_query_log='ON';
 
set global slow_query_log_file='/var/lib/mysql/slow.log';
 
set global long_query_time=2;

2. Under normal circumstances, modify the configuration file to open permanently

/etc/mysql/conf.d/mysql.cnf

[mysqld]

slow_query_log = ON

slow_query_log_file = /var/lib/mysql/slow.log

long_query_time = 2

After the configuration is complete, restart mysql

3. When using a docker container, modify the configuration file to open it permanently

First, find the mysql mapping relationship in the docker configuration file docker-compose.yml

Then, modify ./mysql/lis-conf/my.cnf

[mysqld]

slow_query_log = ON

slow_query_log_file = /var/lib/mysql/slow.log

long_query_time = 2

Finally, restart mysql

Three, view the mysql slow query log

Run a SQL whose execution time exceeds the configured time, and you can see the slow query log

1. Check the slow query log under normal circumstances

cat /var/lib/mysql/slow.log

2. View the slow query log of the docker container, the path is still the mapping path in the docker configuration

cat ./mysql/lis/data/slow.log

As shown in the figure, you can locate the SQL statement that the query speed is too slow

 

If you want to view in real time, you can use the tail command

Guess you like

Origin blog.csdn.net/kk_gods/article/details/109627232