官方文档: MySQL Server Exporter
Prometheus 中文文档: 监控MySQL运行状态
Prometheus 监控 MySQL
MySQL 是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle旗下的产品。MySQL是最流行的关系型数据库管理系统之一。数据库的稳定运行是保证业务可用性的关键因素之一。这一小节当中将介绍如何使用Prometheus提供的MySQLD Exporter实现对MySQL数据库性能以及资源利用率的监控和度量。
部署 MySQL Exporter
二进制部署 MySQL_Exporter
从 GitHub 下载 MySQL Exporter 二进制文件
1
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.13.0/mysqld_exporter-0.13.0.linux-amd64.tar.gz
解压压缩包
1
2tar xf mysqld_exporter-0.13.0.linux-amd64.tar.gz
mv mysqld_exporter-0.13.0.linux-amd64 /usr/local/mysqld_exporter创建 MySQL 监控专用账号
1
2
3GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'mysqld_exporter'@'localhost' identified by '12345678';
GRANT SELECT ON performance_schema.* TO 'mysqld_exporter'@'localhost';
flush privileges;注意,如果开启了数据库审计,还需要授权用户对审计表有权限,如下
1
2
3
4
5
6
7
8
9
10
11
12
13// 查看 init_connect 参数配置
SQL> show variables like 'init_connect';
+---------------+-------------------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------+
| init_connect | insert into auditlog.t_audit(id,thread_id,login_time,localname,matchname) values(null,connection_id(),now(),user(),current_user()); |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
// 授权
SQL> GRANT SELECT,INSERT ON `mysql`.`audit_login` TO 'mysqlexporter'@'127.0.0.1';
// 刷新权限表
SQL> flush privileges;创建 mysqld_exporter 配置文件
1
2
3
4
5cat >> /usr/local/mysqld_exporter/my.cnf <<EOF
[client]
user=mysqld_exporter
password=12345678
EOF创建 systemd 服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15cat > /usr/lib/systemd/system/exporter_mysqld.service << EOF
[Unit]
Description=mysqld_exporter
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter \
--config.my-cnf=/usr/local/mysqld_exporter/my.cnf \
--web.listen-address=0.0.0.0:9104
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF启动 mysql_exporter 服务
1
systemctl enable --noe exporter_mysqld
浏览器访问
http://database-server:9105/metrics
,查看指标获取,如下图所示
Docker 部署 MySQL Exporter
为了简化测试环境复杂度,这里使用Docker Compose定义并启动MySQL以及MySQLD Exporter:
1
2
3
4
5
6
7
8
9
10
11version: "3.5"
services:
mysql-local-dev:
container_name: mysql-local-dev
image: "prom/mysqld-exporter"
environment:
- DATA_SOURCE_NAME=mysqld_exporter:Shkzc123_1@(192.168.64.62:3306)/
ports:
- target: 9104
published: 9105
restart: always创建 MySQL 监控专用账户 mysqld_exporter
1
2
3GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'mysqld_exporter'@'%' identified by 'Shkzc123_1';
GRANT SELECT ON performance_schema.* TO 'mysqld_exporter'@'%';
flush privileges;启动 MySQL exporter 容器
1
docker-compose up -d mysql-local-dev
浏览器访问
http://docker-server:9105/metrics
查看监控指标
配置 Prometheus
当在浏览器上可以查看到 MySQL 相关的指标时,就可以配置 Prometheus,将 MySQL 添加到 Prometheus 监控指标中了。
修改 Prometheus 的主配置文件 prometheus.yml 文件,增加一个对 MySQL Exporter 实例的采集任务配置,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13# 以下配置是需要监控的 MySQL 数据库
- file_sd_configs:
- files:
- 'configs/mysql.yml'
refresh_interval: 10s
job_name: 'MySQL'
metrics_path: /metrics
metric_relabel_configs:
relabel_configs:
- source_labels: [__address__]
regex: (.*):(\d+)
target_label: instance
replacement: $1新建 configs/mysql.yml 文件,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14- labels:
env: 'dev'
region: 'dg2'
hostname: 'oracle'
server: '192.168.64.62'
targets:
- '192.168.64.61:9105'
- labels:
env: 'product'
region: 'aliyun'
hostname: 'aliyun-apps'
server: '47.106.122.190'
targets:
- '192.168.64.61:9104'运行以下命令,重新加载配置文件
1
curl -X POST http://prometheus-server:9090/-/reload/
打开浏览器,访问
http://prometheus-server:9090/targets/
,检查是否有 MySQL 的监控对象
下载 Grafana Dashboard 配置文件,导入 Grafana,这里使用 MySQL Exporter 导出器自带的 dashboard 文件,下载地址:
https://github.com/prometheus/mysqld_exporter/tree/master/mysqld-mixin/dashboards