通过pt-query-digest分析收集MySQL慢查询日志到指定位置



说明:本脚本是基于开源工具Lepus监控

           需要安装percona-toolkit工具包

慢查询时间:主库1s,只读库3s

一台服务器上单实例或者多实例都可执行,本例子是单实例和双实例的环境

可将本脚本通过OS定时任务执行,便于慢查询的分析汇总。


#!/bin/bash
#config lepus database server
lepus_db_host="xxx"
lepus_db_port=xxx
lepus_db_user="xxx"
lepus_db_password="xxx"
lepus_db_database="lepus"

#config mysql server
mysql_client="/usr/bin/mysql"
mysql_host="localhost"
mysql_port=xxx
mysql_user="xxx"
mysql_password="xxx"
mysql_socket="xxx"

#pt-query-digest
pt_query_digest="/usr/local/bin/pt-query-digest"

#mysqlslow
slowquery_long_time=1

#define mysql slowquery dir
if [ -d "/data/lib/mysql" ]; then
    arr_slowdir=("/data/lib/mysql/")
elif [ -d "/data/lib/mysqla/" ]; then
    arr_slowdir=("/data/lib/mysqla/" "/data/lib/mysqlb/")
fi

for (( i=0; i<${#arr_slowdir[@]}; i++ ))
do
    slowquery_dir=${arr_slowdir[i]}
    if [ "$slowquery_dir" = "/data/lib/mysqlb/" ]; then
        mysql_port=3307
        mysql_socket="/data/lib/mysqlb/mysql.sock"
    elif [ "$slowquery_dir" = "/data/lib/mysqla/" ]; then
        mysql_port=3306
        mysql_socket="/data/lib/mysqla/mysql.sock"
    fi
   
    #mysql readonly is on or off
    read_only=`$mysql_client -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password --socket=$mysql_socket -e "show variables like 'read_only'"|grep O|awk '{print $2}'`   
    if [ "$read_only" = "ON" ]; then
        slowquery_long_time=3
    fi
   
    #mysql slowquery file
    slowquery_file=`$mysql_client -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password  --socket=$mysql_socket -e "show variables like 'slow_query_log_file'"|grep log|awk '{print $2}'`
   
    #config server_id
    host_ip="`/sbin/ifconfig|grep "inet addr:"|cut -d ":" -f2|awk '{print $1}'|head -1`"
    lepus_server_id=`$mysql_client -h$lepus_db_host -P$lepus_db_port -u$lepus_db_user -p$lepus_db_password -D$lepus_db_database -e "SELECT server_id FROM db_status WHERE host='$host_ip'"|sed
-n -e '2p'`

    #collect mysql slowquery log into lepus database
    $pt_query_digest --user=$lepus_db_user --password=$lepus_db_password --port=$lepus_db_port --charset=utf8 --review h=$lepus_db_host,D=$lepus_db_database,t=mysql_slow_query_review  --histo
ry h=$lepus_db_host,D=$lepus_db_database,t=mysql_slow_query_review_history  --no-report --limit=100% --filter="\$event->{add_column} = length(\$event->{arg}) and \$event->{serverid}=$lepus_se
rver_id" $slowquery_file > /data/mysqlslow/lepus_slowquery.log   

    ##### set a new slow query log ###########
    tmp_log=`$mysql_client -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password --socket=$mysql_socket -e "select concat('$slowquery_dir','slowquery_',date_format(now(),'%Y%m%d%H%i'),'
.log');"|grep log|sed -n -e '2p'`

    #config mysql slowquery
    $mysql_client -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password --socket=$mysql_socket -e "set global slow_query_log=1;set global long_query_time=$slowquery_long_time;"
    $mysql_client -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password --socket=$mysql_socket -e "set global slow_query_log_file = '$tmp_log'; "
 
    #delete log before 5 days
    cd $slowquery_dir
    /usr/bin/find ./ -name 'slowquery_*' -mtime +5|xargs rm -rf
done

你可能感兴趣的:(MySQL)