使用 prometheus 監控 mysql

mysqld-exporter 介紹

mysqld_exporter 是一個開源的 MySQL 數據庫指標收集器,它可以將 MySQL 數據庫的各種指標數據輸出爲 Prometheus 所需的格式,使得您可以使用 Prometheus 來監控和分析 MySQL 數據庫的運行情況。

mysqld_exporter 可以收集 MySQL 數據庫的諸如查詢數量、響應時間、連接數、緩存命中率、鎖等指標數據,並將這些數據輸出爲 Prometheus 所需的格式。它可以通過多種方式連接到 MySQL 數據庫,並支持多種配置選項,以便您根據自己的需要進行定製化配置。

通過使用 mysqld_exporter,您可以實時監控 MySQL 數據庫的運行狀態,及時發現並解決潛在的問題。mysqld_exporter 還提供了一些有用的指標數據,例如慢查詢、鎖爭用、緩存命中率等,這些數據可以幫助您更好地瞭解 MySQL 數據庫的性能和健康狀況。

總的來說,mysqld_exporter 是一個非常有用的工具,可以幫助您更好地監控和管理 MySQL 數據庫。

mysqld_exporter 常見問題和解決方法

  1. 連接 MySQL 數據庫失敗

這可能是由於 MySQL 數據庫的連接配置不正確或者 MySQL 數據庫沒有授權 mysqld_exporter 連接所需的權限。解決方法是檢查連接配置是否正確,確保 MySQL 數據庫授權了 mysqld_exporter 連接所需的權限。

  1. 指標數據不準確或缺失

這可能是由於 mysqld_exporter 配置不正確或者 MySQL 數據庫出現了問題。解決方法是檢查 mysqld_exporter 配置是否正確,確保 MySQL 數據庫正常運行且沒有出現問題。

  1. 內存佔用過高

這可能是由於 mysqld_exporter 默認配置下會緩存 MySQL 數據庫的所有指標數據,導致內存佔用過高。解決方法是調整 mysqld_exporter 的配置,減少緩存數據的數量或緩存時間。

  1. 安全性問題

默認情況下,mysqld_exporter 不進行身份驗證或加密,可能會存在安全風險。解決方法是配置 mysqld_exporter 進行身份驗證和加密,以確保數據安全性。

  1. 版本兼容性問題

mysqld_exporter 和 MySQL 數據庫的版本兼容性可能存在問題,導致無法正確收集指標數據。解決方法是查看 mysqld_exporter 和 MySQL 數據庫的版本兼容性,並確保使用兼容的版本。

總的來說,要確保 mysqld_exporter 正常運行並收集準確的指標數據,需要仔細配置和調試,並及時解決出現的問題。

mysqld-exporter 常用告警規則

MySQL instance is down on {{$labels.instance}}

  - alert: MysqlDown
    expr: mysql_up == 0
    for: 0m
    labels:
      severity: critical
    annotations:
      summary: MySQL down (instance {{ $labels.instance }})
      description: "MySQL instance is down on {{ $labels.instance }}\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"

More than 80% of MySQL connections are in use on {{$labels.instance}}

  - alert: MysqlTooManyConnections(>80%)
    expr: max_over_time(mysql_global_status_threads_connected[1m]) / mysql_global_variables_max_connections * 100 > 80
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: MySQL too many connections (> 80%) (instance {{ $labels.instance }})
      description: "More than 80% of MySQL connections are in use on {{ $labels.instance }}\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"

More than 60% of MySQL connections are in running state on {{$labels.instance}}

  - alert: MysqlHighThreadsRunning
    expr: max_over_time(mysql_global_status_threads_running[1m]) / mysql_global_variables_max_connections * 100 > 60
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: MySQL high threads running (instance {{ $labels.instance }})
      description: "More than 60% of MySQL connections are in running state on {{ $labels.instance }}\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"

MySQL Slave IO thread not running on {{$labels.instance}}

  - alert: MysqlSlaveIoThreadNotRunning
    expr: ( mysql_slave_status_slave_io_running and ON (instance) mysql_slave_status_master_server_id > 0 ) == 0
    for: 0m
    labels:
      severity: critical
    annotations:
      summary: MySQL Slave IO thread not running (instance {{ $labels.instance }})
      description: "MySQL Slave IO thread not running on {{ $labels.instance }}\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"

MySQL Slave SQL thread not running on {{$labels.instance}}

  - alert: MysqlSlaveSqlThreadNotRunning
    expr: ( mysql_slave_status_slave_sql_running and ON (instance) mysql_slave_status_master_server_id > 0) == 0
    for: 0m
    labels:
      severity: critical
    annotations:
      summary: MySQL Slave SQL thread not running (instance {{ $labels.instance }})
      description: "MySQL Slave SQL thread not running on {{ $labels.instance }}\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"

MySQL replication lag on {{$labels.instance}}

  - alert: MysqlSlaveReplicationLag
    expr: ( (mysql_slave_status_seconds_behind_master - mysql_slave_status_sql_delay) and ON (instance) mysql_slave_status_master_server_id > 0 ) > 30
    for: 1m
    labels:
      severity: critical
    annotations:
      summary: MySQL Slave replication lag (instance {{ $labels.instance }})
      description: "MySQL replication lag on {{ $labels.instance }}\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"

MySQL server mysql has some new slow query

  - alert: MysqlSlowQueries
    expr: increase(mysql_global_status_slow_queries[1m]) > 0
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: MySQL slow queries (instance {{ $labels.instance }})
      description: "MySQL server mysql has some new slow query.\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"

MySQL innodb log writes stalling

  - alert: MysqlInnodbLogWaits
    expr: rate(mysql_global_status_innodb_log_waits[15m]) > 10
    for: 0m
    labels:
      severity: warning
    annotations:
      summary: MySQL InnoDB log waits (instance {{ $labels.instance }})
      description: "MySQL innodb log writes stalling\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"

MySQL has just been restarted, less than one minute ago on {{$labels.instance}}.

  - alert: MysqlRestarted
    expr: mysql_global_status_uptime < 60
    for: 0m
    labels:
      severity: info
    annotations:
      summary: MySQL restarted (instance {{ $labels.instance }})
      description: "MySQL has just been restarted, less than one minute ago

來源:https://www.toutiao.com/article/7243071976682668578/?log_from=fc40922d0781c_1686702522723

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/vIZ_cBc_Pm3iR7vWFFq1SQ