0

0

安装MySQL慢查询日志工具Anemometer

php中文网

php中文网

发布时间:2016-06-06 20:14:42

|

1805人浏览过

|

来源于php中文网

原创

首先安装LNMP环境,要求PHP-5.3以上版本. 参考:http://isadba.com/?p=82 或者参考http://isadba.com/?p=572 然后下载Anemometer git clonehttps://github.com/box

首先安装LNMP环境,要求PHP-5.3以上版本.
参考:?p=82 或者参考 ?p=572

然后下载Anemometer
git clone https://github.com/box/Anemometer.git anemometer

配置LNMP将下载下来的anemometer部署到LAMP上面,可以通过web打开页面,会提示你没有配置网站.
mysql -uroot -p mysql -uroot -p

安装percona的toolkit工具
yum install
yum install percona-toolkit -y

修改配置文件
cd /var/www/anemometer/conf/
cp sample.config.inc.php config.inc.php
修改35-40行的数据库配置,连接到anemometer的数据库,当然,你数据库也需要授权.

#使用pt-query-digest工具,将一些慢查询数据导入数据库
pt-query-digest –user=anemometer –password=anemometerpass –review h=192.168.11.28,D=slow_query_log,t=global_query_review \
–history h=192.168.11.28,D=slow_query_log,t=global_query_review_history \
–no-report –limit=0% –filter=” \$event->{Bytes} = length(\$event->{arg}) and \$event->{hostname}=\”$HOSTNAME\”" \
/usr/local/mariamysql/data/localhost-slow.log

执行命令以后,h=192.168.11.28,d=slow_query_log,t=global_query_review_history和global_query_review表已经已经有一些数据了,你可以尝试手工查询.

现在可以再通过web访问anemometer,就能看到相关信息了.

到这一步,anemometer算是跑通了,下一步就是真正的部署使用.

现在的环境是,有多台MySQL服务器的slow log需要监控.
我们有三种方案.
1、手工导入的install.sql里面的global_query_review_history表包含了hostname_max和db_max,通过hostname_max区分把多个数据源存放到一个表里.
2、每台mysql把处理后的binlog数据放到当前服务器,然后anemometer连接对应的服务器获取数据.
3、所有的mysql把处理后的binlog数据统一存放到anemometer所在的数据库上,然后通过表名或者数据库名字区分.

如果选择第二种或者第三种方案,需要定义多个数据源,并且anemometer_collect.sh.里面对应的history_db_name要对应.
在conf/config.inc.php总修改数据源的配置,注意可以不同的数据源指向的是不同的db,一般可以按照项目区分.
在conf/config.inc.php总修改数据源的配置,注意不同的数据源指向的是不同的db.

$conf['datasources']['192.168.11.28'] = array( 'host' => '192.168.11.28', 'port' => 3306, 'db' => 'slow_query_log', 'user' => 'anemometer', 'password' => 'anemometerpass', 'tables' => array( 'global_query_review' => 'fact', 'global_query_review_history' => 'dimension' ), 'source_type' => 'slow_query_log' ); $conf['datasources']['192.168.11.17'] = array( 'host' => '192.168.11.28', 'port' => 3306, 'db' => 'slow_query_log_192_168_11_17', 'user' => 'anemometer', 'password' => 'anemometerpass', 'tables' => array( 'global_query_review' => 'fact', 'global_query_review_history' => 'dimension' ), 'source_type' => 'slow_query_log' );

在所有MySQL上部署scripts/anemometer_collect.sh收集慢查询日志.
anemometer_collect.sh脚本有几个注意点
1、PATH里面是否有mysql的bin路径
2、如果history_db_name需要根据不同机器做修改,和config.inc.php定义的对应
3、注意原始慢查询日志路径是否正确
4、通过设置long_query_time设置慢查询的时长
5、所有MySQL服务器上需要安装percona-toolkit的工具箱.

并且在anemometer_collect.sh当前目录下增加下面两个配置文件.
连接到本地MySQL服务器的账号,需要有super权限.

[root@localhost scripts]# cat anemometer-localhost.cnf [client] user=root password= host=localhost socket=/tmp/mysql.sock

连接到anemometer服务器数据库的账号,需要对anemometer_collect.sh脚本中history_db_name对应的数据库有all privileges权限.

MyMap AI
MyMap AI

使用AI将想法转化为图表

下载
[root@localhost scripts]# cat anemometer-server.cnf [client] user=anemometer password=anemometerpass

使用下面命令调试脚本.

sh -x ./anemometer_collect.sh --interval 30 --history-db-host 192.168.11.17 --defaults-file ./anemometer-localhost.cnf --history-defaults-file ./anemometer-server.cnf

调试通过以后,在crontab添加如下命令,实现定期采集慢查询日志到数据库存储.间隔可以根据自己的需求设置,我的设置基本上是采集所有的慢查询日志.
*/1 * * * * /opt/anemometer_collect.sh –interval 59 –history-db-host 192.168.11.28 –defaults-file /opt/anemometer-localhost.cnf –history-defaults-file /opt/anemometer-server.cnf

由于anemometer不能跟踪SQL是哪个账号,哪个主机的请求,所以我们有必要保存原始日志,来做例外分析.
下面是我修改后的anemometer_collect.sh,增加了可定制的慢查询问题,mysql path,慢查询时长变量设置,保存历史的慢查询日志.

