Linux安装mysql_exporter

mysqld_exporter 是一个用于监控 MySQL 数据库的 Prometheus exporter。可以从 MySQL 数据库的 metrics_schema 收集指标,相关指标主要包括:

  • MySQL 服务器指标:例如 uptime、version 等
  • 数据库指标:例如 schema_name、table_rows 等
  • 表指标:例如 table_name、engine、rows 等
  • 用户指标:例如 user、max_connections 等
  • InnoDB 指标:例如 innodb_buffer_pool_pages_dirty 等
  • 网络指标:例如 bytes_sent 等

0、前提:

已安装MySQL

进入mysql安装路径

复制代码
cd /usr/local/mysql

1)查看是否启动

复制代码
mysql.server status

没有的话先启动

复制代码
# 启动MySQL服务
sudo mysql.server start

2)登录

复制代码
mysql -u root -p

先查看是否以前已创建mysql_exporter用户

查看用户信息:

复制代码
SELECT USER FROM mysql.USER;

如果已有的话,下一步骤3)则不需要

3)创建exporter新用户

创建新用户

复制代码
CREATE USER 'mysql_exporter'@'%' IDENTIFIED BY 'password';

赋予特定数据库的所有权限

复制代码
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysql_exporter'@'%';

刷新权限使变更生效

复制代码
FLUSH PRIVILEGES;

查看用户信息:

复制代码
SELECT USER FROM mysql.USER;

quit;退出

一、Linux 下载设置mysqld_exporter

1、下载

法一.官网下载 (推荐)

Download | Prometheus

上传到Linux系统

法二、线上下载

访问GitHub上的MySQL exporter项目页面(如:https://github.com/prometheus/mysqld_exporter/releases),找到适合您操作系统和架构的最新稳定版本的二进制文件。

例如,对于Linux amd64系统:

复制代码
wget https://github.com/prometheus/mysqld_exporter/releases/download/v<version>/mysqld_exporter-<version>.linux-amd64.tar.gz

2.解压

复制代码
tar -xvf mysqld_exporter-0.17.2.linux-amd64.tar.gz

3.移动位置(可选)

如果有需要,将解压后的可执行文件移动到系统的合适路径,例如 /usr/local/bin

复制代码
sudo mv mysqld_exporter-0.17.2.linux-amd64 /usr/local/

4.创建配置文件.cnf

创建

复制代码
touch mysql_exporter.cnf

编辑配置文件,填写你的MySQL凭证

复制代码
vim mysql_exporter.cnf

[client]
user=mysql_exporter
password=123456
port=3306

5.运行mysqld_exporter

法一:直接运行(推荐--已验证)

如果不在路径下,先进入下载路径:

复制代码
cd /usr/local/mysqld_exporter-0.17.2.linux-amd64

./mysqld_exporter --web.listen-address=:9104 --config.my-cnf=/usr/local/mysqld_exporter-0.17.2.linux-amd64/mysql_exporter.cnf &

ss -lnpt|grep 9104

法二:将mysqld_exporter做成服务(没有验证成功)

复制代码
vim /usr/lib/systemd/system/mysqld_exporter.service

[Unit]
Description=mysqld_exporter
After=network.target

[Service]
ExecStart=/usr/local/mysqld_exporter-0.17.2.linux-amd64/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter-0.17.2.linux-amd64/.mysql_exporter.cnf


[Install]
WantedBy=multi-user.target

设置开机启动

复制代码
systemctl enable mysqld_exporter

重载配置

复制代码
systemctl daemon-reload

启动

复制代码
systemctl start mysqld_exporter

查看端口

复制代码
ss -lnpt|grep 9104

重启

复制代码
systemctl restart mysqld_exporter

法三:运行 (没有验证)

运行MySQL exporter

可以直接运行mysqld_exporter,通过命令行参数提供数据库连接信息:

复制代码
nohup ./mysqld_exporter \
  --collect.info_schema.processlist \
  --collect.global_status \
  --collect.global_variables \
  --collect.slave_status \
  --collect.engine_innodb_status \
  --mysql.user=exporter \
  --mysql.password=password \
  --web.listen-address=:9104 &

这里的示例启用了多个收集器并指定了监听端口为9104,以及MySQL连接的用户名和密码

二、设置Prometheus.yml

普罗米修斯Prometheus监控安装(mac)-CSDN博客

在 mac的Prometheus.yml中添加mysqld_exporter的配置

  • Prometheus.yml配置路径

路径

/usr/local/etc/prometheus.yml

复制代码
- job_name: "mysql"
    static_configs:
        - targets: ["mysql的ip地址:9104"]  

mac端重新加载:

复制代码
prometheus --config.file=/usr/local/etc/prometheus.yml

端口9104可以根据你的需求更改,只要它不与系统上其他服务的端口冲突。

安装完成后,你可以通过访问http://localhost:9104/metrics来验证mysqld_exporter是否正常工作,它应该输出Prometheus格式的指标数据

Prometheus验证配置

通过Prometheus查看配置是否成功:

复制代码
http://localhost:9090/targets

注意事项

  • 确保mysqld_exporter使用的MySQL连接参数与实际环境匹配。
  • 遵循最小权限原则,仅授予Exporter所需的最低权限。
  • 监控生产环境时,请考虑安全性因素,比如使用SSL加密连接等。
相关推荐
猫猫的小茶馆12 小时前
【Linux 驱动开发】七. 中断下半部
linux·arm开发·驱动开发·stm32·嵌入式硬件·mcu
cyber_两只龙宝12 小时前
LVS-DR模式实验配置及原理详解
linux·网络·云原生·智能路由器·lvs·dr模式
好好学习啊天天向上17 小时前
C盘容量不够,python , pip,安装包的位置
linux·python·pip
a努力。17 小时前
国家电网Java面试被问:混沌工程在分布式系统中的应用
java·开发语言·数据库·git·mysql·面试·职场和发展
li_wen0117 小时前
文件系统(八):Linux JFFS2文件系统工作原理、优势与局限
大数据·linux·数据库·文件系统·jffs2
wypywyp18 小时前
2.虚拟机一直显示黑屏,无法打开,可能是分配的硬盘空间不够
linux·运维·服务器
SongYuLong的博客18 小时前
TL-WR710N-V2.1 硬改刷机OpenWRT源码编译固件
linux·物联网·网络协议
AlfredZhao18 小时前
Docker 快速入门:手把手教你打包 Python 应用
linux·docker·podman
HIT_Weston19 小时前
107、【Ubuntu】【Hugo】搭建私人博客:模糊搜索 Fuse.js(三)
linux·javascript·ubuntu
Fᴏʀ ʏ꯭ᴏ꯭ᴜ꯭.19 小时前
Haproxy会话保持:基于Cookie优化
运维·负载均衡