Jquery中文網 www.uhadif.co
Jquery中文網 >  數據庫  >  mysql  >  正文 用MySQL慢日志分析解決MySQL CPU占用高的問題

用MySQL慢日志分析解決MySQL CPU占用高的問題

發布時間:2017-12-13   編輯:www.uhadif.co
jquery中文網為您提供用MySQL慢日志分析解決MySQL CPU占用高的問題等資源,歡迎您收藏本站,我們將為您提供最新的用MySQL慢日志分析解決MySQL CPU占用高的問題資源
我們可以通過MySQL慢日志分析來分析網站執行的sql語句占用的時間與性能從而我們對此條sql進行優化操作了,下面一起來看小編整理的用MySQL慢日志分析解決MySQL CPU占用高的問題的方法。

首先找到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>
[[email protected] ~]$ more /mysqllog/slow_log/slow_queries_3306_new.log
......
Time                 Id Command    Argument
# Time: 140213  6:44:24
# [email protected]: 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語句是性能的瓶頸,進行優化,比如加索引,該應用的實現方式等

您可能感興趣的文章:
用MySQL慢日志分析解決MySQL CPU占用高的問題
shell腳本統計多個CPU利用率
mysql開啟慢查詢以檢查查詢慢的語句
mongodb的NUMA問題的解決方法
php程序隨機記錄mysql rand()造成CPU 100%的解決方法
清除mysql的log-bin日志的方案
shell腳本:MySQL慢查詢日志和錯誤日志按天輪詢
libmysqlclient.so.15()(64bit) is needed by perl-DBD-MySQL-3.0007-2.el5.x86_64的解決
MySQL 根據 status 狀態優化
mysql主從復制配置與原理分析

[關閉]
北京pk赛车历史