您好,登录后才能下订单哦!
# Prometheus服务监控之MySQL监控配置指南
## 一、Prometheus监控体系概述
### 1.1 Prometheus核心组件
Prometheus是一个开源的系统监控和警报工具包,由以下几个核心组件构成:
- **Prometheus Server**:负责抓取和存储时间序列数据
- **Client Libraries**:用于检测应用程序代码
- **Push Gateway**:支持短生命周期任务的指标推送
- **Exporters**:用于暴露现有第三方系统的指标
- **Alertmanager**:处理警报
- **可视化界面**(如Grafana)
### 1.2 监控数据模型
Prometheus所有监控数据都存储为时间序列,具有以下特征:
- 指标名称(metric name)
- 标签(key-value pairs)
- 时间戳
- 浮点数值
示例格式:
## 二、MySQL监控方案设计
### 2.1 监控指标分类
对MySQL数据库的监控主要包含以下方面:
| 类别 | 具体指标示例 |
|----------------|-------------------------------------|
| 性能指标 | QPS, TPS, 查询延迟, 连接数 |
| 资源使用 | CPU, 内存, 磁盘IO, 网络流量 |
| 复制状态 | 主从延迟, 复制线程状态 |
| 存储引擎 | InnoDB缓冲池状态, 锁等待 |
| 慢查询 | 慢查询数量, 执行时间分布 |
### 2.2 技术选型对比
#### 方案一:mysqld_exporter
- 官方推荐方案
- 支持绝大多数MySQL指标
- 与Prometheus生态无缝集成
#### 方案二:Percona监控插件
- 提供更专业的MySQL指标
- 需要额外部署组件
- 适合复杂的企业环境
#### 方案三:自定义脚本+node_exporter
- 灵活性高
- 维护成本大
- 适合特殊定制场景
**推荐选择mysqld_exporter作为基础方案**
## 三、mysqld_exporter部署配置
### 3.1 安装部署
#### 二进制安装(推荐)
```bash
# 下载最新版本
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.0/mysqld_exporter-0.15.0.linux-amd64.tar.gz
# 解压安装
tar xvfz mysqld_exporter-*.tar.gz
cd mysqld_exporter-*
cp mysqld_exporter /usr/local/bin/
docker pull prom/mysqld-exporter
docker run -d \
-p 9104:9104 \
--name mysqld-exporter \
-e DATA_SOURCE_NAME="user:password@(hostname:3306)/" \
prom/mysqld-exporter
创建专用监控账号:
CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'StrongPassword' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
FLUSH PRIVILEGES;
创建配置文件/etc/mysqld_exporter.cnf
:
[client]
user=exporter
password=StrongPassword
host=127.0.0.1
port=3306
常用启动参数:
mysqld_exporter \
--config.my-cnf="/etc/mysqld_exporter.cnf" \
--web.listen-address="0.0.0.0:9104" \
--collect.global_status \
--collect.info_schema.innodb_metrics \
--collect.auto_increment.columns \
--collect.info_schema.processlist \
--collect.binlog_size \
--collect.info_schema.tablestats \
--collect.global_variables \
--collect.info_schema.query_response_time \
--collect.info_schema.userstats \
--collect.info_schema.tables \
--collect.perf_schema.tablelocks \
--collect.perf_schema.file_events \
--collect.perf_schema.eventswaits \
--collect.perf_schema.indexiowaits \
--collect.perf_schema.tableiowaits \
--collect.slave_status \
--log.level="info"
创建systemd服务文件/etc/systemd/system/mysqld_exporter.service
:
[Unit]
Description=MySQL Prometheus Exporter
After=network.target
[Service]
User=mysql
Group=mysql
Type=simple
ExecStart=/usr/local/bin/mysqld_exporter \
--config.my-cnf=/etc/mysqld_exporter.cnf \
--web.listen-address=0.0.0.0:9104
Restart=always
[Install]
WantedBy=multi-user.target
启动服务:
systemctl daemon-reload
systemctl enable mysqld_exporter
systemctl start mysqld_exporter
修改Prometheus配置文件prometheus.yml
:
scrape_configs:
- job_name: 'mysql'
static_configs:
- targets: ['mysql-server:9104']
relabel_configs:
- source_labels: [__address__]
target_label: instance
regex: '(.*):.*'
replacement: '$1'
对于大型MySQL实例,建议启用指标过滤:
metric_relabel_configs:
- source_labels: [__name__]
regex: '(mysql_global_status_wsrep_.*|mysql_slave_status_.*)'
action: keep
对于多实例环境,建议使用服务发现:
scrape_configs:
- job_name: 'mysql'
consul_sd_configs:
- server: 'consul:8500'
services: ['mysql']
relabel_configs:
- source_labels: [__meta_consul_service]
target_label: job
推荐使用以下社区仪表盘: - MySQL Overview (ID: 7362) - Percona MySQL/Graphite Dashboard (ID: 4201)
导入方法: 1. 登录Grafana 2. 左侧菜单选择”+” > Import 3. 输入仪表盘ID 4. 选择对应的Prometheus数据源
创建mysql_rules.yml
文件:
groups:
- name: mysql-alerts
rules:
- alert: MySQLDown
expr: up{job="mysql"} == 0
for: 1m
labels:
severity: critical
annotations:
summary: "MySQL instance down (instance {{ $labels.instance }})"
- alert: HighThreadsRunning
expr: mysql_global_status_threads_running > 50
for: 5m
labels:
severity: warning
annotations:
summary: "High number of running threads ({{ $value }})"
- alert: ReplicationNotRunning
expr: mysql_slave_status_slave_io_running != 1 or mysql_slave_status_slave_sql_running != 1
for: 2m
labels:
severity: critical
annotations:
summary: "MySQL replication stopped (instance {{ $labels.instance }})"
配置邮件通知:
route:
receiver: 'email-alerts'
group_wait: 30s
group_interval: 5m
receivers:
- name: 'email-alerts'
email_configs:
- to: 'dba-team@example.com'
from: 'prometheus-alerts@example.com'
smarthost: 'smtp.example.com:587'
auth_username: 'alertuser'
auth_password: 'password'
send_resolved: true
通过--collect.*
参数控制指标收集范围:
# 只收集性能相关指标
--collect.global_status --collect.info_schema.innodb_metrics --collect.slave_status
配置my.cnf启用慢查询日志:
[mysqld]
slow_query_log = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
long_query_time = 2
log_queries_not_using_indexes = 1
使用pt-query-digest分析:
pt-query-digest /var/log/mysql/mysql-slow.log | tee slow-report.log
调整抓取间隔:对于生产环境建议30s-1m
scrape_interval: 30s
启用指标缓存:
--web.telemetry-path="/metrics" \
--web.disable-exporter-metrics
限制指标数量:
--collect.info_schema.tables.databases="production_db" \
--collect.perf_schema.eventswaits.limit=100
# 测试exporter连接
mysql --defaults-file=/etc/mysqld_exporter.cnf -e "SHOW STATUS"
# 查看exporter自身资源使用
top -p $(pgrep -f mysqld_exporter)
# 检查抓取耗时
curl -s http://localhost:9104/metrics | wc -l
网络隔离:
认证加密:
--web.config.file="/etc/prometheus/web.yml"
web.yml内容:
basic_auth_users:
prom_user: "$2y$10$hashedpassword"
最小权限原则:
与Kubernetes集成:
智能告警:
性能分析集成:
通过以上完整配置,您将建立起一个专业的MySQL监控体系,能够及时发现性能瓶颈、预测容量问题并快速响应故障,为数据库的稳定运行提供有力保障。 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。