用MySQL慢日誌分析解決MySQL CPU佔用高的問題

來源:互聯網
上載者:User

首先找到MySQL的設定檔my.cnf,根據不同版本的mysql開啟慢查詢的配置也不一樣

mysql 5.0

[mysqld]
long_query_time = 1
log-slow-queries = /var/log/mysql/slow.log

mysql 5.1

[mysqld]
long_query_time = 1
slow_query_log=1
slow_query_log_file = /var/log/mysql/slow.log

long_query_time 是指執行超過多久的sql會被log下來,這裡是1秒。
log-slow-queries和slow_query_log_file 設定把日誌寫在哪裡

把上述參數開啟,運行一段時間,就可以關掉了,省得影響生產環境

接下來就是分析了,我這裡的檔案名稱字叫 /var/log/mysql/slow.log。
先mysqldumpslow –help下,主要用的是

-s ORDER what to sort by (t, at, l, al, r, ar etc), ‘at’ is default
-t NUM just show the top n queries
-g PATTERN grep: only consider stmts that include this string

-s,是order的順序,說明寫的不夠詳細,主要有
c,t,l,r和ac,at,al,ar,分別是按照query次數,時間,lock的時間和返回的記錄數來排序,前面加了a的時倒序
-t,是top n的意思,即為返回前面多少條的資料
-g,後邊可以寫一個正則匹配模式,大小寫不敏感的

mysqldumpslow -s c -t 20  /var/log/mysql/slow.log
mysqldumpslow -s r -t 20  /var/log/mysql/slow.log

上述命令可以看出訪問次數最多的20個sql語句和返回記錄集最多的20個sql。

mysqldumpslow -t 10 -s t -g “left join”  /var/log/mysql/slow.log
這個是按照時間返回前10條裡面含有左串連的sql語句。

例子,正確安全清空線上慢查詢日誌slow log的流程

1, see the slow log status;
mysql> show variables like '%slow%';
+---------------------+------------------------------------------+
| Variable_name       | Value                                    |
+---------------------+------------------------------------------+
| log_slow_queries    | ON                                       |
| slow_launch_time    | 2                                        |
| slow_query_log      | ON                                       |
| slow_query_log_file | /mysqllog/slow_log/slow_queries_3306.log |
+---------------------+------------------------------------------+
4 rows in set (0.00 sec)


2, stop the slow log server.
mysql> set global slow_query_log=0;
Query OK, 0 rows affected (0.27 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 | /mysqllog/slow_log/slow_queries_3306.log |
+---------------------+------------------------------------------+
4 rows in set (0.00 sec)


mysql>
mysql> show variables like '%slow%'; -- check slow log status
+---------------------+------------------------------------------+
| Variable_name       | Value                                    |
+---------------------+------------------------------------------+
| log_slow_queries    | OFF                                      |
| slow_launch_time    | 2                                        |
| slow_query_log      | OFF                                      |
| slow_query_log_file | /mysqllog/slow_log/slow_queries_3306.log |
+---------------------+------------------------------------------+
4 rows in set (0.00 sec)


3, reset the new path of slow log
mysql> set global slow_query_log_file='/mysqllog/slow_log/slow_queries_3306_new.log';
Query OK, 0 rows affected (0.03 sec)


4, start the slow log server
mysql>
mysql>
mysql> set global slow_query_log=1;
Query OK, 0 rows affected (0.01 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 | /mysqllog/slow_log/slow_queries_3306_new.log |
+---------------------+----------------------------------------------+
4 rows in set (0.00 sec)


5, check the slow sql in the new slow log file.
mysql> select sleep(10) as a, 1 as b;
+---+---+
| a | b |
+---+---+
| 0 | 1 |
+---+---+
1 row in set (10.00 sec)


mysql>
[mysql@xxx-xxx ~]$ more /mysqllog/slow_log/slow_queries_3306_new.log
......
Time                 Id Command    Argument
# Time: 140213  6:44:24
# User@Host: root[root] @ localhost []
# Query_time: 10.000365  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
SET timestamp=1392273864;
select sleep(10) as a, 1 as b;


6, backup the old big slow log file to other directory.
mv /mysqllog/slow_log/slow_queries_3306.log /mysqlbackup/slow_log/slow_queries_3306.log.bak.20140213

用了這個工具就可以查詢出來那些sql語句是效能的瓶頸,進行最佳化,比如加索引,該應用的實現方式等

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.