採集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的源代碼裏面,有很多,大家感興趣的可以自己去看一下代碼。