您好,欢迎访问一九零五行业门户网

安装MySQL慢查询日志工具Anemometer

首先安装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=rootpassword=host=localhostsocket=/tmp/mysql.sock连接到anemometer服务器数据库的账号,需要对anemometer_collect.sh脚本中history_db_name对应的数据库有all privileges权限.
[root@localhost scripts]# cat anemometer-server.cnf[client]user=anemometerpassword=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/:$pathsocket= defaults_file= rate_limit= mysqlopts=interval=30digest='/usr/bin/pt-query-digest'#set log prefixlog_prefix='/usr/local/mariamysql/data/'#set slow log history filelog_history_file='/var/log/slow_query_log_history_3306'long_query_time=0.1#set iphostname=`/sbin/ifconfig | grep 'inet addr' | egrep '172.|192.' | awk '{print $2}' | awk -f : '{print $2}'`port=3306hostname=$hostname\:$porthistory_db_host=history_db_port=3306history_db_name='slow_query_log'history_defaults_file=help () {cat &2 invalid argument: $1;;esacshiftdoneif [ ! -e ${digest} ];thenecho error: cannot find digest script at: ${digest}exit 1fiif [ ! -z ${defaults_file} ];thenmysqlopts=--defaults-file=${defaults_file}fi# path to the slow query loglog=$( mysql $mysqlopts -e show global variables like 'slow_query_log_file' -b | tail -n1 | awk '{ print $2 }' )log=$log_prefix$logif [ $? -ne 0 ];thenecho error getting slow log file locationexit 1fiecho collecting from slow query log file: ${log}# simple 30 second collectionif [ ! -z ${rate} ];thenmysql $mysqlopts -e set global log_slow_rate_limit=${rate}fimysql $mysqlopts -e set global long_query_time=$long_query_timemysql $mysqlopts -e set global slow_query_log=1if [ $? -ne 0 ];thenecho error: cannot enable slow log. abortingexit 1fiecho slow log enabled; sleeping for ${interval} secondssleep ${interval}mysql $mysqlopts -e set global slow_query_log=0echo done. processing log and saving to ${history_db_host}:${history_db_port}/${history_db_name}# process the logif [[ ! -e $log ]]thenecho no slow log to process;exitfimv $log /tmp/tmp_slow_logif [ ! -z ${history_defaults_file} ];thenpass_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_logcat /tmp/tmp_slow_log >> $log_history_file,
其它类似信息

推荐信息