日志服务器数据库 大表的清理
+——–+————————+———+———+
| DB | TABLE | TOTAL | COMMENT |
+——–+————————+———+———+
| zabbix | history_uint | 4307.61 | |
| zabbix | history | 1732.30 | |
| rizhi | varnish_67_05_Jan_2014 | 1450.83 | |
| rizhi | varnish_67_29_Dec_2013 | 1279.54 | |
| rizhi | varnish_67_04_Jan_2014 | 1272.82 | |
| rizhi | varnish_67_06_Jan_2014 | 1256.10 | |
| rizhi | varnish_67_28_Dec_2013 | 1178.28 | |
| rizhi | varnish_67_12_Jan_2014 | 1096.01 | |
| rizhi | varnish_67_01_Jan_2014 | 1087.88 | |
| Syslog | SystemEvents | 1077.43 | |
use zabbix;
truncate table history;
optimize table history;
truncate table history_str;
optimize table history_str;
truncate table history_uint;
optimize table history_uint;
truncate table trends;
optimize table trends;
truncate table trends_uint;
optimize table trends_uint;
truncate table events;
optimize table events;
当zabbix使用一段时间后,里面的数据库太大,想备份里面的数据库时,要浪费大量的时间,zabbix里面最大的表就是历史记录的表了,网上很多人都是写全部清空这些表的数据,其实我们可以按时间来删除里面的历史记录;
里面最大的表是 “history” 和 “history_uint”两个表;
zabbix里面的时间是用的时间戳方式记录,我们可以转换一下,然后根据时间戳来删除;
比如要删除2012年的1月31号以前的数据
1、先将标准时间转换为时间戳
# date +%s -d “Jan 31, 2012 00:00:01”
1327939201
复制代码2、然后mysql执行mysql> DELETE FROM `history_uint` WHERE `clock` < 1327939201;
mysql> optimize table history_uint;
复制代码
#当前时间
t_now=`date +%s`
#10天前的时间
t_ago=`echo 10*24*60*60+$t_now|bc`
mysql -uroot -pxxxxx -e ”
use zabbix;
DELETE FROM history WHERE ‘clock’ <$t_ago;
optimize table history;
DELETE FROM history_str WHERE ‘clock’ <$t_ago;
optimize table history_str;
DELETE FROM history_uint WHERE ‘clock’ <$t_ago;
optimize table history_uint;
DELETE FROM trends WHERE ‘clock’ <$t_ago;
optimize table trends;
DELETE FROM trends_uint WHERE ‘clock’ <$t_ago;
optimize table trends_uint;
DELETE FROM events WHERE ‘clock’ <$t_ago;
optimize table events;
”
mysql> optimize table history_uint;