mysqladmin 是 MySQL 官方客戶端工具,除關閉資料庫外,更能即時輸出效能指標、處理程序資訊。善用 extended-status 搭配 -r(相對值)、-i(輪詢間隔)、grep、awk,可打造輕量級即時監控儀表板。
mysqladmin -uroot -p -r -i 1 extended-status-r(--relative):顯示累計指標的差值(即每秒增量)-i 1(--sleep=1):每 1 秒刷新一次mysqladmin -uroot -r -i 1 -pxxx extended-status \
| grep "Questions|Queries|Innodb_rows|Com_select|Com_insert|Com_update|Com_delete"
mysqladmin -uroot -p -h127.0.0.1 -P3306 -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;
}'
輸出範例:
<------------- 12:38:49 ------------->
Com_delete 0
Com_insert 0
Com_select 0
Innodb_buffer_pool_read_requests 589
Queries 105
Questions 1
mysqladmin -P3306 -uroot -p -h127.0.0.1 -r -i 1 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 ($2 ~ /Queries/) queries=$3;
else if ($2 ~ /Com_select/) com_select=$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);
}
}"
輸出對齊的 QPS、命令計數、InnoDB 行操作、緩衝池讀取等關鍵指標,適合終端機即時監控。