MySQL進(jìn)行監(jiān)控配置的詳細(xì)指南
以下是一個(gè)完整的 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 # 移動(dòng)到 /usr/local mv prometheus-* /usr/local/prometheus mv grafana-* /usr/local/grafana # 創(chuàng)建符號(hào)鏈接 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-* # 移動(dòng)到系統(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;
啟動(dòng) 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.啟動(dòng) Grafana:
grafana-server web &
訪問 http://<服務(wù)器IP>:3000,默認(rèn)賬號(hào) 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 庫(kù),搜索 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 通知(郵件/釘釘/微信)。
七、自動(dòng)化腳本增強(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è)置定時(shí)任務(wù):
crontab -e
# 每天凌晨 1 點(diǎn)執(zhí)行
0 1 * * * /path/to/slow_query_report.sh
八、驗(yàn)證與優(yōu)化
1.模擬故障測(cè)試:
- 停止 MySQL 服務(wù),驗(yàn)證 Prometheus 是否觸發(fā)告警。
- 插入大量慢查詢,檢查慢查詢統(tǒng)計(jì)是否正常。
2.調(diào)整閾值:
根據(jù)業(yè)務(wù)流量調(diào)整 QPS、TPS、連接數(shù)告警閾值。
3.長(zhǎng)期存儲(chǔ):
配置 Prometheus 長(zhǎng)期存儲(chǔ)(如 VictoriaMetrics 或 Thanos)。
總結(jié)
通過以上配置,可以實(shí)現(xiàn)對(duì) MySQL 的全方位監(jiān)控,包括實(shí)時(shí)性能、資源使用、高可用性狀態(tài)等。結(jié)合告警和自動(dòng)化腳本,可提前發(fā)現(xiàn)潛在問題,保障數(shù)據(jù)庫(kù)穩(wěn)定性。
到此這篇關(guān)于MySQL進(jìn)行監(jiān)控配置的詳細(xì)指南的文章就介紹到這了,更多相關(guān)MySQL監(jiān)控配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL數(shù)據(jù)庫(kù)高可用HA實(shí)現(xiàn)小結(jié)
MySQL數(shù)據(jù)庫(kù)是目前開源應(yīng)用最大的關(guān)系型數(shù)據(jù)庫(kù),有海量的應(yīng)用將數(shù)據(jù)存儲(chǔ)在MySQL數(shù)據(jù)庫(kù)中,這篇文章主要介紹了MySQL數(shù)據(jù)庫(kù)高可用HA實(shí)現(xiàn),需要的朋友可以參考下2022-01-01MySQL execute、executeUpdate、executeQuery三者的區(qū)別
這篇文章主要介紹了MySQL execute、executeUpdate、executeQuery三者的區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-05-05MySQL 5.7并發(fā)復(fù)制隱式bug實(shí)例分析
這篇文章主要給大家介紹了關(guān)于MySQL 5.7并發(fā)復(fù)制隱式bug的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用mysql5.7具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11Centos7下安裝和配置MySQL5.7.20的詳細(xì)教程
這篇文章主要介紹了Linux(CentOS7)下安裝和配置MySQL5.7.20詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià),需要的朋友可以參考下2020-05-05linux下啟動(dòng)或者關(guān)閉MySQL數(shù)據(jù)庫(kù)的多種方式
,在Linux服務(wù)器上管理MySQL服務(wù)是一個(gè)基本的運(yùn)維任務(wù),下面這篇文章主要給大家介紹了關(guān)于linux下啟動(dòng)或者關(guān)閉MySQL數(shù)據(jù)庫(kù)的多種方式,文中通過代碼以及圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06Mysql注入中的outfile、dumpfile、load_file函數(shù)詳解
這篇文章主要介紹了Mysql注入中的outfile、dumpfile、load_file,需要的朋友可以參考下2018-05-05