archery 开启sql慢日志
1. 开启 mysql 记录慢日志
slow_query_log_file =/usr/local/mysql/log/mysql-slow.log
slow_query_log =1
long_query_time =2
log_timestamps =SYSTEM
log_timestamps 默认是utc时间,会导致archery里显示的时间不正确,此处更改为system。可动态更改,如:
mysql> set global log_timestamps=system;mysql> show variables like 'log_timestamps';
2. 重启mysql
3. 在mysql机器安装 pt-query-digest 查询日志分析工具
yum -y install percona-toolkit
4. 尝试用 pt-query-digest 分析第一步中mysql导出的慢日志文件
pt-query-digest /usr/local/mysql/log/mysql-slow.log
5. 使用mysql_slow_query_review.sql创建慢日志收集表(一般安装好archery,并初始化sql后已经有这两张表了)
mysql_slow_query_review 表和 mysql_slow_query_review_history 表
github地址:https://github.com/hhyo/Archery/blob/master/src/init_sql/mysql_slow_query_review.sql
6. 编辑此sh文件,在mysql机器上加入crontab运行,看需求设置循环时间
#!/bin/bash DIR="$( cd "$( dirname "$0" )" && pwd )" cd ${DIR} #配置Archery数据库的连接地址 archery_db_host="192.168.1.1" archery_db_port=3306 archery_db_user="root" archery_db_password="123456" archery_db_database="archery" #被分析实例的慢日志位置,建议定期清理日志文件,否则会影响分析效率 slowquery_file="/usr/local/mysql/log/mysql-slow.log" #pt-query-digest可执行文件路径 pt_query_digest="/usr/bin/pt-query-digest" #被分析实例的连接信息 hostname="192.168.1.2:3306" # 需要和Archery实例配置中的内容保持一致,用于筛选,配置错误会导致数据无法展示 #获取上次分析时间,初始化时请删除last_analysis_time_$hostname文件,可分析全部日志数据 if [[ -s last_analysis_time_${hostname} ]]; then last_analysis_time=`cat last_analysis_time_${hostname}` else last_analysis_time='1000-01-01 00:00:00' fi #收集日志 #RDS需要增加--no-version-check选项 ${pt_query_digest} \ --user=${archery_db_user} --password=${archery_db_password} --port=${archery_db_port} \ --review h=${archery_db_host},D=${archery_db_database},t=mysql_slow_query_review \ --history h=${archery_db_host},D=${archery_db_database},t=mysql_slow_query_review_history \ --no-report --limit=100% --charset=utf8 \ --since "$last_analysis_time" \ --filter="\$event->{Bytes} = length(\$event->{arg}) and \$event->{hostname}=\"$hostname\" and \$event->{client}=\$event->{ip} " \ ${slowquery_file} > /tmp/analysis_slow_query.log if [[ $? -ne 0 ]]; then echo "failed" else echo `date "+%Y-%m-%d %H:%M:%S"`>last_analysis_time_${hostname} fi

浙公网安备 33010602011771号