zabbix 由于历史数据过大, 因此导致磁盘空间暴涨,  下面是结局方法步骤


1. 停止 ZABBIX SERER 操作


[plain] view plaincopyprint?清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间_02

  1. [root@gd02-qa-plxt2-nodomain-web-95 ~]# killall zabbix_server  

  2. [root@gd02-qa-plxt2-nodomain-web-95 ~]# lsof -i:10051  




2. 停止 mysql 操作


[plain] view plaincopyprint?清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间_02

  1. [root@gd02-qa-plxt2-nodomain-web-96 dbdat]# mysqladmin -u root -p -h 127.0.0.1 shutdown  




3. 修改 my.cnf

添加 skip-new 参数, 目标可用缩减 innodb 磁盘空间


4. 重启启动 mysql


[plain] view plaincopyprint?清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间_02

  1. /apps/svr/mysql/bin/mysqld_safe --defaults-file=/apps/conf/mysql5.6/my.cnf --ledir=/apps/svr/mysql/bin  --basedir=/apps/svr/mysql/  --datadir=/apps/dbdat/mysql5_data --user=apps &  

  2.   

  3. [root@gd02-qa-plxt2-nodomain-web-96 apps]# lsof -i:3306  

  4. COMMAND   PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME  

  5. mysqld  25527 apps   11u  IPv4 29371110      0t0  TCP *:mysql (LISTEN)  




5. 分析 history 表


[plain] view plaincopyprint?清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间_02

  1. mysql> desc history;  

  2. +--------+---------------------+------+-----+---------+-------+  

  3. | Field  | Type                | Null | Key | Default | Extra |  

  4. +--------+---------------------+------+-----+---------+-------+  

  5. | itemid | bigint(20) unsigned | NO   | MUL | NULL    |       |  

  6. | clock  | int(11)             | NO   |     | 0       |       |  

  7. | value  | double(16,4)        | NO   |     | 0.0000  |       |  

  8. | ns     | int(11)             | NO   |     | 0       |       |  

  9. +--------+---------------------+------+-----+---------+-------+  

  10. 4 rows in set (0.00 sec)  

  11.   

  12. mysql> select max(itemid) from history;  

  13. +-------------+  

  14. | max(itemid) |  

  15. +-------------+  

  16. |       46582 |  

  17. +-------------+  

  18. 1 row in set (0.00 sec)  

  19.   

  20. mysql> select * from history where itemid=46582 limit 1, 20;  

  21. +--------+------------+--------+-----------+  

  22. | itemid | clock      | value  | ns        |  

  23. +--------+------------+--------+-----------+  

  24. |  46582 | 1396361332 | 0.0000 |  81875000 |  

  25. |  46582 | 1396361362 | 0.0000 | 768297000 |  

  26. |  46582 | 1396361392 | 0.0000 | 656787000 |  

  27. |  46582 | 1396361422 | 0.0000 | 665169000 |  

  28. |  46582 | 1396361452 | 0.0000 | 973570000 |  

  29. |  46582 | 1396361482 | 0.0000 | 625619000 |  

  30. |  46582 | 1396361512 | 0.0000 | 292743000 |  

  31. |  46582 | 1396361543 | 0.0000 |    340000 |  

  32. |  46582 | 1396361572 | 0.0000 |  15651000 |  

  33. |  46582 | 1396361602 | 0.0000 | 153264000 |  

  34. |  46582 | 1396361632 | 0.0000 |  79316000 |  

  35. |  46582 | 1396361662 | 0.0000 | 308107000 |  

  36. |  46582 | 1396361692 | 0.0000 | 237146000 |  

  37. |  46582 | 1396361722 | 0.0000 | 108810000 |  

  38. |  46582 | 1396361752 | 0.0000 | 419398000 |  

  39. |  46582 | 1396361782 | 0.0000 | 284113000 |  

  40. |  46582 | 1396361812 | 0.0000 | 254230000 |  

  41. |  46582 | 1396361842 | 0.0000 | 145938000 |  

  42. |  46582 | 1396361872 | 0.0000 | 403163000 |  

  43. |  46582 | 1396361902 | 0.3300 | 193302000 |  

  44. +--------+------------+--------+-----------+  

  45. 20 rows in set (0.01 sec)  




6. 删除两周前数据方法

取得时间戳, 时间只保留至 2014 3 25 日


[plain] view plaincopyprint?清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间_02

  1. [root@gd02-zabbix-db-research api]# date +%s -d "Mar 25, 2014 00:00:00"   

  2. 1395676800  




删除 history, history_unit 表方法


[plain] view plaincopyprint?清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间_02

  1. mysql> delete from history where clock < 1395676800;  

  2. Query OK, 8961912 rows affected (17 min 22.06 sec)  

  3.   

  4. mysql> delete from history_uint where clock < 1395676800;  

  5. Query OK, 7789494 rows affected (21 min 38.02 sec)  




