欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL進行監(jiān)控配置的詳細指南

 更新時間:2025年06月23日 08:53:50   作者:慧一居士  
這篇文章主要和大家分享一個完整的 MySQL 監(jiān)控配置實戰(zhàn)方案,涵蓋監(jiān)控工具安裝,核心指標采集,可視化展示,告警配置等內容,需要的可以了解下

以下是一個完整的 MySQL 監(jiān)控配置實戰(zhàn)方案,涵蓋監(jiān)控工具安裝、核心指標采集、可視化展示、告警配置等內容?;?Prometheus + Grafana + mysqld_exporter 實現(xiàn)。

一、環(huá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ù)導出)

二、部署監(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,添加以下內容:

global:
  scrape_interval: 15s # 采集頻率

scrape_configs:
  - job_name: 'mysql'
    static_configs:
      - targets: ['localhost:9104'] # mysqld_exporter 默認端口

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 訪問權限:

[client]
user=monitor_user    # 用于監(jiān)控的 MySQL 用戶
password=your_password
host=127.0.0.1
port=3306

創(chuàng)建監(jiān)控用戶(替換為實際密碼):

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 指標

在 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://<服務器IP>:3000,默認賬號 admin/admin。

2.添加 Prometheus 數(shù)據(jù)源:

  • 進入 Configuration -> Data Sources -> Add data source
  • 選擇 Prometheus,填寫 URL http://localhost:9090,保存。

3.導入 MySQL 監(jiān)控儀表盤:

訪問 Grafana Dashboard 庫,搜索 MySQL。

推薦導入:

五、核心監(jiān)控指標

通過 Grafana 展示以下關鍵指標:

1.基礎狀態(tài):

  • Uptime
  • Version
  • Connections (Threads_connected)

2.性能指標:

  • 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 主從復制延遲超過 5 分鐘"

在 Prometheus 中加載規(guī)則文件,并配置 Alertmanager 通知(郵件/釘釘/微信)。

七、自動化腳本增強

編寫腳本定期檢查慢查詢、表碎片、索引效率等:

#!/bin/bash
# slow_query_report.sh

# 獲取慢查詢日志
slow_log=$(grep "^# Query" /var/log/mysql/slow.log | tail -n 10)
echo "慢查詢日志:" >> /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

設置定時任務:

crontab -e
# 每天凌晨 1 點執(zhí)行
0 1 * * * /path/to/slow_query_report.sh

八、驗證與優(yōu)化

1.模擬故障測試:

  • 停止 MySQL 服務,驗證 Prometheus 是否觸發(fā)告警。
  • 插入大量慢查詢,檢查慢查詢統(tǒng)計是否正常。

2.調整閾值:

根據(jù)業(yè)務流量調整 QPS、TPS、連接數(shù)告警閾值。

3.長期存儲:

配置 Prometheus 長期存儲(如 VictoriaMetrics 或 Thanos)。

總結

通過以上配置,可以實現(xiàn)對 MySQL 的全方位監(jiān)控,包括實時性能、資源使用、高可用性狀態(tài)等。結合告警和自動化腳本,可提前發(fā)現(xiàn)潛在問題,保障數(shù)據(jù)庫穩(wěn)定性。

到此這篇關于MySQL進行監(jiān)控配置的詳細指南的文章就介紹到這了,更多相關MySQL監(jiān)控配置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • KubeSphere部署mysql的詳細步驟

    KubeSphere部署mysql的詳細步驟

    本文介紹了在KubeSphere中部署Mysql的詳細步驟,包括創(chuàng)建mysql配置、pvc掛載、工作負載、服務,并測試數(shù)據(jù)庫連接,步驟詳盡,包括yaml配置和環(huán)境變量設置,特別強調了路徑一致性和外部訪問設置,還提到了使用NodePort模式解決自定義域名連接問題
    2024-10-10
  • MySQL數(shù)據(jù)庫高可用HA實現(xiàn)小結

    MySQL數(shù)據(jù)庫高可用HA實現(xiàn)小結

    MySQL數(shù)據(jù)庫是目前開源應用最大的關系型數(shù)據(jù)庫,有海量的應用將數(shù)據(jù)存儲在MySQL數(shù)據(jù)庫中,這篇文章主要介紹了MySQL數(shù)據(jù)庫高可用HA實現(xiàn),需要的朋友可以參考下
    2022-01-01
  • mysql grants小記

    mysql grants小記

    grant命令是對mysql數(shù)據(jù)庫進行用戶創(chuàng)建,權限或其他參數(shù)控制的強大的命令,官網(wǎng)上介紹它就有幾大頁,要用精它恐怕不是一日半早的事情,權宜根據(jù)心得慢慢領會吧!
    2011-05-05
  • 常見php與mysql中文亂碼問題解決辦法

    常見php與mysql中文亂碼問題解決辦法

    MySQL對中文的支持程度還是很有限的,尤其是新手,但凡出現(xiàn)亂碼問題,就會頭大。
    2014-09-09
  • MySQL execute、executeUpdate、executeQuery三者的區(qū)別

    MySQL execute、executeUpdate、executeQuery三者的區(qū)別

    這篇文章主要介紹了MySQL execute、executeUpdate、executeQuery三者的區(qū)別的相關資料,需要的朋友可以參考下
    2017-05-05
  • MySQL 5.7并發(fā)復制隱式bug實例分析

    MySQL 5.7并發(fā)復制隱式bug實例分析

    這篇文章主要給大家介紹了關于MySQL 5.7并發(fā)復制隱式bug的相關資料,文中介紹的非常詳細,對大家學習或者使用mysql5.7具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-11-11
  • Centos7下安裝和配置MySQL5.7.20的詳細教程

    Centos7下安裝和配置MySQL5.7.20的詳細教程

    這篇文章主要介紹了Linux(CentOS7)下安裝和配置MySQL5.7.20詳細教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價,需要的朋友可以參考下
    2020-05-05
  • linux下啟動或者關閉MySQL數(shù)據(jù)庫的多種方式

    linux下啟動或者關閉MySQL數(shù)據(jù)庫的多種方式

    ,在Linux服務器上管理MySQL服務是一個基本的運維任務,下面這篇文章主要給大家介紹了關于linux下啟動或者關閉MySQL數(shù)據(jù)庫的多種方式,文中通過代碼以及圖文介紹的非常詳細,需要的朋友可以參考下
    2024-06-06
  • Mysql注入中的outfile、dumpfile、load_file函數(shù)詳解

    Mysql注入中的outfile、dumpfile、load_file函數(shù)詳解

    這篇文章主要介紹了Mysql注入中的outfile、dumpfile、load_file,需要的朋友可以參考下
    2018-05-05
  • MySql 修改密碼后的錯誤快速解決方法

    MySql 修改密碼后的錯誤快速解決方法

    今天在MySql5.6操作時報錯:You must SET PASSWORD before executing this statement解決方法,需要的朋友可以參考下
    2016-11-11

最新評論