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 "===> 安装完成!"
相关推荐
码上淘金13 小时前
【Prometheus】业务指标与基础指标的标签来源差异及设计解析(扩展版)
prometheus
Yang三少喜欢撸铁5 天前
通过Docker部署Prometheus + Grafana搭建监控平台【超详细版】
linux·服务器·docker·容器·grafana·prometheus
liuyunshengsir6 天前
Gin 集成 prometheus 客户端实现注册和暴露指标
prometheus·gin
小黑_深呼吸7 天前
k8s平台:手动部署Grafana
运维·学习·kubernetes·grafana·prometheus
阿桨7 天前
【Prometheus-OracleDB Exporter安装配置指南,开机自启】
数据库·oracle·centos·prometheus
川石教育8 天前
Prometheus使用Recoding Rules优化性能
prometheus·普罗米修斯·普罗修斯监控
yunson_Liu8 天前
prometheus手动添加k8s集群外的node-exporter监控
容器·kubernetes·prometheus
阿桨8 天前
【Prometheus-Mongodb Exporter安装配置指南,开机自启】
数据库·mongodb·prometheus
小马爱打代码8 天前
Spring Boot × K8s 监控实战-集成 Prometheus 与 Grafana
spring boot·kubernetes·prometheus
yunson_Liu9 天前
prometheus通过Endpoints自定义grafana的dashboard模块
贪心算法·grafana·prometheus