使用 prometheus 监控 MySQL

1. 下载 

https://prometheus.io/download/
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gz

2.安装 mysqld_exporter

tar -xvf mysqld_exporter-0.14.0.linux-amd64.tar.gz -C /usr/local
cd /usr/local
mv mysqld_exporter-0.14.0.linux-amd64 mysqld_exporter

3. 创建mysql 监控账号

CREATE USER 'prometheus'@'%' IDENTIFIED BY 'root' WITH MAX_USER_CONNECTIONS 10;
GRANT PROCESS, REPLICATION CLIENT, REPLICATION SLAVE, SELECT ON *.* TO 'prometheus';

4.创建 mysqld_exporter 用的mysql登录文件

vi /usr/local/mysqld_exporter/my.cnf
[client]
host=127.0.0.1
port=3306
user=prometheus
password=root

5.启动 mysqld_exporter 进程

  

./mysqld_exporter --help

nohup ./mysqld_exporter \
--collect.info_schema.processlist.processes_by_user \
--collect.info_schema.processlist.processes_by_host \
--collect.mysql.user.privileges \
--collect.info_schema.processlist \
--collect.mysql.user \
--collect.info_schema.tables \
--collect.info_schema.innodb_tablespaces \
--collect.info_schema.innodb_metrics \
--collect.global_status \
--collect.global_variables \
--collect.slave_status \
--collect.perf_schema.indexiowaits \
--collect.perf_schema.tablelocks \
--collect.perf_schema.eventsstatements \
--collect.perf_schema.eventsstatementssum \
--collect.perf_schema.eventswaits \
--collect.auto_increment.columns \
--collect.binlog_size \
--collect.perf_schema.tableiowaits \
--collect.perf_schema.replication_group_members \
--collect.perf_schema.replication_group_member_stats \
--collect.perf_schema.replication_applier_status_by_worker \
--collect.info_schema.userstats \
--collect.info_schema.clientstats \
--collect.perf_schema.file_events \
--collect.perf_schema.file_instances \
--collect.perf_schema.memory_events \
--collect.info_schema.innodb_cmpmem \
--collect.info_schema.query_response_time \
--collect.engine_innodb_status \
--collect.info_schema.tablestats \
--collect.info_schema.schemastats \
--collect.info_schema.innodb_cmp \
--collect.slave_hosts \
--collect.info_schema.replica_host \
--config.my-cnf=/usr/local/mysqld_exporter/my.cnf &
#注意 \ 前后不能多空格

6.检查是否有监控数据

http://119.8.238.94:9104/metrics

 7. prometheus 中添加mysql监控

1)创建MySQL监控动态配置文件

创建 prd_mysql.json vi /usr/local/prometheus/sd_config/prd_mysql.json ,添加如下内容
[
{
    "targets": [ "119.8.238.94:9104" ],
    "labels": {
      "env": "订单库",
      "job": "mysqld"
    }
  }
]

2)修改 prometheus 配置文件

修改 prometheus 配置文件 prometheus.yml scrape_configs: 部分添加如下内容
- job_name: "prd_mysql"
    file_sd_configs:
      - files: ['/usr/local/prometheus/sd_config/prd_mysql.json']
        refresh_interval: 120s

3)检查 prometheus 配置文件是否能正常读取

./promtool check config prometheus.yml
Checking prometheus.yml
 SUCCESS: prometheus.yml is valid prometheus config file syntax

4)动态调整 prometheus 配置

##启动时加上 --web.enable-lifecycle 参数,后期可以通过如下方式动态调整prometheus配置,动态调整方式如下:
curl -v --request POST 'http://localhost:9090/-/reload'
curl -X POST http://localhost:9090/-/reload
#如果启动时没有使用--web.enable-lifecycle,可以使用  kill -HUP pid 方案热加载

8.查看 prometheus 中是否成功添加了该 instance

9. grafana 中查看刚刚添加的mysql  

1)在 grafana 中添加相应prometheus 数据源(如果该数据源也添加过忽略该步)

grafana 添加 prometheus 数据源方法请见

(7条消息) 通过 Grafana 对prometheus 监控做可视化_渔夫数据库笔记的博客-CSDN博客

2)给该数据源添加 dashboard(如果已添加则忽略该步)

采集配置好,正常采集有了数据之后,还需要为 Grafana 添加监控面板进行展示,如果只是看 MySQL 或 MariaDB 的一些概览情况,可以导入grafana.com
的这个面板:https://grafana.com/grafana/dashboards/7362
如果需要更丰富的面板,可以导入 percona 开源的一些面板,地址: https://github.com/percona/grafana-dashboards/tree/master/dashboards (导入 MySQL_
开头的 json 文件中的内容即可)。

猜你喜欢

转载自blog.csdn.net/shaochenshuo/article/details/126494537