Zabbix 數據清理
在服務器上操做數據庫是挺危險的事情,這裏必定要慎重操做,清理數據有兩種方法:
1. 更改zabbix 上歷史數據、趨勢保存時間
2. 經過對zabbxi mysql 數據庫進行操做清理
這裏筆者比較慫,妥妥使用的第二種,保留30d數據,還有就是在操做的時候必定要注意:關閉報警---->報警媒介,今天備份數據的時候形成誤報,短信大量發送,必定要記住慘痛經歷,否則就要走人啦!
使用表清理數據基本操做語句以下:
Zabbix 數據庫查詢大小
MariaDB [(none)]> select table_schema, concat(truncate(sum(data_length)/1024/1024,2),' mb') as data_size,concat(truncate(sum(index_length)/1024/1024,2),'mb') as index_size from information_schema.tables group by table_schema order by data_size desc;
+--------------------+-------------+------------+
| table_schema | data_size | index_size |
+--------------------+-------------+------------+
| zabbix | 63447.98 mb | 74299.04mb |
| information_schema | 0.08 mb | 0.08mb |
+--------------------+-------------+——————+
數據大小: 61G
索引大小: 37G
查詢Zabbix數據庫裏全部表大小
MariaDB [(none)]> select table_name, (data_length+index_length)/1024/1024 as total_mb,table_rows from information_schema.tables where table_schema='zabbix' order by total_mb desc;
執行sql查看指定日期以前的數據大小
SELECT table_schema as `Database`,table_name AS `Table`,round(((data_length + index_length)), 2) `Size in MB`FROM information_schema.TABLES where CREATE_TIME < '2019-09-01 00:00:00' and table_name='history.ibd';
查看當前目錄大與50M 的文件
# find ./ -size +50M | xargs ls -Slh
刪除2019年11月22日11點之前的數據
[root@128lastsummer ~]# date +%s -d "Nov 22, 2019 11:00:00"
1574391600
MariaDB [zabbix]> delete from history where clock < 1574391600;
MariaDB [zabbix]> delete from history_uint where clock < 1574391600;
MariaDB [zabbix]> optimize table history;
MariaDB [zabbix]> optimize table history_unit;
備份與恢復zabbix history 的表
[root@128lastsummer ~]# mysqldump -uzabbix -p zabbix history > /tmp/zabbix_history.sql
MariaDB [zabbix]> source /tmp/zabbix_history.sql;
查詢數據庫每一個表有多少條數據
select table_name,table_rows from information_schema.tables where TABLE_SCHEMA = '數據庫的名稱' order by table_rows desc;