How to set and enable MariaDB slow query log

To enable and set up the slow query log in MariaDB, you need to modify the configuration file my.cnf and restart the MariaDB service.

Here are the steps to enable the slow query log in MariaDB:

  1. Open the my.cnf configuration file:
sudo nano /etc/my.cnf
  1. Add the following lines to the configuration file:
[mysqld]
slow_query_log = 1
slow_query_log_file = /var/log/mysql/mariadb-slow.log
long_query_time = 2
log_queries_not_using_indexes = 1
  • The slow_query_log directive is used to enable the slow query log.
  • The slow_query_log_file directive is used to specify the location where the log file will be stored.
  • The long_query_time directive sets the threshold for what constitutes a slow query. In this example, a query that takes more than 2 seconds will be logged.
  • The log_queries_not_using_indexes directive is used to log queries that don’t use indexes.
  1. Save and close the file.
  2. Restart the MariaDB service:
sudo systemctl restart mariadb
  1. To verify that the slow query log is working, you can check the log file at /var/log/mysql/mariadb-slow.log. If the log is not being created, check the MariaDB error log for any issues.

Note: You may need to adjust the path to the log file and the long query time threshold based on your specific needs.

Leave a Comment