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 天前
17、grafana安装
运维·grafana·prometheus·监控
webrtc&ffmpeg_study2 天前
监控平台 Grafana+Prometheus+node_exporter初识
grafana·prometheus
zhixingheyi_tian2 天前
prometheus + grafana
grafana·prometheus
勇敢打工人3 天前
Alertmanager 安装与配置指南
prometheus
水岸齐天3 天前
prometheus和Grafana介绍
grafana·prometheus
cui_win4 天前
Prometheus实战教程 05 - 告警通知实现 - 邮件 + 钉钉 + 自定义告警模板
钉钉·prometheus·邮件通知
杰克逊的日记5 天前
通过Prometheus对GPU集群进行监控以及搭建(小型集群)
prometheus·gpu算力·gpu服务器监控
happy_king_zi5 天前
RabbitMQ-Exporter 监控 TLS 加密的 RabbitMQ 集群
分布式·安全·rabbitmq·prometheus
梁正雄8 天前
6、prometheus资源规划
运维·服务器·服务发现·prometheus·监控
梁正雄8 天前
4、prometheus-服务发现k8s api-2
kubernetes·服务发现·prometheus