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

Zabbix 監控 MySQL 完整實施步驟

     編輯:MySQL 綜合教學

Zabbix 監控 MySQL 完整實施步驟

結合 Zabbix Agent 自定義腳本與內建模板,實現 MySQL 關鍵指標採集。

1. 編寫採集腳本

路徑:/usr/local/zabbix/scripts/chk_mysql.sh

#!/bin/bash
MYSQL_USER='root'
MYSQL_PWD='your_password'
MYSQL_HOST='127.0.0.1'
MYSQL_PORT='3306'
MYSQL_CONN="/usr/local/mysql/bin/mysqladmin -u$MYSQL_USER -p$MYSQL_PWD -h$MYSQL_HOST -P$MYSQL_PORT"

case $1 in
  Uptime)        result=`$MYSQL_CONN status | cut -f2 -d":" | cut -f1 -d"T"` ;;
  Com_update)    result=`$MYSQL_CONN extended-status | grep -w "Com_update" | cut -d"|" -f3` ;;
  Slow_queries)  result=`$MYSQL_CONN status | cut -f5 -d":" | cut -f1 -d"O"` ;;
  Com_select)    result=`$MYSQL_CONN extended-status | grep -w "Com_select" | cut -d"|" -f3` ;;
  Com_rollback)  result=`$MYSQL_CONN extended-status | grep -w "Com_rollback" | cut -d"|" -f3` ;;
  Questions)     result=`$MYSQL_CONN status | cut -f4 -d":" | cut -f1 -d"S"` ;;
  Com_insert)    result=`$MYSQL_CONN extended-status | grep -w "Com_insert" | cut -d"|" -f3` ;;
  Com_delete)    result=`$MYSQL_CONN extended-status | grep -w "Com_delete" | cut -d"|" -f3` ;;
  Com_commit)    result=`$MYSQL_CONN extended-status | grep -w "Com_commit" | cut -d"|" -f3` ;;
  Bytes_sent)    result=`$MYSQL_CONN extended-status | grep -w "Bytes_sent" | cut -d"|" -f3` ;;
  Bytes_received) result=`$MYSQL_CONN extended-status | grep -w "Bytes_received" | cut -d"|" -f3` ;;
  Com_begin)     result=`$MYSQL_CONN extended-status | grep -w "Com_begin" | cut -d"|" -f3` ;;
  *) echo "Usage: $0 {Uptime|Com_update|...}"; exit 1 ;;
esac
echo $result

授權:chmod +x chk_mysql.sh

2. Zabbix Agent 配置

編輯 /usr/local/zabbix-agentd/etc/zabbix_agentd.conf:

# 獲取版本
UserParameter=mysql.version,mysql -V

# 性能指標(調用腳本)
UserParameter=mysql.status[*],bash /usr/local/zabbix/scripts/chk_mysql.sh $1

# 存活檢測
UserParameter=mysql.ping,/usr/local/mysql/bin/mysqladmin -uroot -p$MYSQL_PWD -P3306 -h127.0.0.1 ping | grep -c alive

# 線程數
UserParameter=mysql.process,/usr/local/mysql/bin/mysql -uroot -p$MYSQL_PWD -P3306 -h127.0.0.1 -e "show processlist" 2>/dev/null | wc -l

3. 重啟 Agent 並導入模板

systemctl restart zabbix-agent

Zabbix Web 界面:配置 → 主機 → 模板 → 關聯 Template App MySQL(或自建模板映射上述 Key)。

4. 關鍵監控指標建議

  • 連接數:Threads_connected / max_connections
  • QPS/TPS:Questions、Com_commit、Com_rollback
  • 慢查詢:Slow_queries
  • 緩存命中:Innodb_buffer_pool_read_requests vs reads
  • 主從延遲:Seconds_Behind_Master(從庫)
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved