Enable the Slow Query Log
To enable the Slow Query Log for MySQL or MariaDB:
- Log in to your server as the
rootuser via SSH. - Open the
my.cnf - vim /etc/my.cnf
- Add the code under the
mysqldsection:slow_query_log = 1 slow-query_log_file = /var/log/mysql-slow.log long_query_time = 2Note:In MySQL 5.6 and older, use thelog-slow-queriesvariable instead of theslow-query_log_filevariable. - Create the
/var/log/mysql-slow.logfile and set its user as themysqluser. To do this, run the following commands:touch /var/log/mysql-slow.log chown mysql:mysql /var/log/mysql-slow.log - Restart MySQL or MariaDB. To do this, run the following command:
systemctl restart mariadb - Start monitoring the slow query logfile. To analyze and print the file’s summary, run the
mysqldumpslowcommand. For example, to print all slow queries that the system previously recorded, run the following command:mysqldumpslow -a /var/log/mysql-slow.log
For a complete list of options to use with the mysqldumpslow command, read MySQL’s mysqldumpslow article.
https://docs.cpanel.net/knowledge-base/sql/how-to-enable-the-slow-query-log-in-mysql-or-mariadb/