最近發現了一個挺好用的MySQL慢查詢日誌可視化的工具,網上也能找到不少資料,在這裏結合本身的實際狀況從新整理一下。php
操做系統:CentOS 6.5 64位 主機地址:10.0.0.26 主機名:mysql01 mysql版本:mysql-5.6.36
http環境:LAMP 可視化軟件:Anemometer-master.zip, percona-toolkit-3.0.11-1.el6.x86_64.rpm, perl-TermReadKey-2.30-13.el6.x86_64.rpm
採用源碼編譯安裝的方式來安裝MySQL服務,並開啓慢查詢日誌功能。
安裝依賴:html
yum -y install ncurses-devel libaio-devel cmake
建立用戶:mysql
useradd -s /sbin/nologin -M mysql
下載MySQL源碼包:git
cd /usr/local/src wget -q http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36.tar.gz
解壓,編譯安裝github
tar xf mysql-5.6.36.tar.gz cd mysql-5.6.36 cmake . -DCMAKE_INSTALL_PREFIX=/opt/mysql-5.6.36 \ -DMYSQL_DATADIR=/opt/mysql-5.6.36/data \ -DMYSQL_UNIX_ADDR=/opt/mysql-5.6.36/tmp/mysql.sock \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_EXTRA_CHARSETS=all \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_FEDERATED_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \ -DWITH_ZLIB=bundled \ -DWITH_SSL=bundled \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_EMBEDDED_SERVER=1 \ -DENABLE_DOWNLOADS=1 \ -DWITH_DEBUG=0 make && make install ln -s /opt/mysql-5.6.36/ /opt/mysql
初始化數據庫:sql
\cp support-files/my*.cnf /etc/my.cnf /opt/mysql/scripts/mysql_install_db --basedir=/opt/mysql/ --datadir=/opt/mysql/data --user=mysql mkdir /opt/mysql/tmp chown -R mysql.mysql /opt/mysql/ cp /usr/local/src/mysql-5.6.36/support-files/mysql.server /etc/init.d/mysqld chmod 700 /etc/init.d/mysqld chkconfig mysqld on chkconfig --list mysqld /etc/init.d/mysqld start netstat -lntup|grep 3306
修改默認用戶密碼:數據庫
mysqladmin -uroot password 123456;
配置MySQL環境變量:apache
echo 'PATH=/opt/mysql/bin/:$PATH' >>/etc/profile source /etc/profile
修改配置文件,開啓慢查詢日誌:vim
[mysqld] slow_query_log=1 #<==開啓慢查詢日誌 long_query_time = 1 #<==設置慢查詢日誌的閾值,超出設定值的SQL被記錄到日誌文件,缺省10S log_queries_not_using_indexes #<==統計不走索引的語句 #min_examined_row_limit=1000 #<==返回行少於該值,不被記錄到日誌,最好不要設置,可能會有衝突 slow_query_log_file = /opt/mysql/data/slow.log
修改完後,重啓mysql服務:centos
/etc/init.d/mysqld restart
Percona Toolkit是一組高級的命令行工具,用來管理mysql和系統任務,主要包括:
1:驗證主節點和複製數據的一致性 2:有效的度記錄行進行歸檔 3:找出重複的索引 4:總結MySQL服務器 5:從日誌和tcpdump中分析查詢 6:問題發生時收集重要的系統信息
安裝依賴:
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo yum install perl-IO-Socket-SSL perl-DBD-MySQL perl-Time-HiRes -y
下載rpm包:
PT工具須要兩個rpm包,即:
perl-TermReadKey-2.30-13.el6.x86_64.rpm percona-toolkit-3.0.11-1.el6.x86_64.rpm
下載地址分別爲:
https://www.percona.com/downloads/percona-toolkit/3.0.11/binary/redhat/6/x86_64/percona-toolkit-3.0.11-1.el6.x86_64.rpm
http://mirror.centos.org/centos/6/os/x86_64/Packages/perl-TermReadKey-2.30-13.el6.x86_64.rpm
使用rpm命令安裝
[root@mysql02 src]# rpm -ivh perl-TermReadKey-2.30-13.el6.x86_64.rpm Preparing... ########################################### [100%] 1:perl-TermReadKey ########################################### [100%] [root@mysql02 src]# rpm -ivh percona-toolkit-3.0.11-1.el6.x86_64.rpm warning: percona-toolkit-3.0.11-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY Preparing... ########################################### [100%] 1:percona-toolkit ########################################### [100%]
官網:https://github.com/box/Anemometer
cd /usr/local/src &&\ wget https://github.com/box/Anemometer/archive/master.zip
在安裝以前須要先確認epel-release,php_mysqli模塊須要版本epel-release-6-8.noarch纔會有。咱們在以前安裝PT工具的時候已經更新了epel源,所以能夠直接yum安裝。
yum -y install httpd php *bcmath* *mysqli*
檢查安裝結果:
[root@mysql02 ~]# rpm -qa *bcmath* *mysqli* php-bcmath-5.3.3-49.el6.x86_64 php-pear-MDB2-Driver-mysqli-1.5.0-0.8.b4.el6.noarch #<==若是沒有這兩個,必須先安裝
移動Anemometer到http站點目錄:
unzip master #<==下載方式不一樣,包名也可能不一樣,所以需注意本身的包名 mv Anemometer-master /var/www/html/anemometer
運行sql腳本:
進入/var/www/html/anemometer,將mysql腳本導入到數據庫中
mysql -uroot -p123456 < mysql56-install.sql
賦予數據庫相應的權限:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | slow_query_log | +--------------------+ 4 rows in set (0.15 sec) mysql> grant all on slow_query_log.* to 'anemometer'@'%' identified by '123456'; Query OK, 0 rows affected (0.01 sec) mysql> grant all on slow_query_log.* to 'anemometer'@'localhost' identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> grant select on *.* to 'anemometer'@'%'; Query OK, 0 rows affected (0.00 sec) mysql> grant all on slow_query_log.* to 'anemometer'@'localhost'; Query OK, 0 rows affected (0.00 sec) mysql> grant select on *.* to 'anemometer'@'localhost'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
anemometer配置:
vim /var/www/html/anemometer/conf/sample.config.inc.php
php配置:
vim /etc/php.ini,注意本身的socket文件所在位置
apache配置:
vim /etc/httpd/conf/httpd.conf
啓動http服務
啓動httpd服務,並訪問網址:http://10.0.0.26/ anemometer
根據提示,修改配置文件名:
cp /var/www/html/anemometer/conf/sample.config.inc.php /var/www/html/anemometer/conf/config.inc.php`
修改完成配置文件,進入系統以後會有以下報錯:
不要急,這是由於缺省了兩個表,運行以下命令便可:
pt-query-digest --user=anemometer --password=123456 --socket=/opt/mysql/data/mysql.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\"" \ /opt/mysql/data/slow.log
到此,完成全部安裝配置!結果展現以下: