mysql 刪除 zabbix歷史資料

2021-10-10 20:14:53 字數 2301 閱讀 9917

zabbix伺服器報警:zabbix housekeeper processes more than 75% busy

原因分析說明:

處理方法:

vim /etc/zabbix/zabbix_server.conf

housekeepingfrequency=12 #原值是每一小時清理一次,建議改為12或24小時清理一次。

maxhousekeeperdelete=100000 #原值為每次清理5000條,建議改為上限100000條。

此外,另一種原因是由於歷史資料占用過多導致,可通過檢視zabbix資料庫中各個表的大小

select table_schema, table_name , data_length /

1024

/1024

/1024

as data_size_gb , index_length /

1024

/1024

/1024

as index_size_gb ,

(data_length + index_length)

/1024

/1024

/1024

as table_size_gb , table_rows from information_schema.

tables

where table_schema =

'zabbix'

order

by table_size_gb asc

;

我們會發現 history_uint 表占用了34g 可以說是非常大了

解決方法:刪除資料

當然如果是生產環境,不可能直接truncate ,歷史資料還是對於排查問題還是有用的

網上的解決方案是:

mariadb [zabbix]

>

delete

from history_uint where clock <

1575167758

;error 1206

(hy000): the total number of locks exceeds the lock

table size

這樣刪除由於資料量特別大並且clock不是索引會特別慢,當然我執行的時候就報錯如上:

根據上面引用的部落格及對zabbix資料庫各表之間的關係稍加分析,我們可以通過索引itemid 入手進行刪除。這也是系統自動刪除歷史資料的方法,

我們會發現根據zabbix_server.conf 配置檔案中的預設規則:預設一小時刪除一次最早的5000條資料,

我們可以根據這條sql 稍加改進如下

delete

from history_uint where itemid=

46727

and clock <

1575167758

##刪除itemid =46727 並且時間早於2019-12-01 10:35:58之前的歷史資料

然後我們寫個shell 指令碼便於刪除所有item在指定時間之前的歷史資料

#!/bin/bash

date=

`date -d $(date -d "-90 day" +%y%m%d) +%s`

mysql -uzabbix -pxasxaxasx zabbix -e "select itemid from items ;"

> itemid

for itemid in

`cat itemid`

domysql -uzabbix -pxasxaxasx zabbix -e 'delete from history_uint where itemid='

$' and clock < '

$';'

mysql -uzabbix -pxasxaxasx zabbix -e 'delete from history where itemid='

$' and clock < '

$';'

done

zabbix清空歷史記錄

zabbix清空歷史記錄 由於zabbix每天收集大量記錄,清理過期的資料以提高查詢效能。mysql uroot p 輸入mysql密碼 use zabbix truncate table history optimize table history truncate table history s...

MYSQL安全之刪除歷史操作檔案

查詢 mysql 的data 檔案安裝在 進入 mysql 然後模糊搜尋 mysql show variables like data 模糊搜尋 mysql show variables like data dir 精確查詢 一般是在 home 目錄下的。home意思home目錄般 home 登陸使...

MySQL 用儲存過程刪除歷史資料

建立儲存過程 delimiter create procedure autodelete begin delete from amap log where date time date date sub now interval 30 day end delimiter 建立事件,定製每天執行aut...