当前位置: 首页 > news >正文

Prometheus MySQL 性能监控

一、 介绍

Prometheus 是一种开源的监控系统和时序数据库,旨在收集和处理大量数据并提供可视化、监控警报等功能。它支持多种语言、多种部署方式,并且非常灵活,而且社区支持非常活跃,为用户提供了很多优秀的解决方案。
MySQL 是一种流行的关系型数据库管理系统,用于存储和管理结构化数据。MySQL 数据库对于 web 应用程序、企业级应用程序和数据仓库等应用场景都非常适用。

Prometheus 提供了许多适用于 MySQL 监控的插件和可视化界面,让用户可以方便地监控 MySQL 数据库的健康状态、性能指标以及异常情况。下面是使用 Prometheus 进行 MySQL 性能监控的步骤:

二、mysql 指标采集器安装与使用

步骤1:安装和配置 Prometheus

首先需要安装和配置 Prometheus,可以参考官方文档进行操作。

步骤2:安装 mysqld_exporter

mysqld_exporter 是一个用于采集 MySQL 数据库的指标信息的工具。可以通过以下命令进行下载和安装:

wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
tar -zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz
cd mysqld_exporter-0.12.1.linux-amd64/

步骤3:配置 mysqld_exporter

打开 mysqld_exporter 的主配置文件 my.cnf,根据实际需求修改其中的配置项。例如,如果要指定 MySQL 数据库的用户名和密码,则需要在 my.cnf 文件中添加以下内容:

[client]
user=USERNAME
password=PASSWORD

步骤4:启动 mysqld_exporter

启动 mysqld_exporter 之前,最好先执行一遍检查:

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

然后再执行启动命令:

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

步骤5:配置 Prometheus 抓取配置

编辑 prometheus.yml 文件,添加以下内容:

scrape_configs:- job_name: 'mysql'scrape_interval: 1mstatic_configs:- targets: ['<YOUR_MYSQL_SERVER_IP>:9104']

步骤6:重启 Prometheus 和 mysqld_exporter

重新加载 prometheus.yml 文件:

kill -HUP <prometheus_PID>

重新启动 mysqld_exporter:

ps aux | grep mysqld_exporter
kill -9 <mysqld_exporter_PID>
./mysqld_exporter --config.my-cnf=my.cnf &

步骤7:可视化监控数据

使用 Grafana 等数据可视化工具,将 MySQL 的监控指标展示分析出来。例如,可以展示 CPU 使用率、磁盘 I/O 速度、网络连接数、线程数等各种监控数据。

三、prometheus 中 Mysql 采用监控指标

