1、修改my.cnf或my.ini

1)、linux-----------------------------------

/etc/my.cnf  文件

[mysqld]

long_query_time = 1
log-slow-queries XXXXXXXXX

 

2)、window--------------------------------

my.ini    文件      

[mysqld]

long_query_time = 1
log-slow-queries=C:\mysql_slow.log;
log-queries-not-using-indexes

 

2、service mysql restart

 

3、

方法二:用 mysql 客户端命令行修改,但重启之后会失效

mysql> show variables like 'lo%';
+---------------------------------+---------------------+
| Variable_name                   | Value               |
+---------------------------------+---------------------+
| local_infile                    | ON                  |
| lock_wait_timeout               | 31536000            |
| locked_in_memory                | OFF                 |
| log                             | OFF                 |
| log_bin                         | ON                  |
| log_bin_trust_function_creators | OFF                 |
| log_error                       | /disk/mysql/www.err |
| log_output                      | FILE                |
| log_queries_not_using_indexes   | OFF                 |
| log_slave_updates               | OFF                 |
| log_slow_queries                | OFF                 |
| log_warnings                    | 1                   |
| long_query_time                 | 10.000000           |
| low_priority_updates            | OFF                 |
| lower_case_file_system          | OFF                 |
| lower_case_table_names          | 0                   |
+---------------------------------+---------------------+
16 rows in set (0.00 sec)


mysql> set global long_query_time = 1;
Query OK, 0 rows affected (0.00 sec)


mysql> set global log_slow_queries = on;
Query OK, 0 rows affected, 1 warning (0.31 sec)


mysql> set global log_queries_not_using_indexes = on;
Query OK, 0 rows affected (0.00 sec)


mysql> show variables like 'lo%';                    
+---------------------------------+---------------------+
| Variable_name                   | Value               |
+---------------------------------+---------------------+
| local_infile                    | ON                  |
| lock_wait_timeout               | 31536000            |
| locked_in_memory                | OFF                 |
| log                             | OFF                 |
| log_bin                         | ON                  |
| log_bin_trust_function_creators | OFF                 |
| log_error                       | /disk/mysql/www.err |
| log_output                      | FILE                |
| log_queries_not_using_indexes   | ON                  |
| log_slave_updates               | OFF                 |
| log_slow_queries                | ON                  |
| log_warnings                    | 1                   |
| long_query_time                 | 10.000000           |
| low_priority_updates            | OFF                 |
| lower_case_file_system          | OFF                 |
| lower_case_table_names          | 0                   |
+---------------------------------+---------------------+
16 rows in set (0.00 sec)

此处发现一个很奇怪的问题,long_query_time的更改似乎没有生效。

过了几分钟,再看,发现已经改过来了。

mysql> show variables like 'lo%';
+---------------------------------+---------------------+
| Variable_name                   | Value               |
+---------------------------------+---------------------+
| local_infile                    | ON                  |
| lock_wait_timeout               | 31536000            |
| locked_in_memory                | OFF                 |
| log                             | OFF                 |
| log_bin                         | ON                  |
| log_bin_trust_function_creators | OFF                 |
| log_error                       | /disk/mysql/www.err |
| log_output                      | FILE                |
| log_queries_not_using_indexes   | ON                  |
| log_slave_updates               | OFF                 |
| log_slow_queries                | ON                  |
| log_warnings                    | 1                   |
| long_query_time                 | 1.000000            |
| low_priority_updates            | OFF                 |
| lower_case_file_system          | OFF                 |
| lower_case_table_names          | 0                   |
+---------------------------------+---------------------+
16 rows in set (0.00 sec)