使用Anemometer基於pt-query-digest將MySQL慢查詢可視化

來源:互聯網
上載者:User

標籤:completely   database   getting   server   whole   

If you‘re just completely itching to start using this tool, here‘s what you need:

1、a MySQL database to store query analysis data in.

2、pt-query-digest.You may as well just get the whole Percona Toolkit while you‘re at it :)

3、a slow query log from a MySQL server (see The Slow Query Log for info on getting one)

4、a webserver with PHP

 

The first first

[[email protected] anemometer]# wget percona.com/get/percona-toolkit.rpm[[email protected] ~]# yum install -y percona-toolkit.rpm

Then,Pt-query-digest reference configuration is given

[code collapse="false"]pt-query-digest --user=資料庫使用者名稱 --password=資料庫密碼 --review h=資料庫IP和網域名稱,D=slow_query_log,t=global_query_review --history h=資料庫IP和網域名稱,D=slow_query_log,t=global_query_review_history --no-report --limit=0% --filter=" \$event->{Bytes} = length(\$event->{arg}) and \$event->{hostname}=\"$HOSTNAME\"" 資料庫記錄檔地址[/code]

next,Grammar and important options

pt-query-digest [OPTIONS] [FILES] [DSN]
--create-review-table 當使用--review參數把分析結果輸出到表中時,如果沒有表就自動建立。
--create-history-table 當使用--history參數把分析結果輸出到表中時,如果沒有表就自動建立。
--filter 對輸入的慢查詢按指定的字串進行匹配過濾後再進行分析
--limit限制輸出結果百分比或數量,預設值是20,即將最慢的20條語句輸出,如果是50%則按總回應時間佔比從大到小排序,輸出到總和達到50%位置截止。
--host MySQL伺服器位址
--user mysql使用者名稱
--password mysql使用者密碼
--history 將分析結果儲存到表中,分析結果比較詳細,下次再使用--history時,如果存在相同的語句,且查詢所在的時間區間和曆史表中的不同,則會記錄到資料表中,可以通過查詢同一CHECKSUM來比較某類型查詢的曆史變化。
--review 將分析結果儲存到表中,這個分析只是對查詢條件進行參數化,一個類型的查詢一條記錄,比較簡單。當下次使用--review時,如果存在相同的語句分析,就不會記錄到資料表中。
--output 分析結果輸出類型,值可以是report(標準分析報告)、slowlog(Mysql slow log)、json、json-anon,一般使用report,以便於閱讀。
--since 從什麼時間開始分析,值為字串,可以是指定的某個”yyyy-mm-dd [hh:mm:ss]”格式的時間點,也可以是簡單的一個時間值:s(秒)、h(小時)、m(分鐘)、d(天),如12h就表示從12小時前開始統計。
--until 截止時間,配合—since可以分析一段時間內的慢查詢。

 

Install  anemometer

First up, grab the anemometer code from github. Navigate to the document root of your web server and snag a copy of the Box Anemometer code.

[[email protected] ~]# git clone git://github.com/box/Anemometer.git anemometer

or ,if you have 9418 port closed:

[[email protected] ~]# git clone

then change your current working directory to the anemometer directory:

[[email protected] ~]# cd anemometer

Next, you should connect to the MySQL database you‘re looking to store the analysis data in and issue the following command:

[[email protected] anemometer]# mysql -uroot -p123456 < install.sql [[email protected] anemometer]# mysql -uroot -p123456 -e "grant ALL ON slow_query_log.* to  BY ‘123456‘;"

 Next, grab that slow query log file you have (mine‘s called "slow.log"!), and run pt-query-digest on it: NOTE: I‘m using a BASH 3.0 shell here on my MySQL database server! This is so the "$HOSTNAME" variable properly replaces with "db.example.com")

[[email protected] ~]# rpm -qa|grep percona-toolkit percona-toolkit-2.2.16-1.noarch

For pt-query-digest version < 2.2

pt-query-digest --user=anemometer --password=superSecurePass                   --review h=db.example.com,D=slow_query_log,t=global_query_review                   --review-history h=db.example.com,D=slow_query_log,t=global_query_review_history                   --no-report --limit=0% \                   --filter=" \$event->{Bytes} = length(\$event->{arg}) and \$event->{hostname}=\"$HOSTNAME\"" \                   /var/lib/mysql/db.example.com-slow.log

For pt-query-digest version >= 2.2

[[email protected] ~]# pt-query-digest --user=anemometer --password=123456 --review D=slow_query_log,t=global_query_review --history D=slow_query_log,t=global_query_review_history --no-report --limit=0% --filter=" \$event->{Bytes} = length(\$event->{arg}) and \$event->{hostname}=\"$HOSTNAME\"" /usr/local/mysql/var/slow.log

You may see an error like above, that‘s okay! TODO: explain what the options above are doing.

Pipeline process 11 (aggregate fingerprint) caused an error: Argument "57A" isn‘t numeric in numeric gt (>) at (eval 40) line 6, <> line 27.Pipeline process 11 (aggregate fingerprint) caused an error: Argument "57B" isn‘t numeric in numeric gt (>) at (eval 40) line 6, <> line 28.Pipeline process 11 (aggregate fingerprint) caused an error: Argument "57C" isn‘t numeric in numeric gt (>) at (eval 40) line 6, <> line 29.

 

View the data!

Now, navigate to the document root of your web server and copy the sample config so you can edit it:

[[email protected] conf]# cd /home/wwwroot/anemometer/conf[[email protected] conf]# cp sample.config.inc.php config.inc.php

The sample config explains every setting you may want to change in it. At the very least, make sure you set the Datasource to the MySQL database you‘re storing the analyzed digest information in:

$conf[‘datasources‘][‘localhost‘] = array(    ‘host‘  => ‘db.example.com‘,    ‘port‘  => 3306,    ‘db‘    => ‘slow_query_log‘,    ‘user‘  => ‘anemometer‘,    ‘password‘ => ‘superSecurePass‘,    ‘tables‘ => array(        ‘global_query_review‘ => ‘fact‘,        ‘global_query_review_history‘ => ‘dimension‘    ));

In addition, the "explain" plugin is enabled by default in the current release and you‘ll need to setup the username and password it uses to an account that has privileges to explain queries on a given schema on a host. For example, if you‘re digesting slow logs that primarily contain queries from the "world" database on db.example.com, you‘ll need to ensure that the user account you put into the following section of the config has the necessary privileges on the "world" database on db.example.com. To do this, scroll down in the sample config to the section containing the plugins configuration and change the ‘user‘ and ‘password‘ parameters to an appropriate account:

$conf[‘plugins‘] = array(        ...    ‘explain‘       =>      function ($sample) {        $conn[‘user‘] = ‘anemometer‘;        $conn[‘password‘] = ‘superSecurePass‘;        return $conn;    },);

Finally,you can look through

http://10.0.0.5/anemometer/

本文出自 “IT--小哥” 部落格,請務必保留此出處http://402753795.blog.51cto.com/10788998/1832976

使用Anemometer基於pt-query-digest將MySQL慢查詢可視化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.