prometheus监控mysql
安装mysql_exporter
下载
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
解压
tar zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz
mv mysqld_exporter-0.12.1.linux-amd64 /usr/local/mysql_exporter
cd /usr/local/mysql_exporter
在mysql创建exporter用户并赋权
MariaDB [(none)]> CREATE USER 'exporter'@'localhost' IDENTIFIED BY '000000';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
Query OK, 0 rows affected (0.001 sec)
在mysql_exporter创建mysql配置文件,运行时读配置文件登录mysql
vim /usr/local/mysql_exporter/.my.cnf
[client]
user=exporter
password=000000
启动mysql_exporter
./mysqld_exporter --config.my-cnf=.my.cnf
添加系统服务
vim /usr/lib/systemd/system/mysql_exporter.service
[Unit]
Description=https://prometheus.io
[Service]
Restart=on-failure
ExecStart=/usr/local/mysql_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysql_exporter/.my.cnf
[Install]
WantedBy=multi-user.target
启动服务
systemctl daemon-reload
systemctl start mysql_exporter.service
systemctl enable mysql_exporter
测试访问http://192.168.70.133:9104/metrics
在prometheus添加mysql监控节点
cd /usr/lib/prometheus
vim prometheus.yml
- job_name: 'mysql'
static_configs:
- targets: ['localhost:9104']
labels:
service: mysql-service
systemctl restart prometheus
查看prometheus的target查看mysql节点状态http://192.168.70.133:9090/targets
Granfana 导入Mysql 监控图表
我使用7362模板
各项监控指标
指标 | 名称 | 说明 |
---|---|---|
MySQL Uptime | 运行时长 | MySQL 服务器自从上次重启运行到现在的时长。 |
Current QPS | 每秒查询速率 | 根据使用 MySQL 的SHOW STATUS 命令查询到的结果,它是服务器在最后一秒内执行的语句数量。这个变量包含在存储程序中执行的语句,与 Questions 变量不同。 |
InnoDB Buffer Pool Size | InnoDB 缓冲池 | InnoDB 维护一个称为缓冲池的存储区域,用于在内存中缓存数据和索引。了解 InnoDB 缓冲池如何工作,并利用它来将频繁访问的数据保存在内存中,这是 MySQL 调优最重要的方面之一。目标是将工作集保存在内存中。在大多数情况下,这个值应该处于主机上60%-90%的可用内存之间。 |
MySQL Connections | MySQL 连接数 | 是自服务器启动以来同时使用的最大连接数。 |
MySQL Client Thread Activity | 客户端活动线程数 | 未休眠线程数 |
MySQL Questions | 服务器执行的语句数 | 与 QPS 计算中使用的查询不同,只包括客户端发送到服务器的语句,而不包括存储程序中执行的语句。 |
MySQL Thread Cache | 线程缓存 | 当客户端断开连接时,如果缓存未满,客户端的线程将被放入缓存中。 |
MySQL Sorts | 排序使用情况 | 显示当前排序功能的使用情况。 |
MySQL Slow Queries | 慢查询使用情况 | 显示当前慢查询功能的使用情况。 |
MySQL Aborted Connections | 终止的连接数 | 当一个给定的主机连接到 MySQL 并且连接在中间被中断(例如由于凭证错误)时,MySQL 会将该信息保存在系统表中。 |
MySQL Table Locks | 表级锁使用情况 | MySQL 因各种原因需要多个不同的锁。在这个图表中,我们看到 MySQL 从存储引擎请求了多少个表级锁。 |
MySQL Network Traffic | 网络流量 | 在这里我们可以看到 MySQL 产生了多少网络流量。出站是从 MySQL 发送的网络流量,入站是 MySQL 收到的网络流量。 |
MySQL Network Usage Hourly | 每小时网络流量 | 这里我们可以看到每小时 MySQL 产生多少网络流量。您可以使用条形图来比较 MySQL 发送的数据和 MySQL 收到的数据。 |
MySQL Network Usage Hourly | 内存概述 | 数据库使用的内存情况。 |
MySQL Table Open Cache Status | 表状态 | 正在打开的表的缓存大小 |
MySQL Open Tables | 打开的表的数量 | 打开的表的数量 |
MySQL Table Definition Cache | 表定义缓存 | 表定义缓存。 |
alermanager报警规则示例
监控 | 说明 | 阈值(根据实际情况自己设定) |
---|---|---|
Mysql_High_QPS | 获取5分钟MySQL实例查询速率 | 500 |
Mysql_Too_Many_Connections | 查看最大连接数 | 200 |
Mysql_Too_Many_slow_queries | 查看最大慢查询数 | 3 |
SQL thread stopped | 获取主从状态 | 0 |
Slave lagging behind Master | 主从数据差异 | 30 |
groups:
- name: MySQLStatsAlert
rules:
- alert: Mysql_High_QPS
expr: rate(mysql_global_status_questions[5m]) > 500
for: 2m
labels:
severity: warning
annotations:
summary: "{{$labels.instance}}: Mysql_High_QPS detected"
description: "{{$labels.instance}}: Mysql opreation is more than 500 per second ,(current value is: {{ $value }})"
- alert: Mysql_Too_Many_Connections
expr: rate(mysql_global_status_threads_connected[5m]) > 200
for: 2m
labels:
severity: warning
annotations:
summary: "{{$labels.instance}}: Mysql Too Many Connections detected"
description: "{{$labels.instance}}: Mysql Connections is more than 100 per second ,(current value is: {{ $value }})"
- alert: Mysql_Too_Many_slow_queries
expr: rate(mysql_global_status_slow_queries[5m]) > 3
for: 2m
labels:
severity: warning
annotations:
summary: "{{$labels.instance}}: Mysql_Too_Many_slow_queries detected"
description: "{{$labels.instance}}: Mysql slow_queries is more than 3 per second ,(current value is: {{ $value }})"
- alert: SQL thread stopped
expr: mysql_slave_status_slave_sql_running == 0
for: 1m
labels:
severity: critical
annotations:
summary: "Instance {{ $labels.instance }} SQL thread stopped"
description: "SQL thread has stopped. This is usually because it cannot apply a SQL statement received from the master."
- alert: Slave lagging behind Master
expr: rate(mysql_slave_status_seconds_behind_master[5m]) >30
for: 1m
labels:
severity: warning
annotations:
summary: "Instance {{ $labels.instance }} Slave lagging behind Master"
description: "Slave is lagging behind Master. Please check if Slave threads are running and if there are some performance issues!"