MySQL 5.1.6以上版本動態開啟慢查詢日誌
轉載於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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL 5.1.6以上版本動態開啟慢查詢日誌薦MySql
- Mysql 啟動慢查詢日誌 (不用重啟)MySql
- MYSQL開啟慢查詢日誌實施MySql
- mysql開啟檢視慢查詢日誌MySql
- 如何在MySQL中開啟慢查詢日誌?MySql
- 慢查詢日誌開啟分析
- 開啟查詢慢查詢日誌引數
- MySQL:慢查詢日誌MySql
- mysql慢查詢日誌MySql
- 【MySQL】慢查詢日誌不列印MySql
- mysqlsla 分析mysql慢查詢日誌MySql
- mysql開啟慢日誌MySql
- MySQL優化 - 開啟MySQL慢查詢日誌及分析工具mysqldumpslowMySql優化
- 關於MySQL 通用查詢日誌和慢查詢日誌分析MySql
- PHP慢指令碼日誌和Mysql的慢查詢日誌PHP指令碼MySql
- MySQL開啟慢查詢MySql
- MySQL 慢查詢日誌——讓“慢”無所遁形MySql
- mysql之 slow log 慢查詢日誌MySql
- MySQL Slow Query log(慢查詢日誌)MySql
- mysql慢查詢日誌分析工具使用MySql
- Mysql慢查詢日誌分析工具mysqlslaMySql
- 【MySql】 慢日誌查詢工具之mysqlslaMySql
- 【MySql】mysql 慢日誌查詢工具之mysqldumpslowMySql
- 使用慢查詢日誌
- MySQL慢查詢日誌相關設定MySql
- 【轉載】MySQL慢查詢日誌總結MySql
- mysql慢查詢和錯誤日誌分析MySql
- 用命令過濾MySQL慢查詢日誌MySql
- Mysql慢查詢日誌檔案轉ExcelMySqlExcel
- 對 MySQL 慢查詢日誌的簡單分析MySql
- mysql分析慢查詢日誌工具mysqlsla安裝MySql
- 分析mysql慢查詢日誌的好工具--mysqlslaMySql
- 慢查詢日誌的管理
- 開啟mysql和php慢日誌方法MySqlPHP
- MySQL資料庫中的日誌檔案---(3)慢查詢日誌MySql資料庫
- mysql 日誌之慢查詢日誌MySql
- mysql 查詢日誌MySql
- mysql查詢日誌MySql