一、快速安裝
概述:需要在一臺服務器(192.168.47.36)上安裝pt-query-digest、web server、php、MySQL server以及Anemometer。
1、安裝Percona Toolkit
Percona Toolkit,內含pt-query-digest
# wget -c https://www.percona.com/downloads/percona-toolkit/3.1.0/binary/redhat/7/x86_64/percona-toolkit-3.1.0-2.el7.x86_64.rpm
# yum localinstall percona-toolkit-3.1.0-2.el7.x86_64.rpm
安裝完成後驗證
# pt-query-digest --version
pt-query-digest 3.1.0
2、安裝webserver(with PHP 5.5+)
安裝 Nginx
yum install openssl openssl-devel
yum install zlib zlib-devel
yum install pcre pcre-devel
yum install gcc-c++
wget -c http://nginx.org/download/nginx-1.17.6.tar.gz
tar zxvf nginx-1.17.6.tar.gz
cd nginx-1.17.6/
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre
make && make install
啓動 Nginx
/usr/local/nginx/sbin/nginx
開放80端口,並重啓iptables
# systemctl restart iptables
此時可以訪問到測試頁面:http://192.168.47.36
直接yum安裝的是php5.4版本,以下爲php7.4的編譯安裝參數:
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-iconv-dir=/usr/local/libiconv \
--with-openssl \
--with-openssl-dir \
--with-pcre-regex \
--with-pcre-dir \
--with-bz2 \
--with-curl \
--enable-ftp \
--enable-sockets \
--disable-ipv6 \
--enable-mbstring \
--enable-calendar \
--with-gettext \
--with-zlib \
--with-zlib-dir \
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--enable-dom \
--enable-xml \
--enable-fpm \
--with-libdir=lib64 \
--enable-bcmath
驗證
# vim /usr/local/nginx/html/info.php
<?php
phpinfo();
?>
重啓後,通過http://192.168.47.36/info.php可以訪問到PHP version信息
3、安裝MySQL
MySQL用於存放慢日誌信息。安裝過程略,server信息:192.168.47.36:3306
4、部署Anemometer
4.1、Anemometer的代碼下載
# cd /var/www/html/
# git clone https://github.com/box/Anemometer.git
4.2、表定義文件(/var/www/html/Anemometer/install.sql)導入
Tips:MySQL5.7版本在導入時會報錯,有兩個解決辦法:
方法1、修改install.sql文件
ts_min datetime NOT NULL DEFAULT '1990-01-01 00:00:00',
ts_max datetime NOT NULL DEFAULT '1990-01-01 00:00:00',
方法2、修改sql mode,去除NO_ZERO_DATE
mysql> select @@global.sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@global.sql_mode |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> set @@global.sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINNE_SUBSTITUTION';
Query OK, 0 rows affected (0.01 sec)
導入文件
# /usr/local/mysql3306/bin/mysql -uroot -p -S /tmp/mysql3306.sock < ./install.sql
生成slow_query_log庫下的兩張表
mysql> show tables;
+-----------------------------+
| Tables_in_slow_query_log |
+-----------------------------+
| global_query_review |
| global_query_review_history |
+-----------------------------+
2 rows in set (0.00 sec)
4.2、配置本地數據庫的信息
本地數據庫中創建賬戶信息
mysql> grant ALL ON slow_query_log.* to 'anemometer'@'%' IDENTIFIED BY 'superSecurePass';
再在/var/www/html/Anemometer/conf/datasource_localhost.inc.php中配置本地數據庫的連接信息
<?php
$conf['datasources']['localhost'] = array(
'host' => '127.0.0.1',
'port' => 3306,
'db' => 'slow_query_log',
'user' => 'anemometer',
'password' => 'superSecurePass',
'tables' => array(
'global_query_review' => 'fact',
'global_query_review_history' => 'dimension'
),
# 'source_type' => 'slow_query_log'
);
此時由於配置未完成,暫無法訪問
按頁面提示:# cp sample.config.inc.php config.inc.php,暫時先不配置該文件,此時可以在瀏覽器中看到web界面了
4.3、基礎調試
Tips1、針對web頁面尾部的報錯,在sql mode中去除:ONLY_FULL_GROUP_BY
Tips2、其他報錯(如:HTTP ERROR 500)信息查詢:tail /var/log/httpd/error_log
二、慢日誌導入
1、利用本地庫測試
將min_examined_row_limit關閉後,執行測試語句:select a,sleep(2) from tbl_1,此時該SQL將會記錄到slow log中。
將slow log通過pt-query-digest分析後存入數據庫中
pt-query-digest --user=anemometer --password=superSecurePass -S /tmp/mysql3306.sock \
--review h=localhost,D=slow_query_log,t=global_query_review \
--history h=localhost,D=slow_query_log,t=global_query_review_history \
--no-report --limit=0% \
--filter=" \$event->{Bytes} = length(\$event->{arg}) and \$event->{hostname}=\"$HOSTNAME\"" /data/mysql3306/slow.log
一般導入過程會有如下報錯:
Pipeline process 5 (iteration) caused an error: DBD::mysql::st execute failed: Incorrect integer value: 'A10C08340795A7F3905F94F51204DE4C' for column 'checksum' at row 1 [for Statement " INSERT INTO `slow_query_log`.`global_query_review`
解決辦法:修改sql mode,去除STRICT_TRANS_TABLES後,重新導入
此時web界面
2、添加遠程DB信息
# cd /var/www/html/Anemometer/conf
# vim config.inc.php
$conf['datasources']['192.168.47.33'] = array(
'host' => '192.168.47.33',
'port' => 3306,
'db' => 'slow_query_log',
'user' => 'root',
'password' => '123456',
'tables' => array(
'global_query_review' => 'fact',
'global_query_review_history' => 'dimension'
),
'source_type' => 'slow_query_log'
);
將以下操作設置爲定時任務,定期推送slow log數據到Anemometer的DB中
pt-query-digest --user=db1 --password=db1Pass --host=192.168.47.33 --port=3306 \
--review h=192.168.47.36,D=slow_query_log,t=global_query_review \
--history h=192.168.47.36,D=slow_query_log,t=global_query_review_history \
--no-report --limit=0% \
--filter=" \$event->{Bytes} = length(\$event->{arg}) and \$event->{hostname}=\"$HOSTNAME\"" /data/mysql3306/slow.log
三、其他功能
1、啓用pt-visual-explain
pt-visual-explain默認已配置,只需填入合適的賬戶密碼(遠程DB的全庫select權限)就可以啓用。
$conf['plugins'] = array(
...
'explain' => function ($sample) {
$conn = array();
...
$conn['user'] = 'explainuser';
$conn['password'] = '123456';
return $conn;
},
);
開啓該功能可提供的信息如下:
執行計劃(Explain Plan):如下所示
Visual Explain Plan:優化器選取的索引、索引的ken_len以及掃描的行數
Create Table Statements和Table Status:表結構信息
參考文檔: