prometheus使用mysql_exporter监控mysql实例
# 安装mysql exporter
二进制部署
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.0/mysqld_exporter-0.12.0.linux-amd64.tar.gz
tar xvf mysqld_exporter-0.12.0.linux-amd64.tar.gz
mv mysqld_exporter-0.12.0.linux-amd64 /data/
# 创建配置文件
cat >> /data/mysql_exporter/localhost_db.cnf <<EOF
[client]
user=mysqld_exporter
password=12345678
EOF
# 创建systemd服务
cat > /etc/systemd/system/mysql_exporter.service << EOF
[Unit]
Description=mysql_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/data/mysql_exporter/mysqld_exporter --config.my-cnf="/data/mysql_exporter/localhost_db.cnf" --web.listen-address=":9105"
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
数据库授权
GRANT 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
2
3
# docker部署mysql exporter
version: '3'
services:
mysql:
image: mysql:5.7
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=database
mysqlexporter:
image: prom/mysqld-exporter
ports:
- "9104:9104"
environment:
- DATA_SOURCE_NAME=root:password@(mysql:3306)/database
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
这里通过环境变量DATA_SOURCE_NAME方式定义监控目标。使用Docker Compose启动测试用的MySQL实例以及MySQLD Exporter:
$ docker-compose up -d
1
可以通过/metrics查看mysql_up指标判断当前MySQLD Exporter是否正常连接到了MySQL实例,当指标值为1时表示能够正常获取监控数据:
# HELP mysql_up Whether the MySQL server is up.
# TYPE mysql_up gauge
mysql_up 1
1
2
3
2
3
修改Prometheus配置文件/etc/prometheus/prometheus.yml,增加对MySQLD Exporter实例的采集任务配置:
- job_name: 'mysql'
static_configs:
- targets: ['xxxx:9105']
labels:
instance: mysql
1
2
3
4
5
2
3
4
5
启动Prometheus:
prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/data/prometheus
1
2
2
配置检测
/usr/local/prometheus/promtool check config /usr/local/prometheus/prometheus.yml
1
加载配置
curl -XPOST "http://xxxxx:9090/-/reload"
1
# grafana 配置mysql仪表盘
导入模板:7362
上次更新: 2023/06/30, 22:33:06