I. Introduction
Turn on slow query log, you can let statement to query exceeds the specified time under the MySQL records, by positioning performance bottleneck analysis, in order to better optimize the performance of database systems.
Second, Parameter Description
slow_query_log slow queries on state
slow_query_log_file slow query log storage location (the directory needs to run the MySQL account write permission, usually set storage directory for MySQL data)
long_query_time query exceeds the number of seconds before the record
Third, set the step
1. Review the slow query 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. Set Methods
Method a: setting a global variable
to slow_query_log global variable is set to "ON" state
mysql> set global slow_query_log='ON';
Set slow query log storage location
mysql> set global slow_query_log_file='/usr/local/mysql/data/slow.log';
Discover more than 1 second to record
mysql> set global long_query_time=1;
Method Two: Profile Settings
modify the configuration file my.cnf, added below the [mysqld] under
[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 parameter settings
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 | +-----------------+----------+
Fourth, the test
1. Perform a slow query SQL statement
mysql> select sleep(2);
2. Check whether to generate slow query log
ls /usr/local/mysql/data/slow.log
If the log exists, MySQL slow query is set to open success!