MySQL中强大的mysqladmin
公众号:杨建荣的学习笔记 · 作者:r13笔记第38天 · 发布:2017-07-31 00:04:59 · 原文链接
如果对MySQL的性能测试工具,比如sysbench做压力测试就可以看到我们关注的性能指标QPS,TPS,压测过程中的性能变化一目了然。
而在平时的工作中,如果也想看这个指标的值,使用sysbench就不合适了。所以我们得先了解下TPS,QPS是怎么计算的,没有sysbench这些工具,我们能不能算出来。
首先性能指标值我们是可以算出来的,我们通过show status能够得到一个实时变化的状态,里面的数值基本上都是累计值,我们可以通过设定频度来换算,由此得到一个近乎实时的状态数据。
这个时候mysqladmin就上场了,不光能够很方便的查看参数情况,比如mysqladmin var,很方便的查看参数等。mysqladmin shutdown来停库,mysqladmin flush-hosts,mysqladmin flush-privileges来刷新权限,或者使用mysqladmin pro来查看线程情况。而如果需要查看QPS,就需要使用show status的结果了。
比如我前后间隔一秒来得到一个状态值。
> show status like 'queries';+---------------+---------+| Variable_name | Value |+---------------+---------+| Queries | 9978587 |+---------------+---------+间隔1秒查看> show status like 'queries';+---------------+---------+| Variable_name | Value |+---------------+---------+| Queries | 9978588 |+---------------+---------+如果要计算出这个值,我们可以使用mysqladmin extended-status来得到,不过这是个累计值,如果想得到一个差值,还是有选项 -r -i来辅助,比如循环调用-r,间隔1秒 -i,
比如我们看一个指标 innodb_pages_read就可以使用如下的方式来查看。
# mysqladmin -r -i 1 extended-status|grep Handler_read_rnd_next| Handler_read_rnd_next | 6814 | Handler_read_rnd_next | 399 | Handler_read_rnd_next | 399 | Handler_read_rnd_next | 399
那么查看TPS呢,其实公式就很简单了,如下:
TPS = (Com_commit + Com_rollback) / Seconds
如果把这些指标值都整合起来,查看就会方便许多了,比如下面的方式,可以查看多个指标值。
mysqladmin -r -i 1 extended-status \|grep "Questions\|Queries\|Innodb_rows\|Com_select \|Com_insert \|Com_update \|Com_delete "
但是效果似乎还是可以更近一步,我看了这方面的大牛的脚本,还是很有意思的。
mysqladmin -r -i 1 ext |\awk -F"|" '{\ if($2 ~ /Variable_name/){\ print " <------------- " strftime("%H:%M:%S") " ------------->";\ }\ if($2 ~ /Questions|Queries|Innodb_rows|Com_select |Com_insert |Com_update |Com_delete |Innodb_buffer_pool_read_requests/)\ print $2 $3;\}'
运行的结果如下:
# sh aa.sh <------------- 23:07:24 -------------> Com_delete 6 Com_insert 4953706 Com_select 2672 Com_update 4 Innodb_buffer_pool_read_requests 67500881 Innodb_rows_deleted 10 Innodb_rows_inserted 8464857 Innodb_rows_read 46945213 Innodb_rows_updated 3 Queries 9978553 Questions 2454058 <------------- 23:07:25 -------------> Com_delete 0 Com_insert 0 Com_select 0 Com_update 0 Innodb_buffer_pool_read_requests 0 Innodb_rows_deleted 0 Innodb_rows_inserted 0 Innodb_rows_read 0 Innodb_rows_updated 0 Queries 1 Questions 1
再来放两个大,一个是使用awk来深度定制,这个效果牛了,脚本是参考链接里的人写的。
mysqladmin -r -i 1 ext |\awk -F"|" \"BEGIN{ count=0; }"\'{ if($2 ~ /Variable_name/ && ++count == 1){\ print "----------|---------|--- MySQL Command Status --|----- Innodb row operation ----|-- Buffer Pool Read --";\ print "---Time---|---QPS---|select insert update delete| read inserted updated deleted| logical physical";\}\else if ($2 ~ /Queries/){queries=$3;}\else if ($2 ~ /Com_select /){com_select=$3;}\else if ($2 ~ /Com_insert /){com_insert=$3;}\else if ($2 ~ /Com_update /){com_update=$3;}\else if ($2 ~ /Com_delete /){com_delete=$3;}\else if ($2 ~ /Innodb_rows_read/){innodb_rows_read=$3;}\else if ($2 ~ /Innodb_rows_deleted/){innodb_rows_deleted=$3;}\else if ($2 ~ /Innodb_rows_inserted/){innodb_rows_inserted=$3;}\else if ($2 ~ /Innodb_rows_updated/){innodb_rows_updated=$3;}\else if ($2 ~ /Innodb_buffer_pool_read_requests/){innodb_lor=$3;}\else if ($2 ~ /Innodb_buffer_pool_reads/){innodb_phr=$3;}\else if ($2 ~ /Uptime / && count >= 2){\ printf(" %s |%9d",strftime("%H:%M:%S"),queries);\ printf("|%6d %6d %6d %6d",com_select,com_insert,com_update,com_delete);\ printf("|%6d %8d %7d %7d",innodb_rows_read,innodb_rows_inserted,innodb_rows_updated,innodb_rows_deleted);\ printf("|%10d %11d\n",innodb_lor,innodb_phr);\}}'
脚本很长,结果很炫。
# sh aa.sh----------|---------|--- MySQL Command Status --|----- Innodb row operation ----|-- Buffer Pool Read -----Time---|---QPS---|select insert update delete| read inserted updated deleted| logical physical 23:10:47 | 193| 64 1 2 0| 12296 1 2 0| 1755 0 23:10:48 | 129| 44 0 2 0| 11184 0 2 0| 1582 0 23:10:49 | 89| 33 0 1 0| 11898 0 1 0| 1667 0
还有简化版
mysqladmin extended-status -i1|awk 'BEGIN{local_switch=0;print "QPS Commit Rollback TPS Threads_con Threads_run \n------------------------------------------------------- "} $2 ~ /Queries$/ {q=$4-lq;lq=$4;} $2 ~ /Com_commit$/ {c=$4-lc;lc=$4;} $2 ~ /Com_rollback$/ {r=$4-lr;lr=$4;} $2 ~ /Threads_connected$/ {tc=$4;} $2 ~ /Threads_running$/ {tr=$4; if(local_switch==0) {local_switch=1; count=0} else { if(count>10) {count=0;print "------------------------------------------------------- \nQPS Commit Rollback TPS Threads_con Threads_run \n------------------------------------------------------- ";} else{ count+=1; printf "%-6d %-8d %-7d %-8d %-10d %d \n", q,c,r,c+r,tc,tr; } }}'
运行结果如下:
# sh aa.shQPS Commit Rollback TPS Threads_con Threads_run ------------------------------------------------------- 108 0 3 3 46 2 173 0 1 1 47 2 69 0 0 0 47 2
参考资料:
http://www.orczhou.com/index.php/2014/03/some-tricky-about-mysqladmin-extended-status/
https://segmentfault.com/a/1190000009506375
