背景
因爲zabbix歷史數據過大,致使佔用過多磁盤空間,需清理數據,釋放空間mysql
一、查看錶佔用空間狀況sql
SELECT table_name AS "Tables", round(((data_length + index_length) / 1024 / 1024), 2) "Size in MB" FROM information_schema.TABLES WHERE table_schema = 'zabbix' ORDER BY (data_length + index_length) DESC;
二、分析表數據
從上面較大的表看,主要集中history_unit,history兩張表,並且是存儲歷史數據,
a、查看history_uint和history數據表結構,能夠根據clock時間戳來進行數據刪除docker
MariaDB [zabbix]> desc history_uint; +--------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------------+------+-----+---------+-------+ | itemid | bigint(20) unsigned | NO | MUL | NULL | | | clock | int(11) | NO | | 0 | | | value | bigint(20) unsigned | NO | | 0 | | | ns | int(11) | NO | | 0 | | +--------+---------------------+------+-----+---------+-------+ 4 rows in set (0.26 sec) MariaDB [zabbix]> desc history; +--------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------------+------+-----+---------+-------+ | itemid | bigint(20) unsigned | NO | MUL | NULL | | | clock | int(11) | NO | | 0 | | | value | double(16,4) | NO | | 0.0000 | | | ns | int(11) | NO | | 0 | | +--------+---------------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) MariaDB [zabbix]>
三、清理表數據,建議中止zabbix server服務。
a、取30天前時間戳數據庫
[root@docker ~]# [root@docker ~]# date +%s -d "Jun 4, 2019 00:00:00" 1559577600 [root@docker ~]#
b、按照時間戳刪除數據,並優化bash
delete from history where clock < 1559577600; optimize table history
自動刪除歷史數據腳本:
#!/bin/bash
User="zabbix"
Passwd="zabbix"
Date=date -d $(date -d "-30 day" +%Y%m%d) +%s
#取30天以前的時間戳
$(which mysql) -u${User} -p${Passwd} -e "
use zabbix;
DELETE FROM history WHERE 'clock' < $Date;
optimize table history;
DELETE FROM history_uint WHERE 'clock' < $Date;
optimize table history_uint;ide