Prometheus监控mysql auto_increment值消耗情况,防止耗尽

如果你用过或了解过MySQL,那你一定知道自增主键了。每个自增id都是定义了初始值,然后按照指定步长增长(默认步长是1)。

虽然,自然数是没有上限的,但是我们在设计表结构的时候,通常都会指定字段长度,那么,这时候id就有上限了。

我们一般会将自增键的类型设置为int,数据范围为2的31次方, 即为负21亿到正21亿,对于一个频繁插入删除数据的表来说,21亿是可能会被用完的,可能引发业务无法正常写入。

因此在建表的时候你需要考察你的表是否有可能达到这个上限,如果有可能,就应该创建成  bigint unsigned,那将允许最大使用 2的64次方,即为18446744073709552000。

在这篇博文中,我们将看看如何使用Prometheus来监控auto_increment值使用情况,防止值耗尽,影响业务。

一、mysqld exporter

MySQL (https://github.com/percona/mysqld_exporter), 需要开启采集指标项  collect.auto_increment.columns ,export 参考如下

/opt/exporter/bin/mysqld_exporter_3306 --web.listen-address=0.0.0.0:51230 --config.my-cnf=/opt/exporter/conf/my3306.cnf --exporter.lock_wait_timeout=1 --collect.info_schema.processlist.processes_by_user --collect.info_schema.processlist.processes_by_host --collect.info_schema.tables.databases=* --collect.info_schema.tables --collect.info_schema.innodb_metrics --collect.global_status --collect.global_variables --collect.slave_status --collect.info_schema.processlist --collect.perf_schema.tablelocks --collect.binlog_size --collect.perf_schema.indexiowaits --collect.perf_schema.tableiowaits --collect.info_schema.clientstats --collect.perf_schema.replication_group_member_stats --collect.perf_schema.replication_applier_status_by_worker --collect.engine_innodb_status --collect.slave_hosts --collect.info_schema.query_response_time --collect.info_schema.tablestats --collect.auto_increment.columns