48小时系统班试听入口

【运维技术详解】使用Prometheus监控MySQL

发布作者:新盟教育 发布日期:2022-12-23 浏览人数:1479人

运维技术详解


资源放送 

↓ 扫一扫 

  • 领取Prometheus监控录屏资料

  • 网页同步文章.png



一、背景

MySQL Exporter是社区专门为采集MySQL/MariaDB数据库监控指标而设计开发,通过Exporter上报核心的数据库指标,用于异常报警和监控大盘展示,云监控Prometheus提供了与MySQL Exporter集成及开箱即用的Grafana监控大盘。

目前,Exporter支持高于5.6版本的MySQL和高于10.1版本的MariaDB。在 MySQL/MariaDB低于5.6版本时,部分监控指标可能无法被采集。

本文将通过配置MySQL Exporter,演示如何将云数据库MySQL的数据采集上报至云原生监控中和使用Grafana可视化数据。能够很好地查看数据库性能,并在遇到问题时知道在何处检查。

本文档将有三个主要步骤:

  • 数据库授权;

  • helm Chart部署MySQL Prometheus exporter;

  • 创建MySQL Grafana仪表板。


实验环境准备如下:

  • Kubernetes集群(Tencent TKE);

  • Prometheus监控(Tencent 云原生监控);

  • MySQL(Tencent CDB)。


二、数据库授权


因为MySQL Exporter是通过查询数据库中状态数据来对其进行监控,所以需要为对应的数据库实例进行授权。帐号和密码需根据实际情况而定,授权步骤如下:

① 登录云数据库MySQL控制台。

② 在实例列表页面单击需要授权的数据库名称,进入数据库详情页。

③ 选择【数据库管理】>【帐号管理】,进入帐号管理页面,根据业务实际需要创建监控建立的账号。

运维111.png


④ 单击帐号右侧操作项下的【修改权限】,修改对应权限。示例如下图所示:至此,数据库授权完成。

运维222.png


三、helm Charts部署Exporter


helm pull prometheus-community/prometheus-mysql-exporter
tar zxf prometheus-mysql-exporter-1.2.0.tgz
[prometheus-mysql-exporter] tree
.
├── Chart.yaml
├── README.md
├── templates
│ ├── NOTES.txt
│ ├── _helpers.tpl
│ ├── deployment.yaml
│ ├── secret-env.yaml
│ ├── secret.yaml
│ ├── service.yaml
│ └── servicemonitor.yaml
└── values.yaml
1 directory, 10 files


此处需要对该项目values.yaml进行修改:

[prometheus-mysql-exporter] helm install mysql-exporter .                                                                                                                              NAME: mysql-exporter
LAST DEPLOYED: Wed Oct 27 15:36:31 2021NAMESPACE: defaultSTATUS: deployedREVISION: 1
TEST SUITE: NoneNOTES:1. Get the application URL by running these commands:
export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=prometheus-mysql-exporter,app.kubernetes.io/instance=mysql-exporter" -o jsonpath="{.items[0].metadata.name}")
echo "Visit http://127.0.0.1:9104 to use your application"
kubectl --namespace default port-forward $POD_NAME 9104
[prometheus-mysql-exporter] helm ls                                                                                                                                                   
NAME           NAMESPACE       REVISION        UPDATED                                 STATUS          CHART                           APP VERSION
mysql-exporter default         1               2021-10-27 15:36:31.079294 +0800 CST    deployed        prometheus-mysql-exporter-1.2.0 v0.12.1
[~] kubectl logs mysql-exporter-prometheus-mysql-exporter-79ff48ffcc-5hzr5time="2021-10-27T07:36:36Z" level=info msg="Starting mysqld_exporter (version=0.12.1, branch=HEAD, revision=48667bf7c3b438b5e93b259f3d17b70a7c9aff96)" source="mysqld_exporter.go:257"time="2021-10-27T07:36:36Z" level=info msg="Build context (go=go1.12.7, user=root@0b3e56a7bc0a, date=20190729-12:35:58)" source="mysqld_exporter.go:258"time="2021-10-27T07:36:36Z" level=info msg="Enabled scrapers:" source="mysqld_exporter.go:269"time="2021-10-27T07:36:36Z" level=info msg=" --collect.global_variables" source="mysqld_exporter.go:273"time="2021-10-27T07:36:36Z" level=info msg=" --collect.slave_status" source="mysqld_exporter.go:273"time="2021-10-27T07:36:36Z" level=info msg=" --collect.global_status" source="mysqld_exporter.go:273"time="2021-10-27T07:36:36Z" level=info msg=" --collect.info_schema.query_response_time" source="mysqld_exporter.go:273"time="2021-10-27T07:36:36Z" level=info msg=" --collect.info_schema.innodb_cmp" source="mysqld_exporter.go:273"time="2021-10-27T07:36:36Z" level=info msg=" --collect.info_schema.innodb_cmpmem" source="mysqld_exporter.go:273"time="2021-10-27T07:36:36Z" level=info msg="Listening on :9104" source="mysqld_exporter.go:283"

查看指标:

[root@VM-1-27-centos ~]# curl 172.17.253.96:9104/metrics | grep mysql_up
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                              Dload  Upload   Total   Spent    Left  Speed100  142k    0  142k    0     0  6174k      0 --:--:-- --:--:-- --:--:-- 6499k# HELP mysql_up Whether the MySQL server is up.# TYPE mysql_up gauge
mysql_up 1


四、配置 Grafana视图


运维333.png






1671585dc74035d34ac554d5f2ae42fc.jpg


推荐阅读

>>>新手必备-Linux入门之云计算是什么

>>>红帽认证入门-Linux系统介绍及企业版本选型

>>>新手必备-Linux系统安装配置+Xshell远程连接

>>>Linux常用命令行合集之绝对路径和相对路径

>>>软连接与硬连接