Zabbix 是企業級開源監控方案,配合自訂腳本可深入掌握 MySQL 核心效能指標(QPS、TPS、緩衝區命中率、慢查詢等)。本文涵蓋 Zabbix Server/Agent 編譯安裝、資料庫初始化、Web 介面設定,以及針對 MySQL 關鍵狀態變數的自訂監控項與觸發器建立完整流程。
yum install -y gcc mysql-devel libcurl-devel net-snmp-devel openldap-devel libxml2-devel libevent-devel pcre-devel
groupadd --system zabbix useradd --system -g zabbix -d /usr/lib/zabbix -s /sbin/nologin zabbix mysql -uroot -p -e " CREATE DATABASE zabbix CHARACTER SET utf8mb4 COLLATE utf8mb4_bin; CREATE USER 'zabbix'@'localhost' IDENTIFIED BY 'ZabbixPassw0rd!'; GRANT ALL PRIVILEGES ON zabbix.* TO 'zabbix'@'localhost'; FLUSH PRIVILEGES;"
cd /usr/src wget https://cdn.zabbix.com/zabbix/sources/stable/6.0/zabbix-6.0.15.tar.gz tar xzf zabbix-6.0.15.tar.gz cd zabbix-6.0.15 ./configure --prefix=/usr/local/zabbix \ --enable-server --enable-agent \ --with-mysql --with-net-snmp --with-libcurl --with-libxml2 make -j$(nproc) && make install
cd /usr/src/zabbix-6.0.15 mysql -uzabbix -pZabbixPassw0rd! zabbix < database/mysql/schema.sql mysql -uzabbix -pZabbixPassw0rd! zabbix < database/mysql/images.sql mysql -uzabbix -pZabbixPassw0rd! zabbix < database/mysql/data.sql
# 修改 zabbix_server.conf sed -i 's/^# DBPassword=.*/DBPassword=ZabbixPassw0rd!/' /usr/local/zabbix/etc/zabbix_server.conf sed -i 's/^# DBUser=.*/DBUser=zabbix/' /usr/local/zabbix/etc/zabbix_server.conf # 建立 systemd unit cat > /etc/systemd/system/zabbix-server.service <<'EOF' [Unit] Description=Zabbix Server After=network.target mysql.service [Service] Type=forking ExecStart=/usr/local/zabbix/sbin/zabbix_server -c /usr/local/zabbix/etc/zabbix_server.conf ExecReload=/bin/kill -HUP $MAINPID PIDFile=/var/run/zabbix/zabbix_server.pid User=zabbix Group=zabbix [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable --now zabbix-server
yum install -y nginx php-fpm php-mysqlnd php-bcmath php-mbstring php-gd php-xml php-ldap
cp -r /usr/src/zabbix-6.0.15/ui /usr/share/zabbix
chown -R nginx:nginx /usr/share/zabbix
# Nginx 設定片段
cat > /etc/nginx/conf.d/zabbix.conf <<'EOF'
server {
listen 80;
server_name zabbix.example.com;
root /usr/share/zabbix;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
EOF
systemctl enable --now nginx php-fpm
瀏覽器開啟 https://zabbix.example.com 完成初始化精靈(資料庫連線、Server 名稱等)。
| 指標 | 對應 Status 變數 | 用途 |
|---|---|---|
| QPS | Questions | 每秒查詢總數 |
| TPS | Com_commit + Com_rollback | 每秒事務提交/回滾數 |
| Select/Insert/Update/Delete | Com_select / Com_insert / Com_update / Com_delete | 各類 DML 吞吐 |
| Slow Queries | Slow_queries | 慢查詢計數 |
| InnoDB Buffer Pool Hit Rate | Innodb_buffer_pool_read_requests / Innodb_buffer_pool_reads | 緩衝區命中率 |
| Threads Running/Connected | Threads_running / Threads_connected | 並發壓力 |
| Replication Lag | Seconds_Behind_Master (從庫) | 主從延遲 |
/usr/local/zabbix/bin/mysql_stats.sh#!/bin/bash # 用法: mysql_stats.sh# 依賴: mysqladmin, mysql client, .my.cnf 免密登入 MYSQL_ADMIN="/usr/bin/mysqladmin" MYSQL_SOCKET="/var/lib/mysql/mysql.sock" MYSQL_USER="zbx_monitor" MYSQL_PASS="MonitorPassw0rd!" metric="$1" case "$metric" in Uptime) $MYSQL_ADMIN -u$MYSQL_USER -p$MYSQL_PASS -S $MYSQL_SOCKET status | awk -F'[: ]+' '/Uptime/ {print $2}' ;; Questions|Com_select|Com_insert|Com_update|Com_delete|Com_commit|Com_rollback|Slow_queries|Threads_running|Threads_connected|Bytes_sent|Bytes_received) $MYSQL_ADMIN -u$MYSQL_USER -p$MYSQL_PASS -S $MYSQL_SOCKET extended-status | awk -v m="$metric" '$2==m {print $4}' ;; Innodb_buffer_pool_read_requests|Innodb_buffer_pool_reads) $MYSQL_ADMIN -u$MYSQL_USER -p$MYSQL_PASS -S $MYSQL_SOCKET extended-status | awk -v m="$metric" '$2==m {print $4}' ;; *) echo "Unknown metric"; exit 1 ;; esac
chmod +x /usr/local/zabbix/bin/mysql_stats.sh chown zabbix:zabbix /usr/local/zabbix/bin/mysql_stats.sh
CREATE USER 'zbx_monitor'@'localhost' IDENTIFIED BY 'MonitorPassw0rd!' REQUIRE SSL; GRANT PROCESS, REPLICATION CLIENT ON *.* TO 'zbx_monitor'@'localhost'; FLUSH PRIVILEGES;
在 Zabbix Agent 主機建立 /root/.my.cnf(權限 600)實現免密:
[client] user=zbx_monitor password=MonitorPassw0rd! socket=/var/lib/mysql/mysql.sock
# /usr/local/zabbix/etc/zabbix_agentd.conf UnsafeUserParameters=1 UserParameter=mysql.stats[*],/usr/local/zabbix/bin/mysql_stats.sh "$1" # 重啟 agent systemctl restart zabbix-agent
zabbix_get -s 127.0.0.1 -k 'mysql.stats[Questions]' zabbix_get -s 127.0.0.1 -k 'mysql.stats[Innodb_buffer_pool_read_requests]'
Template DB MySQL(內建)或自建 Template。mysql.stats[Questions],Type of information = Numeric (unsigned),Units = qps,Update interval = 30s。mysql.bp_hit_rate,Type = Calculated,Formula = last(mysql.stats[Innodb_buffer_pool_read_requests])/(last(mysql.stats[Innodb_buffer_pool_read_requests])+last(mysql.stats[Innodb_buffer_pool_reads]))*100。{Template MySQL:mysql.stats[Slow_queries].last()}>0 → 嚴重性 Warning。{Template MySQL:mysql.stats[Threads_running].avg(5m)}>200 → 高併發告警。{Template MySQL:mysql.bp_hit_rate.last()}<95 → 緩衝區命中率過低。# 從庫腳本擴充
Replication_Lag)
mysql -u$MYSQL_USER -p$MYSQL_PASS -S $MYSQL_SOCKET -e "SHOW SLAVE STATUS\G" | awk -F': ' '/Seconds_Behind_Master/ {print ($2==""?-1:$2)}'
;;
Key = mysql.stats[Replication_Lag],觸發器 >30 秒告警。
| 問題 | 解決 |
|---|---|
Agent 回傳 ZBX_NOTSUPPORTED | 檢查腳本路徑、權限、shebang、依賴指令是否存在;以 zabbix 使用者手動執行腳本除錯 |
| 大量監控項導致 Server 負載高 | 啟用 StartPollers=50、StartPollersUnreachable=10;調整 Update interval 為 60s 以上;使用 Calculated items 減少實際查詢 |
| 圖表無資料 | 確認 Host 與 Template 連結、Item 狀態 Enabled、Agent 防火牆放行 10050/10051 |
結語: 透過 Zabbix + 自訂腳本,可將 MySQL 核心指標納入統一監控體系,配合 Grafana 匯入 Zabbix 資料源更能打造精美儀表板。建議將 Template 匯出為 XML 版本控管,實現基礎設施即代碼。