[root@localhost scripts]# cat anemometer_collect.sh #/usr/bin/env bash # anemometer collection script to gather and digest slow query logs # this is a quick draft script so please give feedback! # # basic usage would be to add this to cron like this: # */5 * * * * anemometer_collect.sh --interval 15 --history-db-host anemometer-db.example.com # # This will have to run as a user which has write privileges to the mysql slow log # # Additionally there are two sets of permissions to worry about: The local mysql instance, and the remote digest storage instance # These are handled through defaults files, just create a file in the: my.cnf format such as: # [client] # user= # password= # # use --defaults-file for permissions to the local mysql instance # and use --history-defaults-file for permissions to the remote digest storage instance # # PATH=/usr/local/mysql-6/bin/:$PATH socket= defaults_file= rate_limit= mysqlopts= interval=30 digest='/usr/bin/pt-query-digest' #set log prefix LOG_PREFIX='/usr/local/mariamysql/data/' #set slow log history file LOG_HISTORY_FILE='/var/log/slow_query_log_history_3306' long_query_time=0.1 #SET IP HOSTNAME=`/sbin/ifconfig | grep 'inet addr' | egrep '172.|192.' | awk '{print $2}' | awk -F ":" '{print $2}'` PORT=3306 HOSTNAME="$HOSTNAME\:$PORT" history_db_host= history_db_port=3306 history_db_name='slow_query_log' history_defaults_file= help () { cat &2 "Invalid argument: $1" ;; esac shift done if [ ! -e "${digest}" ]; then echo "Error: cannot find digest script at: ${digest}" exit 1 fi if [ ! -z "${defaults_file}" ]; then mysqlopts="--defaults-file=${defaults_file}" fi # path to the slow query log LOG=$( mysql $mysqlopts -e " show global variables like 'slow_query_log_file'" -B | tail -n1 | awk '{ print $2 }' ) LOG="$LOG_PREFIX$LOG" if [ $? -ne 0 ]; then echo "Error getting slow log file location" exit 1 fi echo "Collecting from slow query log file: ${LOG}" # simple 30 second collection if [ ! -z "${rate}" ]; then mysql $mysqlopts -e "SET GLOBAL log_slow_rate_limit=${rate}" fi mysql $mysqlopts -e "SET GLOBAL long_query_time=$long_query_time" mysql $mysqlopts -e "SET GLOBAL slow_query_log=1" if [ $? -ne 0 ]; then echo "Error: cannot enable slow log. Aborting" exit 1 fi echo "Slow log enabled; sleeping for ${interval} seconds" sleep "${interval}" mysql $mysqlopts -e "SET GLOBAL slow_query_log=0" echo "Done. Processing log and saving to ${history_db_host}:${history_db_port}/${history_db_name}" # process the log if [[ ! -e "$LOG" ]] then echo "No slow log to process"; exit fi mv "$LOG" /tmp/tmp_slow_log if [ ! -z "${history_defaults_file}" ]; then pass_opt="--defaults-file=${history_defaults_file}" fi "${digest}" $pass_opt \ --review h="${history_db_host}",D="$history_db_name",t=global_query_review \ --history h="${history_db_host}",D="$history_db_name",t=global_query_review_history \ --no-report --limit=0\% \ --filter="\$event->{Bytes} = length(\$event->{arg}) and \$event->{hostname}=\"$HOSTNAME\" " \ "/tmp/tmp_slow_log" #store history slow_log cat /tmp/tmp_slow_log >> $LOG_HISTORY_FILE ,

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Word 字间距调整方法汇总
Word 字间距调整方法汇总

本专题整合了Word字间距调整方法,阅读下面的文章了解更详细操作。

2

2025.12.24

任务管理器教程
任务管理器教程

本专题整合了任务管理器相关教程,阅读下面的文章了解更多详细操作。

2

2025.12.24

AppleID格式
AppleID格式

本专题整合了AppleID相关内容,阅读专题下面的文章了解更多详细教程。

0

2025.12.24

csgo视频观看入口合集
csgo视频观看入口合集

本专题整合了csgo观看入口合集,阅读下面的文章了知道更多入口地址。

29

2025.12.24

yandex外贸入口合集
yandex外贸入口合集

本专题汇总了yandex外贸入口地址,阅读下面的文章了解更多内容。

58

2025.12.24

添加脚注通用方法
添加脚注通用方法

本专题整合了添加脚注方法合集,阅读专题下面的文章了解更多内容。

1

2025.12.24

重启电脑教程汇总
重启电脑教程汇总

本专题整合了重启电脑操作教程,阅读下面的文章了解更多详细教程。

3

2025.12.24

纸张尺寸汇总
纸张尺寸汇总

本专题整合了纸张尺寸相关内容,阅读专题下面的文章了解更多内容。

5

2025.12.24

Java Spring Boot 微服务实战
Java Spring Boot 微服务实战

本专题深入讲解 Java Spring Boot 在微服务架构中的应用,内容涵盖服务注册与发现、REST API开发、配置中心、负载均衡、熔断与限流、日志与监控。通过实际项目案例(如电商订单系统),帮助开发者掌握 从单体应用迁移到高可用微服务系统的完整流程与实战能力。

1

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
CSS3 教程
CSS3 教程

共18课时 | 4万人学习

Django 教程
Django 教程

共28课时 | 2.4万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 2.6万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号