prometheus使用mysql_exporter监控mysql实例

2023-07-01 14:50:52 浏览数 (3)

# 安装mysql exporter

二进制部署

代码语言:javascript复制
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

数据库授权

代码语言:javascript复制
GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'mysqld_exporter'@'localhost' identified by '12345678';
GRANT SELECT ON performance_schema.* TO 'mysqld_exporter'@'localhost';
flush privileges;

# docker部署mysql exporter

代码语言:javascript复制
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

这里通过环境变量DATA_SOURCE_NAME方式定义监控目标。使用Docker Compose启动测试用的MySQL实例以及MySQLD Exporter:

代码语言:javascript复制
$ docker-compose up -d

可以通过/metrics查看mysql_up指标判断当前MySQLD Exporter是否正常连接到了MySQL实例,当指标值为1时表示能够正常获取监控数据:

代码语言:javascript复制
# HELP mysql_up Whether the MySQL server is up.
# TYPE mysql_up gauge
mysql_up 1

修改Prometheus配置文件/etc/prometheus/prometheus.yml,增加对MySQLD Exporter实例的采集任务配置:

代码语言:javascript复制
  - job_name: 'mysql'
    static_configs:
      - targets: ['xxxx:9105']
        labels:
          instance: mysql

启动Prometheus:

代码语言:javascript复制
prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/data/prometheus

配置检测

代码语言:javascript复制
/usr/local/prometheus/promtool check config /usr/local/prometheus/prometheus.yml

加载配置

代码语言:javascript复制
curl -XPOST "http://xxxxx:9090/-/reload"

# grafana 配置mysql仪表盘

导入模板:7362


参考链接 (opens new window)

0 人点赞