文章目錄
- **Prometheus 監控 MySQL **
- **1. 目標**
- **2. 環境準備**
- **2.1 所需組件**
- **2.2 權限要求**
- **3. 部署 mysqld_exporter**
- **3.1 下載與安裝**
- **3.2 創建配置文件**
- **3.3 創建 Systemd 服務**
- **3.4 驗證 Exporter**
- **4. 配置 Prometheus**
- **4.1 添加 Job 到 `prometheus.yml`**
- **4.2 重載 Prometheus**
- **5. 核心監控指標說明**
- **6. 告警規則配置**
- **6.1 創建告警規則文件**
- **6.2 在 `prometheus.yml` 中加載規則**
- **7. Grafana 儀表盤配置**
- **8. 維護與優化**
- **8.1 定期檢查**
- **8.2 安全加固**
- **8.3 性能調整**
- **9. 故障排查**
- **9.1 Exporter 無數據**
- **9.2 Prometheus 未抓取**
- **10. 附錄**
**Prometheus 監控 MySQL **
1. 目標
- 實時監控 MySQL 關鍵性能指標(如連接數、查詢吞吐量、復制狀態等)。
- 設置告警規則,及時發現數據庫異常。
- 通過 Grafana 可視化監控數據。
2. 環境準備
2.1 所需組件
組件 | 作用 | 安裝位置 |
---|---|---|
Prometheus Server | 指標采集與存儲 | 監控服務器 |
mysqld_exporter | 暴露 MySQL 指標 | MySQL 服務器 |
Grafana | 數據可視化 | 監控服務器 |
Alertmanager | 告警通知管理 | 監控服務器 |
2.2 權限要求
- MySQL 用戶需具備
PROCESS
,REPLICATION CLIENT
,SELECT
權限:CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'YourStrongPassword'; GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost'; FLUSH PRIVILEGES;
3. 部署 mysqld_exporter
3.1 下載與安裝
# 下載最新版 (替換版本號)
VERSION="0.15.1"
wget https://github.com/prometheus/mysqld_exporter/releases/download/v${VERSION}/mysqld_exporter-${VERSION}.linux-amd64.tar.gz
tar xvf mysqld_exporter-${VERSION}.linux-amd64.tar.gz
sudo mv mysqld_exporter-${VERSION}.linux-amd64/mysqld_exporter /usr/local/bin/
3.2 創建配置文件
創建 .my.cnf
文件存儲數據庫連接信息:
sudo tee /etc/.mysqld_exporter.cnf <<EOF
[client]
user=exporter
password=YourStrongPassword
host=localhost
port=3306
EOF
sudo chmod 600 /etc/.mysqld_exporter.cnf # 限制權限
3.3 創建 Systemd 服務
/etc/systemd/system/mysqld_exporter.service
:
[Unit]
Description=MySQL Prometheus Exporter
After=network.target[Service]
User=mysqld_exporter
ExecStart=/usr/local/bin/mysqld_exporter \--config.my-cnf=/etc/.mysqld_exporter.cnf \--collect.global_status \--collect.info_schema.innodb_metrics \--collect.slave_status \--collect.info_schema.processlistRestart=always[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl start mysqld_exporter
sudo systemctl enable mysqld_exporter
3.4 驗證 Exporter
訪問指標接口:
curl http://localhost:9104/metrics
應輸出包含 mysql_
前綴的指標。
4. 配置 Prometheus
4.1 添加 Job 到 prometheus.yml
scrape_configs:- job_name: 'mysql'static_configs:- targets: ['mysql-server-ip:9104'] # Exporter 地址relabel_configs:- source_labels: [__address__]target_label: instancereplacement: 'mysql-primary' # 實例標識
4.2 重載 Prometheus
curl -X POST http://localhost:9090/-/reload # 或重啟服務
5. 核心監控指標說明
指標名稱 | 含義 | 告警建議 |
---|---|---|
mysql_global_status_threads_connected | 當前連接數 | > 80% max_connections |
mysql_global_status_threads_running | 活躍連接數 | 持續 > 100 |
mysql_global_status_slow_queries | 慢查詢計數 | 短時間內突增 |
mysql_global_variables_max_connections | 最大連接數 | 規劃容量參考 |
mysql_slave_status_slave_io_running | 主從 IO 線程狀態 | ≠ 1 (異常) |
mysql_info_schema_innodb_row_lock_time_avg | 平均行鎖等待時間 | > 500ms |
6. 告警規則配置
6.1 創建告警規則文件
/etc/prometheus/rules/mysql_alerts.yml
:
groups:
- name: MySQL-Alertsrules:- alert: MySQLHighConnectionsexpr: mysql_global_status_threads_connected / mysql_global_variables_max_connections * 100 > 80for: 5mlabels:severity: warningannotations:summary: "MySQL 高連接數 ({{ $value }}%)"description: "實例 {{ $labels.instance }} 連接數超過 80% 限制"- alert: MySQLReplicationFailureexpr: mysql_slave_status_slave_io_running != 1 or mysql_slave_status_slave_sql_running != 1for: 1mlabels:severity: criticalannotations:summary: "MySQL 復制中斷 ({{ $labels.instance }})"
6.2 在 prometheus.yml
中加載規則
rule_files:- "/etc/prometheus/rules/mysql_alerts.yml"
7. Grafana 儀表盤配置
- 導入官方 Dashboard:
- ID
7362
(MySQL Overview)
- ID
- 配置 Prometheus 為數據源。
8. 維護與優化
8.1 定期檢查
- 驗證 Exporter 狀態:
systemctl status mysqld_exporter
- 檢查指標收集延遲:
prometheus_target_interval_length_seconds
- 審核 MySQL 用戶權限(每年至少一次)。
8.2 安全加固
- 使用 TLS 加密 Exporter 通信:
mysqld_exporter --web.config.file=/path/to/web-config.yml
web-config.yml
示例:tls_server_config:cert_file: server.crtkey_file: server.key
8.3 性能調整
- 限制采集的指標(減少負載):
--no-collect.info_schema.tables # 禁用表統計
- 調整 Prometheus 抓取間隔(默認 15s):
scrape_interval: 30s # prometheus.yml
9. 故障排查
9.1 Exporter 無數據
- 檢查 MySQL 用戶權限。
- 測試連接:
mysql --defaults-file=/etc/.mysqld_exporter.cnf -e "SHOW STATUS"
- 查看 Exporter 日志:
journalctl -u mysqld_exporter -f
9.2 Prometheus 未抓取
- 訪問
http://prom-server:9090/targets
檢查 Target 狀態。 - 驗證網絡連通性:
telnet mysql-server-ip 9104
10. 附錄
- mysqld_exporter 官方文檔:
https://github.com/prometheus/mysqld_exporter - MySQL 監控關鍵指標指南:
https://prometheus.io/docs/guides/mysqld-exporter/
按照此文檔,可實現 MySQL 的全面監控與告警,確保數據庫穩定性。部署后需通過壓力測試驗證監控有效性,并根據業務特點調整告警閾值。