指标名prometheus 指标mysql 获取指标方式
当前连接数mysql_global_status_threads_connected登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘threads_connected’;
最大连接数mysql_global_variables_max_connections登录 mysql,执行 SQL \n SHOW VARIABLES LIKE ‘max_connections’;
MYSQL 缓存命中率mysql_global_status_qcache_hits 和 mysql_global_status_commands_total{command=~“select”}登录 mysql,执行 SQL \n SHOW GLOBAL STATUS \n LIKE ‘Qcache_hits’; \n SHOW GLOBAL STATUS LIKE ‘Com_select’;
InnoDB 缓存命中率mysql_global_status_innodb_buffer_pool_reads \n mysql_global_status_innodb_buffer_pool_read_requests登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘Innodb_buffer_pool_reads%’; \n SHOW GLOBAL STATUS LIKE ‘Innodb_buffer_pool_read_request%’;
MYISAM 缓存命中率mysql_global_status_key_reads 和 mysql_global_status_key_read_requests登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘Key_reads%’; \n SHOW GLOBAL STATUS LIKE ‘Key_read_requests%’;
CPU 使用率container_cpu_usage_seconds_total 和 kube_pod_container_resource_limitsk8s 集群 master 机器执行 \n kubectl top pod -n coding | grep mariadb-primary,核对 use 是否一致 \n kubectl get pod mariadb-primary-0 -n coding -o=jsonpath=‘{.spec.containers[*].resources.limits.memory}’ 核对 limit 是否一致
内存占用container_memory_usage_bytesk8s 集群 master 机器执行 \n kubectl top pod -n coding | grep mariadb-primary,核对 use 是否一致
内存利用率container_memory_usage_bytes 和 kube_pod_container_resource_limits{resource=“memory”,unit=“byte”}k8s 集群 master 机器执行 \n kubectl top pod -n coding | grep mariadb-primary,核对 use 是否一致 \n kubectl get pod mariadb-primary-0 -n coding -o=jsonpath=‘{.spec.containers[*].resources.limits.cpu}’ 核对 limit 是否一致
发送数据量mysql_global_status_bytes_sent登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘Bytes_received’;
接受数据量mysql_global_status_bytes_received登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘Bytes_sent’;
所有SQL语句,无论其是否执行成功mysql_global_status_queries登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘Queries’;
慢查询数mysql_global_status_slow_queries登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘Slow_queries’;
全表查询数mysql_global_status_select_scan登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘Select_scan’;
执行 DELETE 查询的次数mysql_global_status_commands_total{command=~“delete”}登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘Com_delete’;
执行 INSERT 查询的次数mysql_global_status_commands_total{command=~“insert”}登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘Com_insert’;
执行 UPDATE 查询的次数mysql_global_status_commands_total{command=~“update”}登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘Com_update’;
执行 REPLACE 查询的次数mysql_global_status_commands_total{command=~“replace”}登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘Com_replace’;
执行 SELECT 查询的次数mysql_global_status_commands_total{command=~“select”}登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘Com_select’;
现场运行数mysql_global_status_threads_running登录 mysql,执行 SQL \n SHOW STATUS LIKE ‘Threads_running’;
线程创建数mysql_global_status_threads_created登录 mysql,执行 SQL \n SHOW STATUS LIKE ‘Threads_created’;
InnoDB 磁盘写次数 \n 单位: 次/smysql_global_status_innodb_data_writes登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘innodb_data_writes’; \n 或者 \n SHOW ENGINE INNODB STATUS; 查看 OS file writes 一行
InnoDB 磁盘读次数 \n 单位: 次/smysql_global_status_innodb_data_reads登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘innodb_data_reads’; \n 或者 \n SHOW ENGINE INNODB STATUS; 查看 OS file reads 一行
InnoDB 磁盘 fsync 次数 \n 单位: 次/smysql_global_status_innodb_data_fsyncs登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘innodb_data_fsyncs’; \n 或者 \n SHOW ENGINE INNODB STATUS; 查看 OS fsyncs 一行
InnoDB 读取的数据量 \n 单位: 字节/smysql_global_status_innodb_data_reads登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘Innodb_data_reads’;
InnoDB 写入的数据量 \n 单位: 字节/smysql_global_status_innodb_data_writes登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘Innodb_data_written’;
InnoDB行删除量mysql_global_status_innodb_row_ops_total{operation=“deleted”}登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘Innodb_rows_deleted’;
InnoDB行插入量mysql_global_status_innodb_row_ops_total{operation=“inserted”}登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘Innodb_rows_inserted’;
InnoDB行读取量mysql_global_status_innodb_row_ops_total{operation=“read”}登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘Innodb_rows_read’;
InnoDB行更新量mysql_global_status_innodb_row_ops_total{operation=“updated”}登录 mysql,执行 SQL \n SHOW GLOBAL STATUS LIKE ‘Innodb_rows_updated’;
http://www.lryc.cn/news/36184.html

相关文章:

  • 刷题记录:牛客NC24261[USACO 2019 Feb G]Cow Land
  • MYSQL开发误区
  • k8s学习之路 | k8s 工作负载 DaemonSet
  • Javaweb MVC模式和三层架构
  • 综合考虑,在客户端程序中嵌入网页程序,首选CefSharp。
  • 【Java基础 下】 030 -- 网络编程
  • 2021牛客OI赛前集训营-提高组(第三场) T3打拳
  • C++面向对象编程之四:成员变量和成员函数分开存储、this指针、const修饰成员和对象
  • 卷积神经网络(CNN)基础知识
  • opencv+python 常见图像预处理
  • 如何实现一个单例模式
  • 传输线的物理基础(四):传输线的驱动和返回路径
  • Java多态性
  • 算法拾遗二十七之窗口最大值或最小值的更新结构
  • 【带你搞定第二、三、四层交换机】
  • C++基础了解-22-C++ 重载运算符和重载函数
  • BatchNormalization
  • vue 中安装插件实现 rem 适配
  • Hadoop学习
  • Golang反射源码分析
  • Qt之悬浮球菜单
  • 易优cms attribute 栏目属性列表
  • 表格中的table-layout属性讲解
  • 【MFA】windows环境下,使用Montreal-Forced-Aligner训练并对齐音频
  • C语言实验小项目实例源码大全订票信息管理系统贪吃蛇图书商品管理网络通信等
  • 电脑图片损坏是怎么回事
  • 【论文研读】无人机飞行模拟仿真平台设计
  • 【算法题】2379. 得到 K 个黑块的最少涂色次数
  • DJ1-3 计算机网络和因特网
  • Git学习笔记(六)-标签管理