尝试对表进行缩减发生故障


[plain] view plaincopyprint?清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间_02

  1. mysql> optimize table history_uint;  

  2. ERROR 1114 (HY000): The table '#sql-63b7_2d' is full  

  3. mysql> quit  

  4. Bye  

  5. [root@gd02-qa-plxt2-nodomain-web-96 mysql5_data]# df -h  

  6. 文件系统              容量  已用 可用 已用% 挂载点  

  7. /dev/vda1              20G   18G  1.3G  94% /  

  8. tmpfs                 2.0G     0  2.0G   0% /dev/shm  




故障原因, 当前 / 下磁盘空间不够.

临时删除 swapfile


[plain] view plaincopyprint?清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间_02

  1. [root@gd02-qa-plxt2-nodomain-web-96 /]# swapoff -a  

  2. [root@gd02-qa-plxt2-nodomain-web-96 /]# rm -rf swapfile  

  3. [root@gd02-qa-plxt2-nodomain-web-96 /]# df -h  

  4. 文件系统              容量  已用 可用 已用% 挂载点  

  5. /dev/vda1              20G   15G  3.7G  81% /  

  6. tmpfs                 2.0G     0  2.0G   0% /dev/shm  




原理说明, mysql 执行 optimize 过程中, 生成了临时表见下面文件, mysql 直接吧 history_unit 表复制成临时表再重新改名, 实现空间缩减.


[plain] view plaincopyprint?清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间_02

  1. [root@gd02-qa-plxt2-nodomain-web-96 zabbix]# ls *63b7_2e* -lh  

  2. -rw-rw---- 1 apps apps 8.5K 04-02 12:04 #sql-63b7_2e.frm  

  3. -rw-rw---- 1 apps apps 1.1G 04-02 12:10 #sql-63b7_2e.ibd  




再次缩减


[plain] view plaincopyprint?清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间_02

  1. mysql> optimize table history_uint;  

  2. Query OK, 40496963 rows affected (20 min 0.04 sec)  

  3. Records: 40496963  Duplicates: 0  Warnings: 0  

  4.   

  5. mysql> optimize table history;  

  6. Query OK, 45998084 rows affected (21 min 54.99 sec)  

  7. Records: 45998084  Duplicates: 0  Warnings: 0  





7. 缩减前后文件大小比较
缩减前


[plain] view plaincopyprint?清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间_02

  1. [root@gd02-qa-plxt2-nodomain-web-96 mysql5_data]# find -size +50M -exec ls -lh {} \;  

  2. -rw-rw---- 1 apps apps 80M 04-02 10:29 ./zabbix/events.ibd  

  3. -rw-rw---- 1 apps apps 5.1G 04-02 11:44 ./zabbix/history.ibd  

  4. -rw-rw---- 1 apps apps 152M 04-02 10:29 ./zabbix/trends.ibd  

  5. -rw-rw---- 1 apps apps 4.3G 04-02 11:47 ./zabbix/history_uint.ibd  

  6. -rw-rw---- 1 apps apps 328M 04-02 10:29 ./zabbix/trends_uint.ibd  

  7. -rw-rw---- 1 apps apps 1.1G 04-02 11:47 ./ibdata1  




缩减后


[plain] view plaincopyprint?清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间_02

  1. [root@gd02-qa-plxt2-nodomain-web-96 mysql5_data]# find -size +50M -exec ls -lh {} \;  

  2. -rw-rw---- 1 apps apps 80M 04-02 10:29 ./zabbix/events.ibd  

  3. -rw-rw---- 1 apps apps 3.6G 04-02 13:30 ./zabbix/history.ibd  

  4. -rw-rw---- 1 apps apps 152M 04-02 10:29 ./zabbix/trends.ibd  

  5. -rw-rw---- 1 apps apps 3.2G 04-02 12:24 ./zabbix/history_uint.ibd  

  6. -rw-rw---- 1 apps apps 328M 04-02 10:29 ./zabbix/trends_uint.ibd  

  7. -rw-rw---- 1 apps apps 1.1G 04-02 13:30 ./ibdata1  



8. 重启启动 zabbix,  php, nginx, mysql

新问题出现:

当前 zabbix 进行初始化, 会对 mysql 进行大量数据 r/w 操作

因此可能会发生下面警报, 经过 5 分钟后初始化, 下面报警会自动消除, 不用担心.

[plain] view plaincopyprint?清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间清理 zabbix 历史数据, 缩减 mysql 空间_ 缩减 mysql 空间_02

  1. Disk I/O is overloaded on gd02-qa-plxt2-nodomain-web-96.vclound.com  

  2.   

  3. Zabbix history syncer processes more than 75% busy  

  4.   

  5. Zabbix timer processes more than 75% busy