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 文件,添加以下内容:

指标名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’;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值