首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> MySQL 資料庫 >> MySQL 綜合教學

MySQL DBA 必備:mysqladmin 實時性能監控一鍵腳本 (含 awk 進化版)

     編輯:MySQL 綜合教學

核心能力:extended-status + 相對值模式

mysqladmin extended-status 等價於 SHOW GLOBAL STATUS,但加上 -r (--relative) 參數後,輸出變量的每秒增量 (差值),直接反映當前負載,無需手工計算。

進化版腳本:從基礎到生產級

Level 1:基礎輪詢

mysqladmin -uroot -p -r -i 1 extended-status
# -r: 顯示差值
# -i 1: 每秒刷新一次

Level 2:grep 過濾核心指標

mysqladmin -uroot -p -r -i 1 extended-status \
| grep -E "Questions|Queries|Innodb_rows|Com_select|Com_insert|Com_update|Com_delete"

Level 3:awk 添加時間戳

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;
  }
}'

Level 4:生產級儀表盤 (推薦收藏)

mysqladmin -P3306 -uroot -p -h127.0.0.1 -r -i 1 ext | \
awk -F"|" '
BEGIN { count=0; }
{
  if($2 ~ /Variable_name/ && ++count == 1) {
    print "----------|---------|--- MySQL Command Status --|----- Innodb Row Ops ----|-- Buffer Pool Reads --";
    print "---Time---|---QPS---|Sel Ins Upd Del|Read Ins Upd Del|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_inserted/) innodb_rows_inserted=$3;
  else if ($2 ~ /Innodb_rows_updated/) innodb_rows_updated=$3;
  else if ($2 ~ /Innodb_rows_deleted/) innodb_rows_deleted=$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("|%4d %3d %3d %3d", com_select, com_insert, com_update, com_delete);
    printf("|%4d %4d %4d %4d", innodb_rows_read, innodb_rows_inserted, innodb_rows_updated, innodb_rows_deleted);
    printf("|%8d %9d\n", innodb_lor, innodb_phr);
  }
}'

輸出示例

----------|---------|--- MySQL Command Status --|----- Innodb Row Ops ----|-- Buffer Pool Reads --
---Time---|---QPS---|Sel Ins Upd Del|Read Ins Upd Del|Logical Physical
 10:37:13 |    2231| 274 214  70   0| 4811  160   71   0|    4146        0
 10:37:14 |    2972| 403 256  84  23| 2509  173   85  23|    4545        0
 10:37:15 |    2334| 282 232  66   1| 1266  154   67   1|    3543        0
 10:37:16 |    2241| 271 217  66   0| 1160  129   66   0|    2935        0
 10:37:17 |    2497| 299 224  97   0| 1141  149   95   0|    3831        0

指標解讀速查表

指標含義異常閾值參考
Queries / Questions總查詢數/客戶端發送查詢數QPS 突降 > 50% → 疑似阻塞/死鎖
Com_select/insert/update/delete各類 DML 執行次數Com_update 激增 → 熱點行鎖衝突
Innodb_rows_read/inserted/updated/deleted存儲引擎層行操作數Rows_read >> Com_select → 全表掃描/索引失效
Innodb_buffer_pool_read_requests邏輯讀 (內存命中)基準值,配合物理讀算命中率
Innodb_buffer_pool_reads物理讀 (磁盤 I/O)> 0 且持續增長 → Buffer Pool 太小/冷啟動
Buffer Pool 命中率1 - (Physical / Logical)< 99% 需關注,< 95% 嚴重

一鍵部署腳本 (save as mysql_monitor.sh)

#!/bin/bash
# Usage: ./mysql_monitor.sh [host] [port] [user] [interval_sec]
HOST=${1:-127.0.0.1}
PORT=${2:-3306}
USER=${3:-root}
INTERVAL=${4:-1}
read -s -p "MySQL Password: " PASS
echo

mysqladmin -h"$HOST" -P"$PORT" -u"$USER" -p"$PASS" -r -i "$INTERVAL" ext | \
awk -F"|" '
BEGIN { c=0; }
{
  if($2 ~ /Variable_name/ && ++c == 1) {
    print "----------|---------|--- CMD ---|----- ROW OPS -----|-- BUF POOL --";
    print "---Time---|---QPS---|Sel Ins Upd Del|Read Ins Upd Del|Logic Phys";
  }
  else if ($2 ~ /Queries$/) q=$3;
  else if ($2 ~ /Com_select /) cs=$3;
  else if ($2 ~ /Com_insert /) ci=$3;
  else if ($2 ~ /Com_update /) cu=$3;
  else if ($2 ~ /Com_delete /) cd=$3;
  else if ($2 ~ /Innodb_rows_read$/) irr=$3;
  else if ($2 ~ /Innodb_rows_inserted$/) iri=$3;
  else if ($2 ~ /Innodb_rows_updated$/) iru=$3;
  else if ($2 ~ /Innodb_rows_deleted$/) ird=$3;
  else if ($2 ~ /Innodb_buffer_pool_read_requests$/) ior=$3;
  else if ($2 ~ /Innodb_buffer_pool_reads$/) ipr=$3;
  else if ($2 ~ /Uptime / && c >= 2) {
    printf(" %s |%9d", strftime("%H:%M:%S"), q);
    printf("|%4d %3d %3d %3d", cs, ci, cu, cd);
    printf("|%4d %4d %4d %4d", irr, iri, iru, ird);
    printf("|%7d %7d\n", ior, ipr);
  }
}'

總結

mysqladmin -r -i 1 是 DBA 排查突發性能問題的「聽診器」:零部署、零侵入、秒級反饋。配合 awk 定製儀表盤,將關鍵指標 (QPS、行操作、緩存命中) 可視化,比 Grafana/Prometheus 更輕量、更即時。建議收藏 Level 4 腳本,故障時一鍵啟動,直擊現場。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved