MySQL 5.1.6以上版本動態開啟慢查詢日誌

dimen007發表於2011-01-05

轉載於http://wangxiaoyu.blog.**.com/922065/471638,感謝作者提供

以MySQL 5.1.36為例:
在slow_query_log (注意log_slow_querys引數已經廢棄)值為ON的情況下(預設為OFF),當一條SQL語句執行的時間超過了long_query_time 預設的時間(預設為10s,同時精確到微秒)時,預設(log_output值為FIFL時)就會把這種慢查詢記錄到:slow_query_log_file值所指定的檔案中。

mysql> select @@global.log_output;
+---------------------+
| @@global.log_output |
+---------------------+
| FILE                |
+---------------------+
1 row in set (0.00 sec)

注意在MySQL5.1就開始支援把慢查詢的日誌記錄放到mysq.slow_log中,但需要設定log_output變數值為TABLE:
mysql> set @@global.log_output='TABLE';
Query OK, 0 rows affected (0.00 sec)

mysql> desc mysql.slow_log;
+----------------+------------------+------+-----+-------------------+-----------------------------+
| Field          | Type             | Null | Key | Default           | Extra                       |
+----------------+------------------+------+-----+-------------------+-----------------------------+
| start_time     | timestamp        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| user_host      | mediumtext       | NO   |     | NULL              |                             |
| query_time     | time             | NO   |     | NULL              |                             |
| lock_time      | time             | NO   |     | NULL              |                             |
| rows_sent      | int(11)          | NO   |     | NULL              |                             |
| rows_examined  | int(11)          | NO   |     | NULL              |                             |
| db             | varchar(512)     | NO   |     | NULL              |                             |
| last_insert_id | int(11)          | NO   |     | NULL              |                             |
| insert_id      | int(11)          | NO   |     | NULL              |                             |
| server_id      | int(10) unsigned | NO   |     | NULL              |                             |
| sql_text       | mediumtext       | NO   |     | NULL              |                             |
+----------------+------------------+------+-----+-------------------+-----------------------------+
11 rows in set (0.01 sec)

注意,檔案記錄和寫表記錄只會有一種生效。


修改之前:
mysql> show variables like '%slow%';
+---------------------+------------------------------------------------+
| Variable_name       | Value                                          |
+---------------------+------------------------------------------------+
| log_slow_queries    | OFF                                            |
| slow_launch_time    | 2                                              |
| slow_query_log      | OFF                                            |
| slow_query_log_file | /usr/local/mysql-5.1.36/var/localhost-slow.log |
+---------------------+------------------------------------------------+
mysql>
4 rows in set (0.00 sec)

注意這個時候localhost-slow.log檔案是不存在的:
mysql> system cat /usr/local/mysql-5.1.36/var/localhost-slow.log
cat: /usr/local/mysql-5.1.36/var/localhost-slow.log: No such file or directory

修改slow_query_log的方法:
mysql> warnings;
Show warnings enabled.
mysql> set @@global.log_slow_queries=ON;
Query OK, 0 rows affected, 1 warning (0.01 sec)
Warning (Code 1287): The syntax '@@log_slow_queries' is deprecated and will be removed in MySQL 7.0. Please use '@@slow_query_log' instead
注意已經不再使用log_slow_queries引數,用slow_query_log替代,這樣修改slow_query_log時,log_slow_queries也會被隱性地修改:

mysql> set @@global.slow_query_log=ON;
Query OK, 0 rows affected (0.00 sec)

修改之後:
mysql> show variables like '%slow%';
+---------------------+------------------------------------------------+
| Variable_name       | Value                                          |
+---------------------+------------------------------------------------+
| log_slow_queries    | ON                                             |
| slow_launch_time    | 2                                              |
| slow_query_log      | ON                                             |
| slow_query_log_file | /usr/local/mysql-5.1.36/var/localhost-slow.log |
+---------------------+------------------------------------------------+
4 rows in set (0.00 sec)

這時localhost-slow.log檔案已經建立:
mysql> system cat /usr/local/mysql-5.1.36/var/localhost-slow.log
/usr/local/mysql-5.1.36/libexec/mysqld, Version: 5.1.36-log (Source distribution). started with:
Tcp port: 3306  Unix socket: /tmp/mysql.sock
Time                 Id Command    Argument

分析時我們可以直接查詢慢查詢日誌,也可以通過mysqldumpslow命令(推薦)來解析這個檔案:
[root@localhost var]# mysqldumpslow localhost-slow.log
Reading mysql slow query log from localhost-slow.log
Count: 1  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), 0users@0hosts

我們可以通過sleep函式來做簡單的測試:
如:select sleep(11);

回去測試

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12309491/viewspace-683297/,如需轉載,請註明出處,否則將追究法律責任。

相關文章