1.下载mysqld_exporter

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

2.解压

tar -zxvf mysqld_exporter-0.14.0.linux-arm64.tar.gz
mv mysqld_exporter-0.14.0.linux-amd64 /usr/local/prometheus/mysqld_exporter
cd /usr/local/prometheus/mysqld_exporter

3.新增.my.cnf文件

# 在mysqld_exporter目录下新建my.cnf文件
vim .my.cnf

配置内容:

[client]
host = 127.0.0.1
user = mysql_monitor
password = 111111

4.登录MySQL,创建MySQL用户并授权

CREATE USER 'mysql_monitor'@'localhost' IDENTIFIED BY '111111' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysql_monitor'@'localhost';
FLUSH PRIVILEGES;
EXIT

5.验证账号

mysql -umysql_monitor -p'111111'

6.开机启动

vim /etc/systemd/system/mysqld-exporter.service

配置如下:

[Unit]
Description=mysqld_exporter

[Service]
ExecStart=/usr/local/prometheus/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/prometheus/mysqld_exporter/.my.cnf --web.listen-address=0.0.0.0:9104 \
--collect.slave_status \
--collect.binlog_size \
--collect.info_schema.processlist \
--collect.info_schema.innodb_metrics \
--collect.engine_innodb_status \
--collect.perf_schema.file_events \
--collect.perf_schema.replication_group_member_stats
Restart=on-failure

[Install]
WantedBy=multi-user.target

启动服务:

# 重新加载服务配置文件
systemctl daemon-reload
# 开启开机启动
systemctl enable mysqld-exporter
# 关闭开机启动
systemctl disable grafana-server
# 重启mysqld-exporter
systemctl restart mysqld-exporter
# 启动mysqld-exporter
systemctl start mysqld-exporter
# 查看状态
systemctl status mysqld-exporter
# 停止mysqld-exporter
systemctl stop mysqld-exporter

6.检查是否启动成功

# mysqld_exporter 默认端口是9104
netstat -tnlp | grep 9104
# 或者 lsof -i:9104

7.配置Prometheus

vim /usr/local/prometheus/prometheus.yml

配置内容:

scrape_configs:
  - job_name: "mysql-status"
    static_configs:
      - targets: ["localhost:9104"]
        labels:
          instance: mysql-status

8.重启Prometheus

systemctl restart prometheus

9.查看结果
http://localhost:9090/targets

最后修改:2022 年 09 月 03 日
如果觉得我的文章对你有用,请随意赞赏