本人为了找到这个功能,大量的搜索了很久
原文:http://blog.permastyle.com/2010/12/mysql-啟用query-log-與-slow-query-log功能.html
以下範例試用在Centos 5.x,MySQL 5.0環境下
啟用query log功能
以drupal的資料庫連結多,開啟此功能,可預期query log檔案很快就變會很大,建議debug問題時才開啟。
- $vi /etc/my.cnf
在[mysqld]區塊內設定加入
[mysqld] log=/var/log/mysqld.general.log
- mysql不會自動依設定產生mysqld.general.log,要手動加。
$ touch /var/log/mysqld.general.log
$ chown mysql.mysql /var/log/mysqld.general.log
$ chown mysql.mysql /var/log/mysqld.general.log
- 重新啟動mysqld,用瀏覧器連結網站,查看是否有產出log資料
$ /etc/init.d/mysqld restart $
tail -f /var/log/mysqld.general.log
tail -f /var/log/mysqld.general.log
log資料
2 Query SELECT * FROM languages ORDER BY weight ASC, name ASC 2 Query SELECT data, created, headers, expire, serialized FROM cache WHERE cid = 'variables:zh-hant'
2 Query SELECT COUNT(pid) FROM url_alias 2
Query SELECT src FROM url_alias WHERE dst = 'node' AND language IN('zh-hant', '') ORDER BY language DESC, pid DESC
2 Query SELECT COUNT(pid) FROM url_alias 2
Query SELECT src FROM url_alias WHERE dst = 'node' AND language IN('zh-hant', '') ORDER BY language DESC, pid DESC
啟用slow query log功能
- $ vi /etc/my.cnf
在[mysqld]區塊內設定加入
5.0版
[mysqld] // 單位是秒,預設是10秒,最小值是1
log-slow-queries = 5
log-slow-queries = /var/log/mysql-slow.log
log-long-format
log-slow-queries = 5
log-slow-queries = /var/log/mysql-slow.log
log-long-format
5.1.29版後
[mysqld]
slow_query_log
slow_query_log_file = /var/log/mysql-slow.log
long_query_time = 5
log-long-format
slow_query_log
slow_query_log_file = /var/log/mysql-slow.log
long_query_time = 5
log-long-format
- mysql不會自動依設定產生mysql-slow.log,要手動加。
$ touch /var/log/mysql-slow.log
$ chown mysql.mysql /var/log/mysql-slow.log
$ chown mysql.mysql /var/log/mysql-slow.log
- 重新啟動mysqld,用瀏覧器連結網站,查看是否有產出log資料
$ /etc/init.d/mysqld restart $ tail -f /var/log/mysql-slow.log
log資料
# Time: 090317 18:46:59
# User@Host: root[root] @ localhost [] # Query_time: 12 Lock_time: 0 Rows_sent: 242515 Rows_examined: 242515 SELECT * FROM `portal`.`search_index`;
# User@Host: root[root] @ localhost [] # Query_time: 12 Lock_time: 0 Rows_sent: 242515 Rows_examined: 242515 SELECT * FROM `portal`.`search_index`;
使用mysqldumpslow 指令分析mysql-slow.log,結果是耗費最多時間的slow query依順排出。
// query 7次,每次耗費106.43秒 Count: 7 Time=106.43s (745s) Lock=0.00s (0s) Rows=9366.9 (65568), aaa[aaa]@localhost SELECT * FROM `aaa`.`vtiger_crmentity` // query 1次,每次耗費58.00秒 Count: 1 Time=58.00s (58s) Lock=0.00s (0s) Rows=51515.0 (51515), aaa[aaa]@localhost SELECT * FROM `aaa`.`product_mac2` Count: 1 Time=42.00s (42s) Lock=0.00s (0s) Rows=1000.0 (1000), aaa[aaa]@[215.86.72.122] SELECT * FROM aaa.product_mac2 LIMIT N Count: 6 Time=38.00s (228s) Lock=0.00s (0s) Rows=130972.3 (785834), aaa[aaa]@localhost SELECT * FROM `aaa`.`vtiger_audit_trial`