MYSQL slow query and log settings and testing

MYSQL slow query and log settings and testing

1. Introduction

By enabling the slow query log, MySQL can record query statements that exceed the specified time. By locating and analyzing performance bottlenecks, the performance of the database system can be better optimized.

2. Parameter Description

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?

3. Setup steps

1. View slow query related parameters

mysql> show variables like 'slow_query%';
+---------------------------+----------------------------------+
| Variable_name | Value |
+---------------------------+----------------------------------+
| slow_query_log | OFF |
| slow_query_log_file | /mysql/data/localhost-slow.log |
+---------------------------+----------------------------------+

mysql> show variables like 'long_query_time';
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+

2. Setting method

Method 1: Global variable setting

Set the slow_query_log global variable to the "ON" state

mysql> set global slow_query_log='ON';

Set the location where the slow query log is stored

mysql> set global slow_query_log_file='/usr/local/mysql/data/slow.log';

If the query exceeds 1 second, it will be recorded

mysql> set global long_query_time=1;

Method 2: Configuration file settings

Modify the configuration file my.cnf and add the following under [mysqld]

[mysqld] slow_query_log = ON slow_query_log_file = /usr/local/mysql/data/slow.log long_query_time = 1

3. Restart MySQL service

service mysqld restart

4. Check the parameters after setting

mysql> show variables like 'slow_query%';
+---------------------+--------------------------------+
| Variable_name | Value |
+---------------------+--------------------------------+
| slow_query_log | ON |
| slow_query_log_file | /usr/local/mysql/data/slow.log |
+---------------------+--------------------------------+

mysql> show variables like 'long_query_time';
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| long_query_time | 1.000000 |
+-----------------+----------+

4. Testing

1. Execute a slow query SQL statement

mysql> select sleep(2);

2. Check whether slow query logs are generated

ls /usr/local/mysql/data/slow.log

If the log exists, MySQL slow query setting is enabled successfully!

Summarize

This is the end of this article about the setting and testing of MYSQL slow query and log. For more relevant MYSQL slow query and log content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL slow query pitfalls
  • MYSQL slow query and log example explanation
  • The role and opening of MySQL slow query log
  • Enabling and configuring MySQL slow query log
  • Example of MySQL slow query
  • Mysql sql slow query monitoring script code example
  • How to locate MySQL slow queries
  • MySQL slow query method and example
  • Detailed explanation of the reason why the MySQL 5.7 slow query log time is 8 hours behind the system time
  • Mysql slow query optimization method and optimization principle
  • How to optimize MySQL performance through MySQL slow query
  • Solve the problem of MySQL Threads_running surge and slow query

<<:  HTML table tag tutorial (19): row tag

>>:  Limiting the number of short-term accesses to a certain IP based on Nginx

Recommend

Detailed Introduction to the MySQL Keyword Distinct

Introduction to the usage of MySQL keyword Distin...

How to manually deploy war packages through tomcat9 on windows and linux

The results are different in Windows and Linux en...

W3C Tutorial (12): W3C Soap Activity

Web Services are concerned with application-to-ap...

Complete steps to install MySQL 8.0.x on Linux

MySQL Introduction to MySQL MySQL was originally ...

Examples of vertical grid and progressive line spacing

New Questions Come and go in a hurry. It has been...

How to generate a unique server-id in MySQL

Preface We all know that MySQL uses server-id to ...

Example code for mixing float and margin in CSS

In my recent studies, I found some layout exercis...

Complete steps to use element in vue3.0

Preface: Use the element framework in vue3.0, bec...

Use of Zabbix Api in Linux shell environment

You can call it directly in the Linux shell envir...

Rhit efficient visualization Nginx log viewing tool

Table of contents Introduction Install Display Fi...

VS2019 connects to mysql8.0 database tutorial with pictures and text

1. First, prepare VS2019 and MySQL database. Both...

Example of using UserMap in IMG

usemap is an attribute of the <img> tag, use...

CSS flexible layout FLEX, media query and mobile click event implementation

flex layout Definition: The element of Flex layou...

How to set MySQL foreign keys for beginners

Table of contents The role of foreign keys mysql ...