在基础业务架构中,关系型数据库是不可或缺的核心组件之一。MySQL作为最为广泛使用的数据库之一,至今仍然拥有着广泛的应用。对MySQL数据库进行有效的监控是至关重要的,它有助于我们深入了解数据库的运行状态。这不仅使得在测试环境中更好地观察数据库性能表现成为可能,也确保了正式环境中数据库的稳定运行。通过结合预警和告警规则,我们能够保障在问题发生时,业务相关人员能够及时发现并采取相应的措施。有助于进一步保障数据库的可用性和稳定性。
1 配置初始化
# 配置初始化
# selinux 关闭
setenforce 0 && sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config && getenforce
# firewalld 关闭
systemctl disable firewalld && systemctl stop firewalld
# swap 关闭
swapoff -a
# 注释 fstab 中 swap 挂载
vim /etc/fstab
# 创建 prometheus 用户
useradd -s /sbin/nologin prometheus
2 MySQL 数据库中创建 MySQL_expoter 用户
# 注意:exporter 的密码不要有 @ 符号,会导致认证错误
CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'XXXXXXXX' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
3 安装 MySQL_exporter
# 官方 Github下载mysql-exporter
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.0/mysqld_exporter-0.15.0.linux-amd64.tar.gz
# 解压工具
tar xf mysqld_exporter-0.15.0.linux-amd64.tar.gz
# 移动到工具目录
mv mysqld_exporter-0.15.0.linux-amd64 /usr/local/mysqld_exporter
# 授权 Prometheus 用户
chown prometheus.prometheus -R /usr/local/mysqld_exporter
4 MySQL_exporter 开机启动 (systemd)
cat > /usr/lib/systemd/system/mysql_exporter.service << EOF
[Unit]
Description=mysql_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
Group=prometheus
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter \
--collect.info_schema.processlist \
--collect.info_schema.tables \
--collect.info_schema.innodb_tablespaces \
--config.my-cnf=/usr/local/mysqld_exporter/config/config.my-cnf
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
5 配置 Prometheus MySQL 监控规则
# vim /usr/local/prometheus/prometheus.yml
# 通过 sd 方式采集
- job_name: mysql_ds # To get metrics about the mysql exporter’s targets
file_sd_configs:
- files:
- /usr/local/prometheus/sd/mysql-instances.json
metrics_path: /probe
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
# The mysqld_exporter host:port
replacement: localhost:9104
- source_labels: [auth_module]
target_label: __param_auth_module
- action: labeldrop
regex: auth_module
6 配置多 MySQL 的文件发现规则
# vim /usr/local/prometheus/sd/mysql-instances.json
[
{
"targets": ["127.0.0.1:3306"],
"labels": {
"app": "MySQL",
"modes": "Primary DB system",
"auth_module": "client"
}
},
{
"targets": ["127.0.0.2:3306"],
"labels": {
"app": "MySQL",
"modes": "Read replica",
"auth_module": "client" # 为每个MySQL指定认证模块
}
}
]
7 配置多 MySQL 权限
# vim /usr/local/mysqld_exporter/config/config.my-cnf
[client]
user=exporter
password='xxxxxx'
8 调试命令
注意: 如果出现某个数据库实例无法获取指标的情况,可以通过如下的命令调试请求单一数据库的指标
# 默认会使用 client 模块的认证
# client 与 config.my-cnf 中的 client 对应
curl http://127.0.0.1:9104/probe?target=192.168.1.167:3306
9 参考资料
mysql_expoter GitHub 官网
https://github.com/prometheus/mysqld_exporter
mysql_exporter 所需权限
https://github.com/prometheus/mysqld_exporter?tab=readme-ov-file#required-grants
mysql_exporter 收集标签参考
https://github.com/prometheus/mysqld_exporter?tab=readme-ov-file#collector-flags
🚀 炸鸡物料库 🚀
🔍 内容定位: IT技术领域,云计算、云原生、DevOps 和 AI 等相关的技术资讯、实践和经验分享。面向开发、系统和云计算,提供实用的技术内容。
🎯 目标受众: 面向开发者、系统管理员和云计算从业者,提供实用的技术内容。
🗓️ 更新频率:每周更新,每次分享都为您带来有价值的技术内容。
🤝 互动与反馈: 鼓励读者留言、评论,有问题随时提问,我们将积极互动并提供帮助。
💌 订阅方式: 扫描二维码或搜索“炸鸡物料库”关注,不错过每一篇精彩内容。
🌐 合作与分享: 如果您有技术分享或合作计划,欢迎联系我们,共同成长!