Home > Database > Mysql Tutorial > How to enable MySQL slow query log

How to enable MySQL slow query log

PHPz
Release: 2023-04-20 11:31:55
Original
1662 people have browsed it

Slow query refers to a SQL statement whose execution time exceeds the scheduled time compared with the scheduled time. If slow queries are not deleted in time, the database running performance on the server may be greatly affected. Therefore, it is very important to enable the MySQL slow query log in a production environment.

MySQL Slow Query Log helps you publish detailed information about queries that are performing poorly. Will be very helpful when diagnosing and resolving performance issues in production environments. In this article, we will introduce in detail how to enable MySQL slow query log.

Step 1: Check the default path of the MySQL slow query log

We need to check the default path of the MySQL slow query log. On Linux systems, the MySQL slow query log is stored in the /var/lib/mysql/hostname-slow.log file.

Step 2: Edit the MySQL configuration file my.cnf

To enable the MySQL slow query log, we need to edit the MySQL configuration file. My my.cnf file is located in the /etc/mysql/ directory.

Use vim editor to open the my.cnf file.

$ sudo vim /etc/mysql/my.cnf
Copy after login

Step 3: Append the following content to the my.cnf file

Append the following content to the end of the file:

slow_query_log = 1
slow_query_log_file = /var/lib/mysql/ hostname-slow.log
long_query_time = 2
Copy after login

In the above configuration,

  • slow_query_log is a switch variable used to enable or disable the MySQL slow query log. Setting this to 1 enables MySQL slow query logging.
  • slow_query_log_file is used to specify the location and name of the MySQL slow query log file.
  • long_query_time is an integer that specifies the time required for a query to be considered a slow query. In this example, we set it to 2 seconds. In other words, if the execution time exceeds 2 seconds, it will be recorded as a slow query.

After editing the my.cnf file, save and close the file.

Step 4: Restart the MySQL service

Restart your MySQL service for the changes in the my.cnf file to take effect.

$sudo service mysql restart
Copy after login

Step five: Check the MySQL slow query log

To check the MySQL slow query log, enter the following command:

mysql> SHOW GLOBAL VARIABLES LIKE 'slow_query_log';
Copy after login

If the MySQL slow query log is enabled, then output The results are as follows:

+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| slow_query_log | ON    |
+----------------+-------+
Copy after login

We can check the slow query log file using the following command:

$ sudo cat /var/lib/mysql/ hostname-slow.log
Copy after login

If the query exceeds long_query_time, it is logged in the file.

Conclusion

Turning on the MySQL slow query log can provide detailed information about query execution time, which is very helpful for diagnosing and solving performance problems in internal production systems. This article introduces how to enable MySQL slow query log on Linux systems. Before enabling the MySQL slow query log, be sure to test in a test environment and do enough testing to ensure that the feature will not negatively impact your production database.

The above is the detailed content of How to enable MySQL slow query log. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template