概述:需要在一台服务器(192.168.47.36)上安装pt-query-digest、web server、php、MySQL server以及Anemometer。
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
安装 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
重启后,通过http://192.168.47.36/info.php可以访问到PHP version信息
MySQL用于存放慢日志信息。安装过程略,server信息:192.168.47.36:3306
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中配置本地数据库的连接信息
'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:表结构信息
1、Anemometer Github地址