Files
ljwx-k8s/infra/database/monitoring/MYSQL-MONITORING-README.md
2026-02-20 22:50:45 +08:00

6.7 KiB
Raw Permalink Blame History

MySQL 监控配置完成

概述

已成功配置 MySQL 数据库的 Prometheus 监控和 Grafana Dashboard,包括:

  • MySQL 8.0 数据库(StatefulSet 部署)
  • MySQL Exporter v0.15.1(独立 Deployment)
  • Prometheus ServiceMonitor(自动服务发现)
  • Grafana Dashboard(MySQL Database Monitoring)

部署架构

┌─────────────────┐         ┌──────────────────┐         ┌─────────────┐
│  MySQL 8.0      │◄────────│  MySQL Exporter  │◄────────│ Prometheus  │
│  (infra ns)     │  3306   │  (monitoring ns) │  9104   │             │
│  StatefulSet    │         │  Deployment      │         └─────────────┘
└─────────────────┘         └──────────────────┘                │
                                                                 ▼
                                                         ┌─────────────┐
                                                         │  Grafana    │
                                                         │  Dashboard  │
                                                         └─────────────┘

部署的组件

1. MySQL 数据库 (infra namespace)

  • 部署文件: /tmp/mysql-infra-simple.yaml
  • 类型: StatefulSet
  • 镜像: mysql:8.0
  • 命名空间: infra
  • Pod: mysql-infra-0
  • Service: mysql-infra (ClusterIP: None, Headless)
  • 存储: 20Gi PVC (local-path)
  • 凭据:
    • Root 密码: 123456
    • 数据库: appdb
    • 用户: appuser / appuser-password-123

2. MySQL Exporter (monitoring namespace)

  • 部署文件: infra/observability/mysql-exporter.yaml
  • 镜像: harbor.eu.lingjingwanxiang.cn/dockerhub-proxy/prom/mysqld-exporter:v0.15.1
  • 命名空间: monitoring
  • 连接方式:
    --mysqld.address=mysql-infra.infra.svc.cluster.local:3306
    --mysqld.username=root
    MYSQLD_EXPORTER_PASSWORD=123456
    
  • Metrics 端口: 9104
  • ServiceMonitor: 已启用,30秒抓取间隔

3. Grafana Dashboard

  • 配置文件: infra/database/monitoring/grafana-mysql-dashboard.yaml
  • Dashboard UID: mysql-monitoring
  • Dashboard 名称: MySQL Database Monitoring
  • 包含面板:
    • MySQL 状态(UP/DOWN)
    • 活跃连接数
    • 每秒查询数 (QPS)
    • 运行时间
    • 连接使用率
    • 查询性能(包括慢查询)
    • InnoDB 缓冲池使用情况
    • InnoDB 缓冲池命中率
    • 命令操作统计(SELECT/INSERT/UPDATE/DELETE)
    • 网络流量
    • 表锁统计
    • InnoDB 行操作
    • 中断连接统计
    • 临时对象创建统计

可用的 MySQL 指标

主要指标包括:

  • mysql_up: MySQL 运行状态 (1=UP, 0=DOWN)
  • mysql_global_status_threads_connected: 当前连接数
  • mysql_global_status_threads_running: 运行中的线程数
  • mysql_global_status_queries: 查询总数
  • mysql_global_status_slow_queries: 慢查询总数
  • mysql_global_status_innodb_buffer_pool_bytes_*: InnoDB 缓冲池指标
  • mysql_global_status_innodb_rows_*: InnoDB 行操作指标
  • mysql_global_status_commands_total: 各类命令执行统计
  • mysql_global_status_bytes_sent/received: 网络流量
  • mysql_global_status_table_locks_*: 表锁统计
  • mysql_global_variables_max_connections: 最大连接数配置

验证步骤

运行验证脚本:

./verify-mysql-monitoring.sh

或手动验证:

1. 检查 MySQL 状态

kubectl get pod mysql-infra-0 -n infra
kubectl logs mysql-infra-0 -n infra

2. 检查 MySQL Exporter 状态

kubectl get pod -n monitoring -l app=mysql-exporter
kubectl logs -n monitoring -l app=mysql-exporter

3. 测试 Metrics 端点

kubectl exec -n monitoring deployment/mysql-exporter -- \
  wget -q -O- http://localhost:9104/metrics | grep mysql_up

4. 查看 Prometheus Targets

kubectl port-forward -n monitoring svc/prometheus-operated 9090:9090

访问: http://localhost:9090/targets 查找 monitoring/mysql-exporter/0 target

5. 访问 Grafana Dashboard

kubectl port-forward -n monitoring svc/grafana 30300:80

访问: http://localhost:30300

配置文件位置

  • MySQL Exporter 部署: infra/observability/mysql-exporter.yaml
  • MySQL Exporter Secret 示例: infra/observability/mysql-secret-example.yaml
  • Grafana Dashboard: infra/database/monitoring/grafana-mysql-dashboard.yaml
  • 验证脚本: verify-mysql-monitoring.sh

故障排查

MySQL Exporter 无法连接

# 检查 MySQL 是否可访问
kubectl exec -n monitoring deployment/mysql-exporter -- \
  nc -zv mysql-infra.infra.svc.cluster.local 3306

# 检查密码是否正确
kubectl get deployment mysql-exporter -n monitoring -o yaml | grep MYSQLD_EXPORTER_PASSWORD

Prometheus 未抓取指标

# 检查 ServiceMonitor
kubectl get servicemonitor mysql-exporter -n monitoring -o yaml

# 检查 Prometheus 配置
kubectl logs -n monitoring prometheus-prometheus-0 | grep mysql-exporter

Dashboard 未显示数据

# 检查 Grafana 数据源
kubectl exec -n monitoring deployment/grafana -- \
  wget -q -O- http://localhost:3000/api/datasources

# 检查 Prometheus 查询
kubectl port-forward -n monitoring svc/prometheus-operated 9090:9090
# 然后在 Prometheus UI 中测试查询: mysql_up{namespace="infra"}

后续优化建议

  1. 创建监控用户:使用专用的 exporter 用户而不是 root

    CREATE USER 'exporter'@'%' IDENTIFIED BY 'exporter-password' WITH MAX_USER_CONNECTIONS 3;
    GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'%';
    FLUSH PRIVILEGES;
    
  2. 启用更多 Collector:在 mysql-exporter.yaml 中启用额外的指标收集器

    • COLLECT_INFO_SCHEMA_TABLES=true
    • COLLECT_PERF_SCHEMA_*=true
  3. 配置告警规则:添加 PrometheusRule 用于关键指标告警

  4. 持久化 MySQL 数据:确保 PVC 使用可靠的存储类

  5. 配置备份:参考 mysql-backup.yaml 配置定期备份

相关文档

维护记录

  • 2025-12-23: 初始部署完成
    • MySQL 8.0 StatefulSet 部署
    • MySQL Exporter v0.15.1 配置
    • Grafana Dashboard 创建
    • ServiceMonitor 集成