Prometheus Exporter系列-Mysql_Exporter一键部署

新项目旧项目都需要给研发配置mysql监控,这里mysql监控对应aws 阿里云 腾讯云 华为云的云mysql产品或开源自建mysql。

exporter安装虽然简单,经常手动操作不免让人心烦,一键完成省去繁琐的常规操作。

配置信息对的情况下测试多次都可以正常安装,支持一台机器上安装多个数据库exporter,安装时提供不同端口作为参数即可

指定exporter使用的用户名、密码和版本号。默认版本号为文档日期所在最新版本

shell 复制代码
root@exporter-agent:/data/mysql-exporter# cat  install_mysql_exporter.sh 
#!/bin/bash

# MySQL Exporter 一键安装脚本(优化版)
# 使用方法: ./mysql_exporter_setup.sh <导出端口>

set -e

# 配置信息
MYSQL_HOST="db-host"
MYSQL_USER="monitor_user"
MYSQL_PASSWORD="bagayalu321"
MYSQL_PORT="3306"
VER=0.17.2

# 获取导出端口,默认为9104
EXPORTER_PORT=${1:-9104}

# 基础目录
BASE_DIR="/data/mysql-exporter"
# 特定端口的工作目录
WORK_DIR="${BASE_DIR}/${EXPORTER_PORT}"

# 创建基础目录和端口特定目录
mkdir -p $WORK_DIR

echo "===> 开始安装 MySQL Exporter 到 ${WORK_DIR}..."

# 打印创建 MySQL 监控用户的语句(仅供参考,不执行)
echo "===> MySQL 监控用户授权语句(需手动执行):"
cat << EOF
-- 在 MySQL 中创建监控用户的 SQL 语句:
CREATE USER '${MYSQL_MONITOR_USER}'@'%' IDENTIFIED BY '${MYSQL_MONITOR_PASSWORD}';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO '${MYSQL_MONITOR_USER}'@'%';
FLUSH PRIVILEGES;
EOF

cat > $WORK_DIR/create_mysql_user.sql << EOF
-- 在 MySQL 中创建监控用户的 SQL 语句:
CREATE USER '${MYSQL_MONITOR_USER}'@'%' IDENTIFIED BY '${MYSQL_MONITOR_PASSWORD}';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO '${MYSQL_MONITOR_USER}'@'%';
FLUSH PRIVILEGES;
EOF

echo "===> 授权语句已保存到: ${WORK_DIR}/create_mysql_user.sql"
echo "===> 注意: 创建的监控用户为 '${MYSQL_MONITOR_USER}',密码为 '${MYSQL_MONITOR_PASSWORD}'"
echo "===> 如需修改,请编辑授权语句后手动执行"
# 清理旧的服务(如果存在)
if systemctl is-active --quiet mysql_exporter_${EXPORTER_PORT}; then
    echo "===> 停止并禁用旧的 MySQL Exporter 服务..."
    systemctl stop mysql_exporter_${EXPORTER_PORT}
    systemctl disable mysql_exporter_${EXPORTER_PORT}
fi

# 检查 MySQL Exporter 是否已安装
if [ -f "${BASE_DIR}/mysqld_exporter" ] && ${BASE_DIR}/mysqld_exporter --version &>/dev/null; then
    echo "===> MySQL Exporter 已安装,跳过下载步骤..."
    # 确保二进制文件可执行
    chmod +x ${BASE_DIR}/mysqld_exporter
else
    echo "===> 下载并安装 MySQL Exporter..."
    cd $BASE_DIR
    wget https://github.com/prometheus/mysqld_exporter/releases/download/v${VER}/mysqld_exporter-${VER}.linux-amd64.tar.gz
    tar xvfz mysqld_exporter-${VER}.linux-amd64.tar.gz
    mv mysqld_exporter-${VER}.linux-amd64/mysqld_exporter .
    chmod +x mysqld_exporter
    rm -rf mysqld_exporter-${VER}.linux-amd64*
fi

# 创建配置文件
echo "===> 创建 MySQL Exporter 配置文件..."
cat > $WORK_DIR/.my.cnf << EOF
[client]
host=$MYSQL_HOST
port=$MYSQL_PORT
user=$MYSQL_MONITOR_USER
password=$MYSQL_PASSWORD
EOF

chmod 600 $WORK_DIR/.my.cnf

