Prometheus+Grafana可视化监控MySQL

使用Prometheus和Grafana对MySQL服务器性能进行监控。
使用两个exporter:
node_exporter: 服务器系统数据收集
mysqld_exporter: MySQL服务器数据收集
监控架构图:
Prometheus+Grafana可视化监控MySQL
  • Prometheus安装配置
    安装方式二进制安装,详见这里 CentOS7部署Prometheus
    版本:目前最新版本prometheus-2.18.1
    修改Prometheus配置文件prometheus.yml,主要配置scrape configuration项。
    scrape_configs:
    # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
    - job_name: ‘prometheus‘
    # metrics_path defaults to ‘/metrics‘
    # scheme defaults to ‘http‘.
    static_configs:
    - targets: [‘10.20.1.63:9090‘]
    - job_name: ‘local_node‘
    static_configs:
      - targets: [‘10.20.1.63:9100‘]
        labels:
          instance: prometheus_node
    - job_name: ‘mysql01‘
    static_configs:
      - targets: [‘10.20.10.18:9104‘]
        labels:
          instance: db1
    - job_name: ‘mysql02‘
    static_configs:
      - targets: [‘10.20.10.19:9104‘]
        labels:
          instance: db2
  • 安装exporter
    下载安装node_exporter、mysqld_exporter。从官网下载https://prometheus.io/download/
    版本分别为node_exporter-0.18.1、mysqld_exporter-0.12.1。
    1) 安装node_exporter
    wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
    mkdir -p /opt/prometheus
    tar xzvf node_exporter-0.18.1.linux-amd64.tar.gz 
    mv node_exporter-0.18.1.linux-amd64 /opt/prometheus/
    ln -s /opt/prometheus/node_exporter-0.18.1.linux-amd64/ /opt/prometheus/node_exporter

    添加系统服务启动.
    先创建prometheus用户,用于启动node_exporter。

    useradd  -s /sbin/nologin -M prometheus

    vim /usr/lib/systemd/system/node_exporter.service

    [Unit]
    Description=node_exporter
    Documentation=https://prometheus.io/
    After=network.target
    [Service]
    Type=simple
    User=prometheus
    ExecStart=/opt/prometheus/node_exporter/node_exporter 
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target

    启动node_exporter服务

    systemctl status node_exporter.service

    2) 安装mysqld_exporter

    wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
    mkdir -p /opt/prometheus
    mv /usr/local/mysqld_exporter-0.12.1.linux-amd64/ /opt/prometheus/
    ln -s /opt/prometheus/mysqld_exporter-0.12.1.linux-amd64/ /opt/prometheus/mysqld_exporter
    chown -R prometheus:prometheus /opt/prometheus/

    添加系统服务启动.
    先创建prometheus用户,用于启动mysqld_exporter。

    useradd  -s /sbin/nologin -M prometheus

    vim /usr/lib/systemd/system/mysqld_exporter.service

    [Unit]
    Description=node_exporter
    Documentation=https://prometheus.io/
    After=network.target
    [Service]
    Type=simple
    User=prometheus
    ExecStart=/opt/prometheus/mysqld_exporter/mysqld_exporter --config.my-cnf=/opt/prometheus/mysqld_exporter/.my.cnf
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target

    启动参数:--config.my-cnf=".my.cnf" #指定配置文件
    启动mysqld_exporter服务

    systemctl status mysqld_exporter.service

    mysqld_exporter需要连接到Mysql,所以需Mysql的权限,创建mysql_exporter用户并赋予所需的权限。

    mysql> GRANT REPLICATION CLIENT,PROCESS ON *.* TO ‘mysql_exporter‘@‘localhost‘ identified by ‘xxxxxx‘;
    mysql> GRANT SELECT ON *.* TO ‘mysql_exporter‘@‘localhost‘;

    创建.my.cnf文件,并重启mysqld_exporter服务。
    vim /opt/prometheus/mysqld_exporter/.my.cnf

    [client]
    user=mysql_exporter
    password=xxxxxx
  • Prometheus监控
    回到Prometheus,Web查看Status->Targets页面,可以看到Target的State状态都为UP,说明exporter都已收集到监控数据了。
    Prometheus+Grafana可视化监控MySQL
    Prometheus监控使用promSQL查看mysql监控信息,如连接数信息mysql_global_status_connections。
    Prometheus+Grafana可视化监控MySQL
    但Prometheus自带的图形并不够强大,所以结合Grafana作为Prometheus的Dashboard。
  • 配置Grafana
    通过grafana的默认3000端口,访问Grafana网页,添加数据源
    Prometheus+Grafana可视化监控MySQL
    这里选择Prometheus数据源,其中URL填写Prometheus地址,其他项保持默认。单击 Save & Test 添加成功后如下。
    Prometheus+Grafana可视化监控MySQL
    Granfana 导入Mysql 监控图表。推荐 https://grafana.com/grafana/dashboards/7362 监控模板,导出Download JSON。
    Granfana进入Create->Import导入Dashboards。
    Prometheus+Grafana可视化监控MySQL
    监控项非常详细,最终呈现如下:
    Prometheus+Grafana可视化监控MySQL