Enabling and configuring MySQL slow query log

Enabling and configuring MySQL slow query log

Introduction

MySQL slow query log is an important function for troubleshooting problematic SQL statements and checking current MySQL performance.

Check whether the slow query function is enabled:

mysql> show variables like 'slow_query%';
+---------------------+------------------------------------+
| Variable_name | Value |
+---------------------+------------------------------------+
| slow_query_log | OFF |
| slow_query_log_file | /var/lib/mysql/instance-1-slow.log |
+---------------------+------------------------------------+
2 rows in set (0.01 sec)
mysql> show variables like 'long_query_time';
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)

illustrate:

slow_query_log slow query enable status
slow_query_log_file The location where the slow query log is stored (this directory requires writable permissions for the MySQL running account, and is generally set to the MySQL data storage directory)
long_query_time How many seconds does it take for a query to be recorded?

Configuration

Temporary Configuration

Slow query logging is not enabled by default. Enable it temporarily using the command:

mysql> set global slow_query_log='ON';
Query OK, 0 rows affected (0.00 sec)
 
mysql> set global slow_query_log_file='/var/lib/mysql/instance-1-slow.log';
Query OK, 0 rows affected (0.00 sec)
 
mysql> set global long_query_time=2;
Query OK, 0 rows affected (0.00 sec)

Permanent configuration Modify the configuration file to achieve permanent configuration status:

/etc/mysql/conf.d/mysql.cnf
[mysqld]
slow_query_log = ON
slow_query_log_file = /var/lib/mysql/instance-1-slow.log
long_query_time = 2

After configuration, restart MySQL.

test

Run the following command to execute the problematic SQL statement:

mysql> select sleep(2);
+----------+
| sleep(2) |
+----------+
| 0 |
+----------+
1 row in set (2.00 sec)

Then view the slow query log content:

$ cat /var/lib/mysql/instance-1-slow.log
/usr/sbin/mysqld, Version: 8.0.13 (MySQL Community Server - GPL). started with:
Tcp port: 3306 Unix socket: /var/run/mysqld/mysqld.sock
Time Id Command Argument
/usr/sbin/mysqld, Version: 8.0.13 (MySQL Community Server - GPL). started with:
Tcp port: 3306 Unix socket: /var/run/mysqld/mysqld.sock
Time Id Command Argument
# Time: 2018-12-18T05:55:15.941477Z
# User@Host: root[root] @ localhost [] Id: 53
# Query_time: 2.000479 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
SET timestamp=1545112515;
select sleep(2);

The above is the details of how to enable and configure the MySQL slow query log. For more information about the MySQL slow query log, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of MySql slow query analysis and opening slow query log
  • How to enable MySQL slow query log log-slow-queries
  • Tips for enabling slow query log in MYSQL5.7.9
  • mysql enable slow query how to enable mysql slow query logging
  • How to enable slow query log in MySQL
  • How to enable the slow query log function in MySQL
  • Detailed explanation of how to enable slow query log in MySQL database
  • The role and opening of MySQL slow query log
  • MySQL optimization solution: enable slow query log

<<:  XHTML tags that are easily confused by the location of the use

>>:  Solve the problem that the repository and tag names are both none after Docker loads a new image

Recommend

How to execute Linux shell commands in Docker

To execute a shell command in Docker, you need to...

web.config (IIS) and .htaccess (Apache) configuration

xml <?xml version="1.0" encoding=&qu...

Detailed process analysis of docker deployment of snail cinema system

Environmental Statement Host OS: Cetnos7.9 Minimu...

MySQL primary key naming strategy related

Recently, when I was sorting out the details of d...

Example analysis of the impact of MySQL index on sorting

This article uses examples to illustrate the impa...

The use of textarea in html and common problems and case analysis

The textarea tag is an HTML tag that we often use....

How to modify the time zone and time in Ubuntu system

On a Linux computer, there are two times, one is ...

HTML implements a fixed floating semi-transparent search box on mobile

Question. In the mobile shopping mall system, we ...

MySQL index failure principle

Table of contents 1. Reasons for index failure 2....

Django2.* + Mysql5.7 development environment integration tutorial diagram

environment: MAC_OS 10.12 Python 3.6 mysql 5.7.25...

Getting Started with Mysql--sql execution process

Table of contents 1. Process 2. Core Architecture...

Detailed explanation of Vue's monitoring method case

Monitoring method in Vue watch Notice Name: You s...

Implementation of sharing data between Docker Volume containers

What is volume? Volume means capacity in English,...