3 月 012014
 

 



日志服务器数据库 大表的清理

在运行了一段时间后,日志数据库的数据越来越大,主要有zabbix 和 loganalyzer 的
+——–+————————+———+———+
| 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 |         |

+——–+————————+———+———+
主要清理这三个表的数据
Syslog 表
mysql> delete  from SystemEvents where ReceivedAt like “2013%”;
zabbix  表
 
Zabbix数据清理zabbix收集的信息非常多,导致运行一段时间后,做数据备份或者迁移的时候,非常的慢,数据库占用空间也很大,可以先做一部分清理工作,主要涉及到下面的表:

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;

复制代码

清楚10天前的数据.#!/bin/bash

#当前时间

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;

一般执行的代码
date +%s -d “Jan 31, 2012 00:00:01”
复制代码2、然后mysql执行mysql> DELETE FROM `history_uint` WHERE `clock` < 1327939201;

mysql> optimize table history_uint;

 

 Posted by at 下午 3:50

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny