7

Prometheus监控之mysqld_exporter

 1 year ago
source link: https://blog.51cto.com/u_13236892/5890513
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Prometheus监控之mysqld_exporter

精选 原创
用于 MySQL 服务器指标的 Prometheus 导出器。
支持的版本:
MySQL >= 5.6。
MariaDB >= 10.3
注意:并非所有收集方法都在 MySQL/MariaDB < 5.6 上受支持

版本 > 0.10.0 的标志格式示例:
--collect.auto_increment.columns
--no-collect.auto_increment.columns

版本 <= 0.10.0 的标志格式示例:
-collect.auto_increment.columns
-collect.auto_increment.columns=[true|false]
1、创建数据库授权用户
CREATE USER 'mysqld_exporter'@'localhost' IDENTIFIED BY '123456' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysqld_exporter'@'localhost';
flush privileges;
#如果mysqld_exporter和mysql不一台机器,建议将localhost改为%
注意:建议为用户设置最大连接限制,以避免服务器在重负载下因监控抓取而超载。并非所有 MySQL/MariaDB 版本都支持此功能;例如,
MariaDB 10.1(随 Ubuntu 18.04 提供)不支持此功能。

2、下载mysqld_exporter
https://prometheus.io/download/
tar -zxvf mysqld_exporter-0.14.0.linux-amd64.tar.gz
cp -r mysqld_exporter-0.14.0.linux-amd64 /usr/local/mysqld_exporter

3、配置数据库认证
cd /usr/local/mysqld_exporter/
vi .mysqld_exporter.cnf
[client]
user=mysqld_exporter
password=123456
host=192.168.10.131
port=3306

4、启动mysqld_exporter
./mysqld_exporter --config.my-cnf=".mysqld_exporter.cnf" --web.listen-address=192.168.10.131:9104 &>mysqld_exporter.log &

5、创建系统服务启动配置文件mysqld_exporter.service
[root@localhost ~]# vi /usr/lib/systemd/system/mysqld_exporter.service
[Unit]
Description=Prometheus MySQL daemon
After=network.target

[Service]
User=root
Group=root
Type=simple
Restart=always
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter \
--config.my-cnf=/usr/local/mysqld_exporter/.mysqld_exporter.cnf \
--collect.global_status \
--collect.auto_increment.columns \
--collect.info_schema.processlist \
--collect.binlog_size \
--collect.info_schema.tablestats \
--collect.global_variables \
--collect.info_schema.innodb_metrics \
--collect.info_schema.query_response_time \
--collect.info_schema.userstats \
--collect.info_schema.tables \
--collect.perf_schema.tablelocks \
--collect.perf_schema.file_events \
--collect.perf_schema.eventswaits \
--collect.perf_schema.indexiowaits \
--collect.perf_schema.tableiowaits \
--collect.slave_status \
--web.listen-address=192.168.10.131:9104

[Install]
WantedBy=multi-user.target

systemctl daemon-reload
systemctl start mysqld_exporter
systemctl enable mysqld_exporter
systemctl status mysqld_exporter

6、与prometheus集成
vi /usr/local/prometheus/prometheus.yml
- job_name: 'mysqld_exporter'
scrape_interval: 10s
static_configs:
- targets: ['192.168.10.131:9104']
lables:
group: 'mysqld'
#添加一个job
systemctl restart prometheus

7、web测试
访问:http://IP:9090
7.1、查询吞吐量
监控任何系统时,我们的主要关注点是确保系统工作有效完成,数据库运行时完成大量的查询操作,所有监控优先级应该确保MySQL按照预期
执行查询。MySQL有一个名为Questions的内部计数器,MySQL术语为“服务器状态变量”。对于客户端应用程序发送的所有语句,该计数器都
是递增的。
show global status like "Questions";
数学命令行:mysql_global_status_questions

7.2、查询执行性能
关于查询执行性能表现方面,可以使用MySQL提供的Slow_queries计数器,每当查询的执行时间超过long_query_time参数指定的秒数时,
计数器就会增加。默认阀值为10秒。
show global status like "Slow_queries";
数学命令行:mysql_global_status_slow_queries

7.3、连接情况
为了防止MySQL服务器的过载运行,数据库管理员需要根据业务进行预评估,以便现在客户端连接MySQL的数量。可以在my.cnf文件中配置最
大连接数max_connections=512。
show variables like "max_connections";
数学命令行:mysql_global_variables_max_connections

7.4、缓存池使用情况
当MySQL默认的存储引擎是InnoDB时,会使用缓存池来缓存表和索引的数据。可以在my.cnf中配置innodb_buffer_pool_size=128M。这是
InnoDB最重要的参数,主要作用是缓存innodb表和索引、数据和插入数据,默认值为128M。
show global status like "Innodb_buffer_pool_reads";
数学命令行:mysql_global_status_innodb_buffer_pool_reads

三、配置’多个数据源

为了避免在 URL 中放入用户名和密码等敏感信息,您可以在config.my-cnf文件中有多个配置,
并通过添加&auth_module=<section>到请求中来匹配它。
多个配置的示例配置文件
[client]
user = foo
password = foo123
[client.servers]
user = bar
password = bar123

- job_name: mysql # To get metrics about the mysql exporter’s targets
params:
# Not required. Will match value to child in config file. Default value is `client`.
auth_module: client.servers
static_configs:
- targets:
# All mysql hostnames to monitor.
- server1:3306
- server2:3306
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
# The mysqld_exporter host:port
replacement: localhost:9104

四、mysql安全

MySQL Exporter 是支持 TLS 和 Basic Authentication 的。

在使用 TLS 和 Basic Authentication 的时候,需要通过 Web 配置文件来传递参数,使用 --web.config.file 参数来指定 Web 配置文件

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK