采集MySQL数据库表自增主键使用情况
- 2019 年 10 月 4 日
- 笔记
下面这个脚本是采集数据库自增主键水位的(遇到过业务主键设置过小,导致出问题的,因此我们必须将自增id的水位线监控起来)
IP_ADDR=$(/sbin/ip a|egrep '10.[0|1].[1-9]+.[1-9]+*' | awk '{print $2}'| cut -d '/' -f 1); mysql -uroot -p123456 2>/dev/null -e "set @ip='$IP_ADDR';SELECT @ip AS IP_ADDR, curdate() AS INSERT_DATE , table_schema, table_name, column_name, AUTO_INCREMENT, POW(2, CASE data_type WHEN 'tinyint' THEN 7 WHEN 'smallint' THEN 15 WHEN 'mediumint' THEN 23 WHEN 'int' THEN 31 WHEN 'bigint' THEN 63 END+(column_type LIKE '% unsigned'))-1 AS max_int FROM information_schema.tables t JOIN information_schema.columns c USING (table_schema,table_name) WHERE c.extra = 'auto_increment' AND t.TABLE_SCHEMA NOT IN ('information_schema','mysql', 'sys','test','performance_schema') AND t.auto_increment IS NOT NULL ; "
关于MySQL的采集脚本, 在prometheus的mysql_exporter的源代码里面,有很多,大家感兴趣的可以自己去看一下代码。