一个简易的MysQL性能查询脚本

时间:2024-01-01 08:58:09

如下:

#!/bin/sh
mysqladmin -P3306 -uroot -p'password' -r -i ext |\
awk -F"|" \
"BEGIN{ count=0; }"\
'{ if($2 ~ /Variable_name/ && ((++count)%20 == 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 ($ ~ /Queries/){queries=$;}\
else if ($ ~ /Com_select /){com_select=$;}\
else if ($ ~ /Com_insert /){com_insert=$;}\
else if ($ ~ /Com_update /){com_update=$;}\
else if ($ ~ /Com_delete /){com_delete=$;}\
else if ($ ~ /Innodb_rows_read/){innodb_rows_read=$;}\
else if ($ ~ /Innodb_rows_deleted/){innodb_rows_deleted=$;}\
else if ($ ~ /Innodb_rows_inserted/){innodb_rows_inserted=$;}\
else if ($ ~ /Innodb_rows_updated/){innodb_rows_updated=$;}\
else if ($ ~ /Innodb_buffer_pool_read_requests/){innodb_lor=$;}\
else if ($ ~ /Innodb_buffer_pool_reads/){innodb_phr=$;}\
else if ($ ~ /Uptime / && count >= ){\
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);\
}}'

输出如下:

一个简易的MysQL性能查询脚本

这个脚本主要利用了mysqladmin提供的功能

-r, --relative      Show difference between current and previous values when
used with -i. Currently only works with extended-status.
extended-status       Gives an extended status message from the server

ext是extended-status的缩写~