Prometheus MySQL 性能监控
创始人
2024-05-31 14:44:27
0

一、 介绍

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: [':9104']

步骤6:重启 Prometheus 和 mysqld_exporter

重新加载 prometheus.yml 文件:

kill -HUP 

重新启动 mysqld_exporter:

ps aux | grep mysqld_exporter
kill -9 
./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’;

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...