当前位置: 代码网 > it编程>数据库>Mysql > MySQL进行监控配置的详细指南

MySQL进行监控配置的详细指南

2025年06月23日 Mysql 我要评论
以下是一个完整的 mysql 监控配置实战方案,涵盖监控工具安装、核心指标采集、可视化展示、告警配置等内容。基于 prometheus + grafana + mysqld_exporter 实现。一

以下是一个完整的 mysql 监控配置实战方案,涵盖监控工具安装、核心指标采集、可视化展示、告警配置等内容。基于 prometheus + grafana + mysqld_exporter 实现。

一、环境准备

1.操作系统:centos 7+ / ubuntu 20+

2.mysql 版本:5.7+ 或 8.0+

3.监控工具栈:

  • prometheus(数据采集与告警)
  • grafana(可视化)
  • mysqld_exporter(mysql 数据导出)

二、部署监控组件

1. 安装 prometheus & grafana

# 下载并解压
wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz
wget https://dl.grafana.com/oss/release/grafana-10.0.0.linux-amd64.tar.gz

# 解压
tar -zxvf prometheus-*.tar.gz
tar -zxvf grafana-*.tar.gz

# 移动到 /usr/local
mv prometheus-* /usr/local/prometheus
mv grafana-* /usr/local/grafana

# 创建符号链接
ln -s /usr/local/prometheus/bin/prometheus /usr/local/bin/prometheus
ln -s /usr/local/grafana/bin/grafana-server /usr/local/bin/grafana-server

2. 配置 prometheus

编辑 /usr/local/prometheus/prometheus.yml,添加以下内容:

global:
  scrape_interval: 15s # 采集频率

scrape_configs:
  - job_name: 'mysql'
    static_configs:
      - targets: ['localhost:9104'] # mysqld_exporter 默认端口

3. 安装 mysqld_exporter

# 下载并解压
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gz
tar -zxvf mysqld_exporter-*.tar.gz
cd mysqld_exporter-*

# 移动到系统路径
mv mysqld_exporter /usr/local/bin/

# 创建配置文件目录
mkdir -p /etc/mysqld_exporter

4. 配置 mysqld_exporter

创建 /etc/mysqld_exporter/.my.cnf,配置 mysql 访问权限:

[client]
user=monitor_user    # 用于监控的 mysql 用户
password=your_password
host=127.0.0.1
port=3306

创建监控用户(替换为实际密码):

create user 'monitor_user'@'localhost' identified by 'your_password';
grant process, replication client, show databases on *.* to 'monitor_user'@'localhost';
flush privileges;

启动 mysqld_exporter:

mysqld_exporter --config.my-cnf=/etc/mysqld_exporter/.my.cnf &

三、配置 prometheus 抓取 mysql 指标

在 prometheus.yml 中添加:

scrape_configs:
  - job_name: 'mysql'
    metrics_path: /metrics
    static_configs:
      - targets: ['localhost:9104']

重启 prometheus:

systemctl restart prometheus

四、部署 grafana 并配置仪表盘

1.启动 grafana:

grafana-server web &

访问 http://<服务器ip>:3000,默认账号 admin/admin。

2.添加 prometheus 数据源:

  • 进入 configuration -> data sources -> add data source
  • 选择 prometheus,填写 url http://localhost:9090,保存。

3.导入 mysql 监控仪表盘:

访问 grafana dashboard 库,搜索 mysql。

推荐导入:

五、核心监控指标

通过 grafana 展示以下关键指标:

1.基础状态:

  • uptime
  • version
  • connections (threads_connected)

2.性能指标:

  • qps (queries per second)
  • tps (transactions per second)
  • innodb buffer pool usage
  • slow queries

3.资源使用:

  • cpu usage (user/system time)
  • memory usage (key buffer, innodb buffer pool)
  • disk i/o (read/write throughput)

4.高可用性:

  • replication status (seconds_behind_master)
  • mgr member state

六、告警配置

在 prometheus 中配置告警规则(/etc/prometheus/alert.rules):

groups:
  - name: mysql_alerts
    rules:
      - alert: highconnections
        expr: mysql_global_status_threads_connected > 100
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "mysql 活跃连接数过高"
      - alert: replicationdelay
        expr: mysql_slave_seconds_behind_master > 300
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "mysql 主从复制延迟超过 5 分钟"

在 prometheus 中加载规则文件,并配置 alertmanager 通知(邮件/钉钉/微信)。

七、自动化脚本增强

编写脚本定期检查慢查询、表碎片、索引效率等:

#!/bin/bash
# slow_query_report.sh

# 获取慢查询日志
slow_log=$(grep "^# query" /var/log/mysql/slow.log | tail -n 10)
echo "慢查询日志:" >> /tmp/mysql_report.txt
echo "$slow_log" >> /tmp/mysql_report.txt

# 检查表碎片
table_fragmentation=$(mysql -e "select table_schema, table_name, round((data_length - index_length) / data_length * 100, 2) as fragmentation from information_schema.tables where fragmentation > 10;")
echo "表碎片情况:" >> /tmp/mysql_report.txt
echo "$table_fragmentation" >> /tmp/mysql_report.txt

# 发送邮件
mail -s "mysql daily report" admin@example.com < /tmp/mysql_report.txt

设置定时任务:

crontab -e
# 每天凌晨 1 点执行
0 1 * * * /path/to/slow_query_report.sh

八、验证与优化

1.模拟故障测试:

  • 停止 mysql 服务,验证 prometheus 是否触发告警。
  • 插入大量慢查询,检查慢查询统计是否正常。

2.调整阈值:

根据业务流量调整 qps、tps、连接数告警阈值。

3.长期存储:

配置 prometheus 长期存储(如 victoriametrics 或 thanos)。

总结

通过以上配置,可以实现对 mysql 的全方位监控,包括实时性能、资源使用、高可用性状态等。结合告警和自动化脚本,可提前发现潜在问题,保障数据库稳定性。

到此这篇关于mysql进行监控配置的详细指南的文章就介绍到这了,更多相关mysql监控配置内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com