本文共 11273 字,大约阅读时间需要 37 分钟。
改进版
可以建立主从机制,在从上执行慢查询语句分析
主
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | cat / usr / local / sbin / lepus_slowquery.sh #!/bin/bash #****************************************************************# # ScriptName:/usr/local/sbin/lepus_slowquery.sh # Create Date:2014-03-25 10:01 # Modify Date:2014-03-25 10:01 #***************************************************************# #configlepus database server监控机 lepus_db_host = "172.1.1.197" lepus_db_port = 3306 lepus_db_user = "lepus_user" lepus_db_password = "lepus_password" lepus_db_database = "lepus" #configmysql server被监控机 mysql_client = "/opt/mysql/bin/mysql" mysql_host = "172.1.1.221" mysql_port = 3306 mysql_user = "lepus_monitor" mysql_password = "lepus_password" #configslowqury慢日志位置 slowquery_dir = "/home/log/mysql/" slowquery_long_time = 0.5 slowquery_file = `$mysql_client - h$mysql_host - P$mysql_port - u$mysql_user - p$mysql_password - e "show variables like'slow_query_log_file'" |grep log|awk '{print $2}' ` pt_query_digest = "/usr/local/bin/pt-query-digest" #config server_id被监控机id从lepus图形界面看 lepus_server_id = 275 #collect mysqlslowquery 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 --history 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_server_id " $slowquery_file >/tmp/lepus_slowquery.log ##### set a new slowquery log ########### tmp_log = `$mysql_client - h$mysql_host - P$mysql_port - u$mysql_user - p$mysql_password - e "select concat('$slowquery_dir','slowquery_',date_format(now(),'%Y%m%d%H'),'.log');" |grep log|sed - n - e '2p' ` #config mysqlslowquery $mysql_client - h$mysql_host - P$mysql_port - u$mysql_user - p$mysql_password - 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 - e "set global slow_query_log_file = '$tmp_log'; " echo "$tmp_log" >$slowquery_dir / version.txt #delete log before 7days cd $slowquery_dir / usr / bin / find . / - name 'slowquery_*.log' - mtime + 14 |xargs rm - rf ; ###同步慢查询日志到从服务上 src = / home / log / mysql / dest = mysql ip = 172.1 . 1.222 #/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y%H:%M' --format '%T %w %f' -e modify,delete,create,attrib $src | while read DATE TIME DIR FILE; #do #filechange=${DIR}${FILE} / usr / bin / rsync - avz - - delete - - progress $src 221mysqlbak @$ip::$dest - - password - file = / etc / rsyncd.password > / home / rsync.log 2 >& 1 #echo "At ${TIME} on ${DATE}, file $filechange was backed up via rsynce" >/home/rsync.log 2>&1 #done ####END#### |
定时任务
00 */1 * * * root /bin/bash /usr/local/sbin/lepus_slowquery.sh 1>/dev/null 2>&1 &
从
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | cat / usr / local / sbin / lepus_slowquery.sh #!/bin/bash #****************************************************************# # ScriptName:/usr/local/sbin/lepus_slowquery.sh # Create Date:2014-03-25 10:01 # Modify Date:2014-03-25 10:01 #***************************************************************# #configlepus database server监控机 lepus_db_host = "172.1.1.197" lepus_db_port = 3306 lepus_db_user = "lepus_user" lepus_db_password = "lepus_password" lepus_db_database = "lepus" #configmysql server被监控机 mysql_client = "/opt/mysql/bin/mysql" mysql_host = "172.1.1.222" mysql_port = 3306 mysql_user = "lepus_monitor" mysql_password = "lepus_password" #configslowqury慢日志位置 slowquery_dir = "/home/log/mysql/" slowquery_long_time = 0.5 #slowquery_file=`$mysql_client -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password -e "show variables like'slow_query_log_file'"|grep log|awk '{print $2}'` pt_query_digest = "/usr/local/bin/pt-query-digest" #config server_id被监控机id从lepus图形界面看 lepus_server_id = 276 slowquery_file = `cat ${slowquery_dir}version.txt` #collect mysqlslowquery 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 - - history 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_server_id " $slowquery_file > / home / lepus_slowquery.log ##### set a new slowquery log ########### #tmp_log=`$mysql_client -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password -e "select concat('$slowquery_dir','slowquery_',date_format(now(),'%Y%m%d%H'),'.log');"|grep log|sed -n -e '2p'` #config mysqlslowquery #$mysql_client -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password -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 -e "set global slow_query_log_file = '$tmp_log'; " #delete log before 7days #cd $slowquery_dir #/usr/bin/find ./ -name 'slowquery_*' -mtime +7|xargs rm -rf ; ####END#### |
定时任务
*/30 * * * * root /bin/bash /usr/local/sbin/lepus_slowquery.sh 1>/dev/null 2>&1 &
web界面点击172.1.1.222慢查询分析就代表是主的慢查询
-------------------------------------------分割线----------------------------------------------------
首先,说明几点
1.lepus3.7和lepus3.8 安装基本相同
参考http://suifu.blog.51cto.com/9167728/1770121
一定要用apache,php最好高版本,yum最好
或者
参考http://suifu.blog.51cto.com/9167728/1770493
有一键部署的lamp环境,如果是新机器,建议采用,参时间
开始安装部署
因为要监控mysql5.7,所以下载lepus3.8版本
a 监控机
1.1如果监控机的mysql也是5.7的话,那么这里有个问题,就是sql_mode,5.7默认开启了
select version(), @@sql_mode;
关闭的话,vim /etc/my.cnf
sql_mode='IGNORE_SPACE,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
这步只要在监控机上做。
主要解决php中慢查询执行sql报错,就是sql_mode引起的
1.2
lamp环境,yum略过,最好自己内部有yum服务器,那样安装起来也很快
1.3
详细步骤请参考最上面的文章
mysql> create database lepus default character set utf8;
mysql> grant select,insert,update,delete,create on lepus.* to 'lepus_user'@'%' identified by'MANAGER';
grant select,insert,update,delete,create on lepus.* to 'lepus_user'@'%' identified by 'xx';
mysql> flush privileges;
[root@HE3Lepus_v3.8_beta]# mysql -uroot -p lepus < sql/lepus_table.sql
[root@HE3Lepus_v3.8_beta]# mysql -uroot -p lepus < sql/lepus_data.sql
mysql>use lepus
mysql>alter table mysql_status modify column max_connect_errors bigint(18);
mysql>alter table mysql_status_history modify column max_connect_errors bigi
安装Lpeus程序
进入到Lepus软件包的python文件夹
# cd python/
授予install.sh可执行权限
# chmod +x install.sh
执行安装
# ./install.sh
修改配置文件
进入到安装目录,默认为/usr/local/lepus
[root@HE3~]# vi /usr/local/lepus/etc/config.ini
###监控机MySQL数据库连接地址###
[monitor_server]
host="192.168.1.250"
port=4008
user="lepus_user"
passwd="MANAGER"
dbname="lepus"
重启lepus进程
/usr/local/lepus/lepus stop
/usr/local/lepus/lepus start
[root@HE3lepus]# tail -f logs/lepus.log
安装WEB管理台
进入到你的lepus软件目录
[root@HE3 ~]# cd /home/Lepus_v3.8_beta/
[root@HE3Lepus_v3.8_beta]# cp -r php /home/www
[root@HE3 ~]# vi /home/lepus/php/application/config/database.php
$db['default']['hostname'] = '192.168.1.250'; #这是a机器
$db['default']['port'] = '4008';
$db['default']['username'] = 'lepus_user';
$db['default']['password'] = 'MANAGER';
$db['default']['database'] = 'lepus';
$db['default']['dbdriver'] = 'mysql';
b 被监控机
2.
mysql> grant select,super,process,reload,show databases,replication client on *.* to'lepus_monitor'@'%' identified by 'MANAGER';
mysql> flush privileges;
3.登录进行添加主机和监控
通过浏览器输入mysqlmtop1.ikongjian.com打开监控界面,即可登录系统。.默认管理员账号密码admin/Lepusadmin登录后请修改管理员密码,增加普通账号。
4.表空间的监控,添加代码
job = Process(target = job_run, args = ('check_mysql_bigtable',int(frequency_monitor)*120))
joblist.append(job)
job.start()
5.慢查询
[root@ot21 bin]# service crond restart
停止 crond: [确定]
正在启动 crond: [确定]
[root@ot21 bin]# cat /usr/local/sbin/lepus_slowquery.sh
#!/bin/bash
#****************************************************************#
# ScriptName:/usr/local/sbin/lepus_slowquery.sh
# Create Date:2014-03-25 10:01
# Modify Date:2014-03-25 10:01
#***************************************************************#
#configlepus database server监控机
lepus_db_host="a"
lepus_db_port=3306
lepus_db_user="lepus_user"
lepus_db_password="xx"
lepus_db_database="lepus"
#configmysql server被监控机
mysql_client="xx"
mysql_host="b"
mysql_port=3306
mysql_user="lepus_monitor"
mysql_password="xx"
#configslowqury慢日志位置
slowquery_dir="/log/mysql/"
slowquery_long_time=0.5
slowquery_file=`$mysql_client -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password -e "show variables like'slow_query_log_file'"|grep log|awk '{print $2}'`
pt_query_digest="/usr/local/bin/pt-query-digest"
#config server_id被监控机id从lepus图形界面看
lepus_server_id=275
#collect mysqlslowquery 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 --history 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_server_id " $slowquery_file >/tmp/lepus_slowquery.log
##### set a new slowquery log ###########
tmp_log=`$mysql_client -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password -e "select concat('$slowquery_dir','slowquery_',date_format(now(),'%Y%m%d%H'),'.log');"|grep log|sed -n -e '2p'`
#config mysqlslowquery
$mysql_client -h$mysql_host -P$mysql_port -u$mysql_user -p$mysql_password -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 -e "set global slow_query_log_file = '$tmp_log'; "
#delete log before 7days
cd $slowquery_dir
/usr/bin/find ./ -name 'slowquery_*' -mtime +7|xargs rm -rf ;
####END####
定时任务
*/5 * * * * root /bin/bash /usr/local/sbin/lepus_slowquery.sh 1>/dev/null 2>&1 &
慢查询图片
8 | 7.162 | 6.189 | 9.012 | 0.0685 | 0.00014 | 0.03841 |
6.生成awr报告
7.定时邮件发送
完成。
注意问题
1.pt-query-digest 2.2.19
2.chown -R mysql.mysql /log/mysql
3.grant select,super,process,reload,show databases,replication client on *.* to'lepus_monitor'@'%' identified by 'xx';
4.lepus_server_id=275
5.sql_mode
附上查询语句
select t2.* from
(select a.checksum,a.fingerprint,a.sample,a.first_seen,a.last_seen,b.serverid_max,
b.db_max,b.user_max,b.ts_min,b.ts_max,
sum(b.ts_cnt) as ts_cnt,
sum(b.Query_time_sum)/sum(b.ts_cnt) as Query_time_avg,
max(b.Query_time_max) as Query_time_max,
min(b.Query_time_min) as Query_time_min,
b.Query_time_sum as Query_time_sum,
max(b.Lock_time_max) as Lock_time_max,
min(b.Lock_time_min) as Lock_time_min,
sum(b.Lock_time_sum) as Lock_time_sum
from mysql_slow_query_review a join mysql_slow_query_review_history b
where a.checksum=b.checksum and b.serverid_max='275'
group by a.checksum ) t2 where t2.Query_time_avg>4 and t2.ts_cnt>1000
-- order by Query_time_sum desc
order by t2.ts_cnt desc
本文转自 liqius 51CTO博客,原文链接:http://blog.51cto.com/szgb17/1887605,如需转载请自行联系原作者