# 保存配置信息
cat > $WORK_DIR/config.info << EOF
# MySQL Exporter 配置信息
MYSQL_HOST=$MYSQL_HOST
MYSQL_PORT=$MYSQL_PORT
MYSQL_USER=$MYSQL_USER
MYSQL_PASSWORD=$MYSQL_PASSWORD
EXPORTER_PORT=$EXPORTER_PORT
EOF

chmod 600 $WORK_DIR/config.info

# 创建 systemd 服务文件
echo "===> 配置 systemd 服务..."
cat > /etc/systemd/system/mysql_exporter_${EXPORTER_PORT}.service << EOF
[Unit]
Description=MySQL Exporter for Prometheus (Port ${EXPORTER_PORT})
After=network.target

[Service]
User=root
Group=root
Type=simple
ExecStart=${BASE_DIR}/mysqld_exporter --config.my-cnf=${WORK_DIR}/.my.cnf --web.listen-address=:${EXPORTER_PORT}
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
EOF

# 重新加载 systemd 配置
systemctl daemon-reload

# 启动 MySQL Exporter 服务
echo "===> 启动 MySQL Exporter 服务..."
systemctl enable mysql_exporter_${EXPORTER_PORT}
systemctl start mysql_exporter_${EXPORTER_PORT}

# 等待服务启动
sleep 5

# 检查服务状态
if systemctl is-active --quiet mysql_exporter_${EXPORTER_PORT}; then
    echo "===> MySQL Exporter 安装成功!服务正在运行"
    echo "===> 监控端口: ${EXPORTER_PORT}"
    echo "===> 可通过访问 http://$(hostname -I | awk '{print $1}'):${EXPORTER_PORT}/metrics 测试"
    
    # 创建清理脚本
    cat > $WORK_DIR/uninstall.sh << EOF
#!/bin/bash
# 卸载此 MySQL Exporter 实例的脚本

systemctl stop mysql_exporter_${EXPORTER_PORT}
systemctl disable mysql_exporter_${EXPORTER_PORT}
rm -f /etc/systemd/system/mysql_exporter_${EXPORTER_PORT}.service
systemctl daemon-reload

echo "MySQL Exporter (端口 ${EXPORTER_PORT}) 已停止并禁用"
echo "可以手动删除目录: ${WORK_DIR}"
EOF
    chmod +x $WORK_DIR/uninstall.sh
    
    echo "===> 卸载脚本已创建: ${WORK_DIR}/uninstall.sh"
else
    echo "===> 安装失败,服务未能正常启动"
    echo "===> 请检查日志: journalctl -u mysql_exporter_${EXPORTER_PORT}"
    exit 1
fi

echo "===> 所有文件均存放在: ${WORK_DIR}"
echo "===> 二进制文件位置: ${BASE_DIR}/mysqld_exporter"
echo "===> 配置信息已保存到: ${WORK_DIR}/config.info"
echo "===> 监控用户创建脚本: ${WORK_DIR}/create_mysql_user.sql"
echo "===> 安装完成!"
相关推荐
川石课堂软件测试3 天前
涨薪技术|Prometheus之HTTP API中使用PromQL
网络协议·测试工具·jmeter·http·单元测试·postman·prometheus
IT界的老黄牛4 天前
Prometheus TSDB 拆不出来、也存不了一年:4 条外部存储出路 + 容量测算
prometheus·时序数据库·监控·thanos·victoriametrics·remote_write
Ningcode_cloud5 天前
Kubernetes 弹性伸缩实验手册:从集群搭建到 HPA / VPA 自动扩缩容
云原生·k8s·prometheus
2601_962097366 天前
可观测性实战:Prometheus+Grafana+OTel构建监控
grafana·prometheus·微服务架构·可观测性·opentelemetry
IT界的老黄牛6 天前
Jenkins 监控一条龙:接入、看板 9964、11 条告警规则直接抄走
jenkins·grafana·prometheus·监控·ci-cd·告警规则
陈皮糖..7 天前
基于 Keepalived 的传统 Web 高可用架构的容器化改造与可观测性升级
运维·docker·性能优化·架构·云计算·prometheus
heimeiyingwang7 天前
【Prometheus·部署篇】存储与容量规划:本地存储、远程存储与长期保存
prometheus
heimeiyingwang7 天前
【Prometheus·可视化篇】Grafana 集成:数据源配置与 Dashboard 设计原则
grafana·prometheus
IT界的老黄牛7 天前
Jenkins 构建卡了 23 天,abort 按钮点了没用
jenkins·maven·prometheus·告警·排查·ci-cd
heimeiyingwang9 天前
【Prometheus·告警篇】告警规则:Recording Rules 与 Alerting Rules 最佳实践
prometheus