5.5 KiB
5.5 KiB
MySQL Monitoring Dashboard 访问指南
Dashboard 信息
- 名称: MySQL Database Monitoring
- UID: mysql-monitoring
- 版本: 2
- 状态: ✅ 已修复并可用
快速访问
1. 启动 Port Forward (如果未运行)
kubectl port-forward -n monitoring svc/grafana 30300:80
2. 访问 Dashboard
在浏览器中打开以下地址:
http://localhost:30300/d/mysql-monitoring/mysql-database-monitoring
3. 登录凭据
- 用户名: admin
- 密码: admin123
Dashboard 面板概览
核心状态指标
- MySQL Status - 数据库运行状态 (UP/DOWN)
- Active Connections - 当前活跃连接数
- Queries Per Second (QPS) - 每秒查询数
- Uptime - 数据库运行时间
连接和性能
- Connection Usage - 连接使用情况(已连接/运行中/最大连接数)
- Query Performance - 查询性能(查询/问题/慢查询)
InnoDB 存储引擎
- InnoDB Buffer Pool Usage - 缓冲池使用情况(数据/脏页/总大小)
- InnoDB Buffer Pool Hit Rate - 缓冲池命中率(越高越好,>95% 为优)
操作统计
- Command Operations - 命令操作统计(SELECT/INSERT/UPDATE/DELETE)
- Network Traffic - 网络流量(接收/发送)
- Table Locks - 表锁统计(立即获得/等待)
- InnoDB Row Operations - InnoDB 行操作(读/插入/更新/删除)
错误和异常
- Aborted Connections - 中断连接统计
- Temporary Objects - 临时对象创建(临时表/磁盘临时表/临时文件)
当前监控数据验证
MySQL Exporter 状态
kubectl get pod -n monitoring -l app=mysql-exporter
# 预期: Running
kubectl logs -n monitoring -l app=mysql-exporter --tail=5
# 预期: 看到 "Listening on [::]:9104"
指标可用性检查
# 检查 mysql_up 指标
kubectl exec -n monitoring deployment/mysql-exporter -- \
wget -q -O- http://localhost:9104/metrics | grep "^mysql_up "
# 预期输出: mysql_up 1
Prometheus Target 状态
kubectl port-forward -n monitoring svc/prometheus-operated 9090:9090
访问: http://localhost:9090/targets
查找: monitoring/mysql-exporter/0
状态应该是: UP
常见问题排查
Dashboard 无数据显示
-
检查时间范围
- Dashboard 默认显示最近 1 小时数据
- 点击右上角时间选择器调整范围
-
检查 Prometheus 数据源
# 在 Grafana 中 # Configuration → Data Sources → Prometheus # 点击 "Test" 按钮,应该显示 "Data source is working" -
检查 MySQL Exporter 是否正常
kubectl get pod -n monitoring -l app=mysql-exporter kubectl logs -n monitoring -l app=mysql-exporter
404 或 400 错误
已修复问题:
- ✅ 数据源 UID 已更新为正确值
- ✅ 注解配置已修复为对象格式
- ✅ Dashboard JSON 结构已验证
如果仍有问题,重新导入 Dashboard:
./import-mysql-dashboard.sh
指标显示 "No data"
-
验证 MySQL 是否可访问
kubectl get pod mysql-infra-0 -n infra # 应该是 Running -
测试 Exporter 连接
kubectl exec -n monitoring deployment/mysql-exporter -- \ nc -zv mysql-infra.infra.svc.cluster.local 3306 # 应该显示: Connection succeeded -
检查 ServiceMonitor
kubectl get servicemonitor mysql-exporter -n monitoring
关键指标说明
mysql_up
- 含义: MySQL 运行状态
- 值: 1 = UP, 0 = DOWN
- 告警阈值: = 0
mysql_global_status_threads_connected
- 含义: 当前连接数
- 正常范围: < max_connections 的 70%
- 告警阈值: > 150 (配置的 max_connections 为 200)
InnoDB Buffer Pool Hit Rate
- 含义: 缓冲池命中率
- 计算: (读请求 / (读请求 + 磁盘读)) * 100
- 优秀: > 99%
- 良好: 95-99%
- 需优化: < 95%
mysql_global_status_slow_queries
- 含义: 慢查询累计数
- 配置: long_query_time = 2 秒
- 监控: rate(slow_queries[5m]) - 每秒慢查询数
- 告警: 如果持续 > 1/s 需要优化
性能优化建议
根据 Dashboard 指标进行优化:
-
连接数过高
- 检查应用连接池配置
- 考虑增加 max_connections
- 检查是否有连接泄漏
-
Buffer Pool 命中率低
- 增加 innodb_buffer_pool_size
- 当前配置: 1GB
- 推荐: 物理内存的 50-70%
-
慢查询多
- 查看慢查询日志
- 添加或优化索引
- 优化查询 SQL
-
临时磁盘表过多
- 增加 tmp_table_size
- 增加 max_heap_table_size
- 优化查询避免使用临时表
相关命令
# 查看完整验证
./verify-mysql-monitoring.sh
# 重新导入 Dashboard
./import-mysql-dashboard.sh
# 查看 MySQL 日志
kubectl logs mysql-infra-0 -n infra
# 查看 Exporter 日志
kubectl logs -n monitoring -l app=mysql-exporter
# 进入 MySQL 容器
kubectl exec -it mysql-infra-0 -n infra -- mysql -uroot -p123456
监控数据示例
当前实时指标(示例):
mysql_up = 1 # MySQL 运行正常
mysql_threads_connected = 1 # 1 个活跃连接
mysql_global_status_queries = 258 # 总查询数
mysql_global_status_slow_queries = 0 # 无慢查询
mysql_innodb_buffer_pool_bytes = 20316160 # Buffer Pool: ~19.4 MB
更新记录
- 2025-12-23 14:30: 修复数据源 UID 配置
- 2025-12-23 14:25: 修复注解配置格式
- 2025-12-23 14:00: 初始 Dashboard 创建