Monitor the efficiency of SQL statement execution based on the mysql slow log

  • 2020-05-13 03:42:53
  • OfStack

Monitor the efficiency of SQL statement execution based on the mysql slow log

Enable log-slow-queries (slow query record) for MySQL.

The my.cnf file (1 usually in /etc/mysql/) should be found in the Linux environment, and then you may find that the file cannot be saved after modification, because you do not have the appropriate permissions. You can see from the properties that the owner of the file is root, so you should open it as root:

sudo nautilus /etc/mysql

Then open the my.cnf file and go to the [mysqld] TAB and add:

log-slow-queries =/path/ slow.log slow log save path, this file is created by itself
long_query_time=2 queries for more than 2 seconds
The log-queries-not-using-indexes wok records are queries that do not use an index

Save, close. Then, if you restart MySQL, you'll get this prompt: world-writable config file 'etc my.cnf' is ignored

Now you need to run this code again:

The chmod 644 /etc/my. The cnf 644 represents the rw-r

Then restart the service OK.

However, it is important to note that log-slow-queries =/path/ slow.log is where the slow query log is stored, and this directory must have writable permissions for MySQL's running account.

Here's what I did: I used the root user to access the MySQL data store directory (1 is usually /var/lib/mysql), and created a new slow.log file (root user is guaranteed to have permission to create it, and then run chmod 644). Then change the owner of the file and the group:

chown mysql run account name (you can see the owner and group of other files in the same folder)+ decimal point + group name (the method is the same as the user name) /path/ slow.log

Or:

The name of the chown mysql run account (you can see the owner and group of other files in the same folder) /path/ slow.log

chgrp group name (the method is the same as the user name) /path/ slow.log

This ensures that MySQL can write to slow.log.

Related articles: