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 "===> 安装完成!"
相关推荐
云游1 天前
大模型性能指标的监控系统(prometheus3.5.0)和可视化工具(grafana12.1.0)基础篇
grafana·prometheus·可视化·监控
qq_232045572 天前
非容器方式安装Prometheus和Grafana,以及nginx配置访问Grafana
nginx·grafana·prometheus
夜莺云原生监控2 天前
Prometheus 监控 Kubernetes Cluster 最新极简教程
容器·kubernetes·prometheus
SRETalk3 天前
Prometheus 监控 Kubernetes Cluster 最新极简教程
kubernetes·prometheus
川石课堂软件测试3 天前
JMeter并发测试与多进程测试
功能测试·jmeter·docker·容器·kubernetes·单元测试·prometheus
SRETalk4 天前
夜莺监控的几种架构模式详解
prometheus·victoriametrics·nightingale·夜莺监控
Ditglu.5 天前
使用Prometheus + Grafana + node_exporter实现Linux服务器性能监控
服务器·grafana·prometheus
SRETalk5 天前
监控系统如何选型:Zabbix vs Prometheus
zabbix·prometheus
睡觉z5 天前
云原生环境Prometheus企业级监控
云原生·prometheus
归梧谣5 天前
云原生环境 Prometheus 企业级监控实战
云原生·prometheus