prometheus+grafana搭建数据库监控系统

概述

Prometheus是一个监控数据采集和存储系统,利用监控数据采集组件从指定的MySQL数据库中采集的监控数据存储到磁盘,通过Grafana可视化监控系统把Prometheus采集到的数据展示成报表,让我们能够直观看到MySQL监控情况。

1.下载prometheus安装包

下载地址: http://cactifans.hi-www.com/prometheus

prometheus-2.1.0.linux-amd64.tar.gz

node_exporter-0.15.2.linux-amd64.tar.gz

还需要下载mysqld_exporter

下载地址:https://github.com/prometheus/mysqld_exporter/releases/download/v0.10.0/mysqld_exporter-0.10.0.linux-amd64.tar.gz

将以上压缩包上传到服务器并进行解压

解压

cd /usr/local/
tar -xvf node_exporter-0.15.2.linux-amd64.tar.gz
tar -xvf mysqld_exporter-0.10.0.linux-amd64.tar.gz
tar -xvf prometheus-2.1.0.linux-amd64.tar.gz
mv mysqld_exporter-0.10.0.linux-amd64/ mysql_exporter
mv node_exporter-0.15.2.linux-amd64/ node_exporter
mv prometheus-2.1.0.linux-amd64 prometheus

编辑prometheus.yml,将scrape_configs下面这段自定义配置替换

cd prometheus/
vi prometheus.yml

prometheus.yml

scrape_configs:
  - file_sd_configs:
    - files:
      - "host.yml"
    job_name: Host
    metrics_path: /metrics
    relabel_configs:
      - source_labels: [__address__]
        regex: (.*)
        target_label: instance
        replacement: $1
      - source_labels: [__address__]
        regex: (.*)
        target_label: __address__
        replacement: $1:9100
  - file_sd_configs:
    - files:
      - "mysql.yml"
    job_name: MySQL
    metrics_path: /metrics
    relabel_configs:
      - source_labels: [__address__]
        regex: (.*)
        target_label: instance
        replacement: $1
      - source_labels: [__address__]
        regex: (.*)
        target_label: __address__
        replacement: $1:9104
  # 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: [‘localhost:9090‘]

启动命令

./prometheus

启动日志

level=info ts=2021-05-25T10:31:01.384908059Z caller=main.go:225 msg="Starting Prometheus" version="(version=2.1.0, branch=HEAD, revision=85f23d82a045d103ea7f3c89a91fba4a93e6367a)"
level=info ts=2021-05-25T10:31:01.384962463Z caller=main.go:226 build_context="(go=go1.9.2, user=root@6e784304d3ff, date=20180119-12:01:23)"
level=info ts=2021-05-25T10:31:01.384975957Z caller=main.go:227 host_details="(Linux 3.10.0-1062.1.1.el7.x86_64 #1 SMP Fri Sep 13 22:55:44 UTC 2019 x86_64 ecs-c6s-large-2-linux-20200324191054 (none))"
level=info ts=2021-05-25T10:31:01.384987853Z caller=main.go:228 fd_limits="(soft=65536, hard=65536)"
level=info ts=2021-05-25T10:31:01.387939968Z caller=main.go:499 msg="Starting TSDB ..."
level=info ts=2021-05-25T10:31:01.38988367Z caller=web.go:383 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2021-05-25T10:31:01.414742465Z caller=main.go:509 msg="TSDB started"
level=info ts=2021-05-25T10:31:01.414821398Z caller=main.go:585 msg="Loading configuration file" filename=prometheus.yml
level=info ts=2021-05-25T10:31:01.41584456Z caller=main.go:486 msg="Server is ready to receive web requests."
level=info ts=2021-05-25T10:31:01.433708022Z caller=manager.go:59 component="scrape manager" msg="Starting scrape manager..."
2.下载grafana安装包

下载地址: https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-4.6.3.linux-x64.tar.gz

将下载好的压缩包上传到服务器

解压

cd /usr/local
tar -xvf grafana-4.6.3.linux-x64.tar.gz 
mv grafana-4.6.3 grafana

启动

./bin/grafana-server &

下载grafana-dashboards组件

下载地址: https://github.com/percona/grafana-dashboards/archive/v1.6.1.tar.gz

将压缩包上传到服务器

解压

tar -xvf grafana-dashboards-1.6.1.tar.gz
cp -r grafana-dashboards/dashboards/ /usr/local/grafana
cd /usr/local/grafana
vi gragana.ini

[dashboards.json]
enabled = true
path = /usr/local/grafana/dashboards

重启grafana

3.添加MySQL机器监控

启动node_exporter

cd /usr/local/node_exporter

nohup ./node_exporter &

我们在MySQL所在的机器上启动了一个node_exporter,他就会自动采集这台机器的CPU、磁盘、内存和网络的监控数据,但是,还有Prometheus上还没加入对这台机器的监控。

之前在Prometheus的yml配置文件中,定义了一个host监控项,就是用来监控机器的,配置文件是host.yml,在这个host.yml配置文件中,加入mysql所在的机器地址就可以了

vi host.yml

- labels:
  service: test
targets:
  - 127.0.0.1
4.添加MySQL数据库的监控

接着需要在MySQL所在的机器上启动mysqld_exporter组件,他负责采集MySQL数据库的一些监控数据。

但是启动之前,还需要配置一些环境变量,设置mysqld_exporter要监控的数据库的地址信息

export DATA_SOURCE_NAME=‘root:123456@(127.0.0.1:3306)/‘
echo "export DATA_SOURCE_NAME=‘root:123456@(127.0.0.1:3306)/‘" >> /etc/profile

启动mysqld_exporter

nohup ./mysqld_exporter --collect.info_schema.processlist --collect.info_schema.innodb_tablespaces --collect.info_schema.innodb_metrics --collect.perf_schema.tableiowaits --collect.perf_schema.indexiowaits --collect.perf_schema.tablelocks --collect.engine_innodb_status --collect.perf_schema.file_events --collect.info_schema.processlist --collect.binlog_size --collect.info_schema.clientstats --collect.perf_schema.eventswaits

接着mysqld_exporter进程就会自动采集MySQL的监控数据了,然后我们还需要再Prometheus配置一些他跟mysqld_exporter通信获取数据以及存储,然后Grafana才能看到对应的监控数据。

vi /usr/local/prometheus/mysql.yml

- labels:
  service: mysql_test
targets:
 - 127.0.0.1

接着我们在Grafana中就能看到MySQL的各种监控数据了。

prometheus+grafana搭建数据库监控系统

prometheus+grafana搭建数据库监控系统

上一篇:.NET Core EF(Entity Framework) Core 自动创建数据库


下一篇:Oracle转MySQL日记