linux 使用 MySQL Performance Schema 和 Prometheus + Grafana 来监控 MySQL 性能

🎯 方案 1:使用 MySQL Performance Schema

Performance Schema 是 MySQL 内置的性能监控工具,适合排查慢查询、锁等待、资源使用等问题。

✅ 步骤 1:启用 Performance Schema

1️⃣ 在 MySQL 配置文件中(通常是 /etc/mysql/my.cnf 或 /etc/mysql/mysql.conf.d/mysqld.cnf),启用 Performance Schema:

ini 复制代码
[mysqld]
performance_schema = ON

2️⃣ 重启 MySQL 服务:

bash 复制代码
sudo systemctl restart mysql

3️⃣ 登录到 MySQL,确认 Performance Schema 已启用:

bash 复制代码
mysql -u root -p

执行以下命令:

sql 复制代码
SHOW VARIABLES LIKE 'performance_schema';

✅ 步骤 2:查询常用的 Performance Schema 表

1️⃣ 查看 活跃的连接和查询:

sql 复制代码
SELECT * FROM performance_schema.events_statements_current;

2️⃣ 查看 慢查询统计:

sql 复制代码
SELECT * FROM performance_schema.events_statements_summary_by_digest ORDER BY COUNT_STAR DESC LIMIT 10;

3️⃣ 查看 锁等待时间:

sql 复制代码
SELECT * FROM performance_schema.events_waits_summary_global_by_event_name;

🎯 方案 2:使用 Prometheus + Grafana 监控 MySQL

✅ 步骤 1:安装 Prometheus 和 Grafana

➡️ 1. 在 Linux 上安装 Prometheus
bash 复制代码
sudo apt update
sudo apt install prometheus
➡️ 2. 安装 Grafana
bash 复制代码
sudo apt install grafana

启动 Grafana:

bash 复制代码
sudo systemctl start grafana-server

访问 Grafana Web 界面:

复制代码
http://localhost:3000

默认登录:

  • 用户名:admin
  • 密码:admin

✅ 步骤 2:安装 MySQL Exporter

Prometheus 需要一个 MySQL Exporter 来收集 MySQL 的性能数据。

➡️ 1. 下载并配置 MySQL Exporter
bash 复制代码
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gz
tar xzf mysqld_exporter-0.14.0.linux-amd64.tar.gz
cd mysqld_exporter-0.14.0.linux-amd64
➡️ 2. 创建 MySQL 用户

在 MySQL 中创建一个用户来访问 Performance Schema:

sql 复制代码
CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'your_password';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
FLUSH PRIVILEGES;
➡️ 3. 启动 MySQL Exporter
bash 复制代码
./mysqld_exporter --config.my-cnf="/path/to/.my.cnf"

✅ 步骤 3:配置 Prometheus

编辑 Prometheus 的配置文件 prometheus.yml,添加 MySQL Exporter:

yaml 复制代码
scrape_configs:
  - job_name: 'mysql'
    static_configs:
      - targets: ['localhost:9104']

重启 Prometheus:

bash 复制代码
sudo systemctl restart prometheus

✅ 步骤 4:配置 Grafana

1️⃣ 在 Grafana Web 界面中,添加 Prometheus 作为数据源。

2️⃣ 导入现成的 MySQL 监控仪表板 模板(ID: 7362)。


✅ 最终效果

  • 通过 Grafana,可以实时查看 MySQL 查询性能、慢查询、连接数、缓存命中率、磁盘 I/O 等关键指标。

  • 可以设置告警,及时发现 查询延迟、磁盘空间不足 等问题。


⚙️ 常用 Prometheus + Grafana MySQL 监控指标

指标 说明
mysql_global_status 全局 MySQL 状态
mysql_up MySQL 是否正常运行
mysql_global_variables MySQL 全局变量
mysql_queries_per_second 每秒查询次数
mysql_slow_queries 慢查询次数

🛠 总结

方案 适用场景
Performance Schema 轻量级性能分析工具
Prometheus + Grafana 实时监控和告警
相关推荐
FungLeo13 小时前
成为全栈·基础补充·数据库迁移与事务:数据结构怎样安全演进
mysql·postgresql·sqlite·数据一致性·数据库事务·数据库迁移·成为全栈
2601_9679873718 小时前
安装 Redis 或 Redis Stack
linux·windows·redis·macos·安装
feng_you_ying_li18 小时前
linux之数据链路层
linux·网络·智能路由器
GeW19 小时前
高端化走到深处,是数据库和Linux的事
linux
aiks_yuan19 小时前
MySQL GROUP BY 很慢怎么优化:加了索引反而从 1 秒变 7 秒,Using temporary 该不该消(MySQL 8.2 实测 400 万行)
mysql
程序员初一20 小时前
Linux 远程桌面有四种做法
linux·ssh
数据库小学妹20 小时前
MySQL大字段治理:行溢出机制、覆盖索引与拆表
数据库·mysql·性能优化
程序开发阿元21 小时前
1C1G 能跑什么:把留常驻服务和不留摆进一张内存表
linux·ssh
这个DBA有点耶1 天前
MySQL 8.0不可见索引:删索引怕出事,不删又拖慢写入——中间态怎么用?
数据库·mysql·代码规范
xbzb1 天前
自定义 yum 软件源:Linux 上 createrepo,Windows 上纯 Python 也能造
linux·运维·windows·python·yum·createrepo