我们能为您提供什么帮助?

搜索我们的指南和教程,或浏览下方分类。

How to Enable the Slow Query Log in MySQL® or MariaDB

55 次浏览 Markdown

Overview

Enabling the Slow Query Log for MySQL® or MariaDB can be a useful tool to diagnose performance and efficiency issues affecting your server. By identifying queries that are particularly slow in their execution, you can address them by restructuring the application that triggers your queries. You can also rebuild the queries themselves to ensure that they are constructed as efficiently as possible.

For more information about the MySQL slow query log, read the MySQL 5.7 Reference Manual: The Slow Query Log documentation.

Enable the Slow Query Log

To enable the Slow Query Log for MySQL or MariaDB:

  1. Log in to your server as the root user via SSH.

  2. Open the my.cnf file with a text editor and add the following block of code under the mysqld section:

    slow_query_log = 1
    slow-query_log_file = /var/log/mysql-slow.log
    long_query_time = 2
    Note:

    In MySQL 5.6 and older, use the log-slow-queries variable instead of the slow-query_log_file variable.

     

  3. Create the /var/log/mysql-slow.log file and set its user as the mysql user. To do this, run the following commands:

    touch /var/log/mysql-slow.log
    chown mysql:mysql /var/log/mysql-slow.log

     

  4. Restart MySQL or MariaDB. To do this, run the following command:

    /usr/local/cpanel/scripts/restartsrv_mysql

     

  5. Start monitoring the slow query logfile. To analyze and print the file’s summary, run the mysqldumpslow command. For example, to print all slow queries that the system previously recorded, run the following command:

    mysqldumpslow -a /var/log/mysql-slow.log
这对您有帮助吗?

感谢您的反馈!

仍需帮助?

如果上述内容未能解答您的问题,我们的支持团队全天候为您服务。