MySQL進(jìn)行監(jiān)控配置的詳細(xì)指南
以下是一個完整的 MySQL 監(jiān)控配置實(shí)戰(zhàn)方案,涵蓋監(jiān)控工具安裝、核心指標(biāo)采集、可視化展示、告警配置等內(nèi)容?;?Prometheus + Grafana + mysqld_exporter 實(shí)現(xiàn)。
一、環(huán)境準(zhǔn)備
1.操作系統(tǒng):CentOS 7+ / Ubuntu 20+
2.MySQL 版本:5.7+ 或 8.0+
3.監(jiān)控工具棧:
- Prometheus(數(shù)據(jù)采集與告警)
- Grafana(可視化)
- mysqld_exporter(MySQL 數(shù)據(jù)導(dǎo)出)
二、部署監(jiān)控組件
1. 安裝 Prometheus & Grafana
# 下載并解壓 wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz wget https://dl.grafana.com/oss/release/grafana-10.0.0.linux-amd64.tar.gz # 解壓 tar -zxvf prometheus-*.tar.gz tar -zxvf grafana-*.tar.gz # 移動到 /usr/local mv prometheus-* /usr/local/prometheus mv grafana-* /usr/local/grafana # 創(chuàng)建符號鏈接 ln -s /usr/local/prometheus/bin/prometheus /usr/local/bin/prometheus ln -s /usr/local/grafana/bin/grafana-server /usr/local/bin/grafana-server
2. 配置 Prometheus
編輯 /usr/local/prometheus/prometheus.yml,添加以下內(nèi)容:
global:
scrape_interval: 15s # 采集頻率
scrape_configs:
- job_name: 'mysql'
static_configs:
- targets: ['localhost:9104'] # mysqld_exporter 默認(rèn)端口
3. 安裝 mysqld_exporter
# 下載并解壓 wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gz tar -zxvf mysqld_exporter-*.tar.gz cd mysqld_exporter-* # 移動到系統(tǒng)路徑 mv mysqld_exporter /usr/local/bin/ # 創(chuàng)建配置文件目錄 mkdir -p /etc/mysqld_exporter
4. 配置 mysqld_exporter
創(chuàng)建 /etc/mysqld_exporter/.my.cnf,配置 MySQL 訪問權(quán)限:
[client] user=monitor_user # 用于監(jiān)控的 MySQL 用戶 password=your_password host=127.0.0.1 port=3306
創(chuàng)建監(jiān)控用戶(替換為實(shí)際密碼):
CREATE USER 'monitor_user'@'localhost' IDENTIFIED BY 'your_password'; GRANT PROCESS, REPLICATION CLIENT, SHOW DATABASES ON *.* TO 'monitor_user'@'localhost'; FLUSH PRIVILEGES;
啟動 mysqld_exporter:
mysqld_exporter --config.my-cnf=/etc/mysqld_exporter/.my.cnf &
三、配置 Prometheus 抓取 MySQL 指標(biāo)
在 prometheus.yml 中添加:
scrape_configs:
- job_name: 'mysql'
metrics_path: /metrics
static_configs:
- targets: ['localhost:9104']
重啟 Prometheus:
systemctl restart prometheus
四、部署 Grafana 并配置儀表盤
1.啟動 Grafana:
grafana-server web &
訪問 http://<服務(wù)器IP>:3000,默認(rèn)賬號 admin/admin。
2.添加 Prometheus 數(shù)據(jù)源:
- 進(jìn)入 Configuration -> Data Sources -> Add data source
- 選擇 Prometheus,填寫 URL http://localhost:9090,保存。
3.導(dǎo)入 MySQL 監(jiān)控儀表盤:
訪問 Grafana Dashboard 庫,搜索 MySQL。
推薦導(dǎo)入:
五、核心監(jiān)控指標(biāo)
通過 Grafana 展示以下關(guān)鍵指標(biāo):
1.基礎(chǔ)狀態(tài):
- Uptime
- Version
- Connections (Threads_connected)
2.性能指標(biāo):
- QPS (Queries per second)
- TPS (Transactions per second)
- InnoDB Buffer Pool Usage
- Slow Queries
3.資源使用:
- CPU Usage (User/System Time)
- Memory Usage (Key buffer, InnoDB buffer pool)
- Disk I/O (Read/Write throughput)
4.高可用性:
- Replication Status (Seconds_Behind_Master)
- MGR Member State
六、告警配置
在 Prometheus 中配置告警規(guī)則(/etc/prometheus/alert.rules):
groups:
- name: mysql_alerts
rules:
- alert: HighConnections
expr: mysql_global_status_threads_connected > 100
for: 5m
labels:
severity: warning
annotations:
summary: "MySQL 活躍連接數(shù)過高"
- alert: ReplicationDelay
expr: mysql_slave_seconds_behind_master > 300
for: 5m
labels:
severity: critical
annotations:
summary: "MySQL 主從復(fù)制延遲超過 5 分鐘"在 Prometheus 中加載規(guī)則文件,并配置 Alertmanager 通知(郵件/釘釘/微信)。
七、自動化腳本增強(qiáng)
編寫腳本定期檢查慢查詢、表碎片、索引效率等:
#!/bin/bash # slow_query_report.sh # 獲取慢查詢?nèi)罩? slow_log=$(grep "^# Query" /var/log/mysql/slow.log | tail -n 10) echo "慢查詢?nèi)罩荆? >> /tmp/mysql_report.txt echo "$slow_log" >> /tmp/mysql_report.txt # 檢查表碎片 table_fragmentation=$(mysql -e "SELECT table_schema, table_name, round((data_length - index_length) / data_length * 100, 2) as fragmentation FROM information_schema.tables WHERE fragmentation > 10;") echo "表碎片情況:" >> /tmp/mysql_report.txt echo "$table_fragmentation" >> /tmp/mysql_report.txt # 發(fā)送郵件 mail -s "MySQL Daily Report" admin@example.com < /tmp/mysql_report.txt
設(shè)置定時任務(wù):
crontab -e
# 每天凌晨 1 點(diǎn)執(zhí)行
0 1 * * * /path/to/slow_query_report.sh
八、驗(yàn)證與優(yōu)化
1.模擬故障測試:
- 停止 MySQL 服務(wù),驗(yàn)證 Prometheus 是否觸發(fā)告警。
- 插入大量慢查詢,檢查慢查詢統(tǒng)計(jì)是否正常。
2.調(diào)整閾值:
根據(jù)業(yè)務(wù)流量調(diào)整 QPS、TPS、連接數(shù)告警閾值。
3.長期存儲:
配置 Prometheus 長期存儲(如 VictoriaMetrics 或 Thanos)。
總結(jié)
通過以上配置,可以實(shí)現(xiàn)對 MySQL 的全方位監(jiān)控,包括實(shí)時性能、資源使用、高可用性狀態(tài)等。結(jié)合告警和自動化腳本,可提前發(fā)現(xiàn)潛在問題,保障數(shù)據(jù)庫穩(wěn)定性。
到此這篇關(guān)于MySQL進(jìn)行監(jiān)控配置的詳細(xì)指南的文章就介紹到這了,更多相關(guān)MySQL監(jiān)控配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談MyISAM 和 InnoDB 的區(qū)別與優(yōu)化
InnoDB和MyISAM是在使用MySQL最常用的兩個表類型,各有優(yōu)缺點(diǎn),視具體應(yīng)用而定。下面我們就來具體探討下吧2015-07-07
mysql could not be resolved: Name or service not known
今天查看mysql日志的時候發(fā)現(xiàn)[Warning] IP address '10.0.0.220' could not be resolved: Name or service not known,原來是mysql DNS反解:skip-name-resolve的原因,屏蔽一下就可以了2015-08-08
MySQL?中如何歸檔數(shù)據(jù)的實(shí)現(xiàn)方法
本文主要介紹了MySQL?中如何歸檔數(shù)據(jù)的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
用SELECT... INTO OUTFILE語句導(dǎo)出MySQL數(shù)據(jù)的教程
這篇文章主要介紹了用SELECT... INTO OUTFILE語句導(dǎo)出MySQL數(shù)據(jù)的教程,是MySQL入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-05-05
Mysql數(shù)據(jù)庫綠色版安裝教程 解決系統(tǒng)錯誤1067的方法
這篇文章主要為大家詳細(xì)介紹了MySql數(shù)據(jù)庫綠色版安裝教程,以及系統(tǒng)錯誤1067的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
如何恢復(fù)Mysql數(shù)據(jù)庫的詳細(xì)介紹
這里說的MySql恢復(fù)數(shù)據(jù)庫,是指沒有通過正常備份的情況下,通過Mysql保存的數(shù)據(jù)文件如何恢復(fù)數(shù)據(jù)庫2013-09-09

