Zabbix数据库表分区

Zabbix数据库表分区的具体步骤
zabbix的监控主机数量将近300,且运行了一年时间了,最近zabbix server服务监控历史数据等服务不断自身告警、查询性能也变得很低
关于历史数据的两个参数,在zabbix server的配置文件中
可以选择关闭housekeeper禁止自动定期清除历史记录数据,因为对于大数据的删除会直接影响zabbix的性能、或者调整相应参数
HousekeepingFrequency
取值范围:0-24
默认值:1
说明:housekeep执行频率,默认每小时回去删除一些过期数据。如果server重启,那么30分钟之后才执行一次,接下来,每隔一小时在执行一次。
MaxHousekeeperDelete
取值范围: 0-1000000
默认值:5000
housekeeping一次删除的数据不能大于MaxHousekeeperDelete

数据库优化
一、设置独立表空间(innodb_file_per_table=1)  # 5.6版本以上自动开启 以上版本跳过这一段
1、清空history数据
[url=][/url]
[root@Zabbix-Server ~]# mysql -u zabbix -pMariaDB [(none)]> use zabbix;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedMariaDB [zabbix]> MariaDB [zabbix]> truncate table history;Query OK, 0 rows affected (0.19 sec)MariaDB [zabbix]> optimize table history;+----------------+----------+----------+-------------------------------------------------------------------+| Table          | Op       | Msg_type | Msg_text                                                          |+----------------+----------+----------+-------------------------------------------------------------------+| zabbix.history | optimize | note     | Table does not support optimize, doing recreate + analyze instead || zabbix.history | optimize | status   | OK                                                                |+----------------+----------+----------+-------------------------------------------------------------------+2 rows in set (0.81 sec)MariaDB [zabbix]> truncate table history_str;Query OK, 0 rows affected (0.05 sec)               MariaDB [zabbix]>  truncate table history_uint;Query OK, 0 rows affected (6.32 sec)[url=][/url]

2、修改表结构
[url=][/url]
MariaDB [(none)]> use zabbix;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedMariaDB [zabbix]> Alter table history_text drop primary key, add index (id), drop index history_text_2, add index history_text_2 (itemid, id);Query OK, 0 rows affected (1.11 sec)               Records: 0  Duplicates: 0  Warnings: 0MariaDB [zabbix]> Alter table history_log drop primary key, add index (id), drop index history_log_2, add index history_log_2 (itemid, id);Query OK, 0 rows affected (0.14 sec)               Records: 0  Duplicates: 0  Warnings: 0[url=]
[/url]

修改完之后再按照官网上的过程创建四个存储过程:
3、将官方的四个分散代码拷贝至一个文件保存为sql,导入数据库;
 View Code

[url=][/url]
[root@Zabbix-Server ~]# mysql -u zabbix -p zabbixEnter password: Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -AWelcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection id is 48790Server version: 5.5.52-MariaDB MariaDB ServerCopyright (c) 20002016, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [zabbix]> use zabbix;Database changedMariaDB [zabbix]> source /root/zabbix-partition.sql;Query OK, 0 rows affected (0.04 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)MariaDB [zabbix]> CALL partition_maintenance('zabbix''history_log'282414);+---------------------------------------------------------------+| msg                                                           |+---------------------------------------------------------------+| partition_create(zabbix,history_log,p201801100000,1515600000) |+---------------------------------------------------------------+1 row in set (0.18 sec)+---------------------------------------------------------------+| msg                                                           |+---------------------------------------------------------------+| partition_create(zabbix,history_log,p201801110000,1515686400) |+---------------------------------------------------------------+1 row in set (0.48 sec)+---------------------------------------------------------------+| msg                                                           |+---------------------------------------------------------------+| partition_create(zabbix,history_log,p201801120000,1515772800) |+---------------------------------------------------------------+1 row in set (0.67 sec)+---------------------------------------------------------------+| msg                                                           |+---------------------------------------------------------------+| partition_create(zabbix,history_log,p201801130000,1515859200) |+---------------------------------------------------------------+1 row in set (1.02 sec)+---------------------------------------------------------------+| msg                                                           |+---------------------------------------------------------------+| partition_create(zabbix,history_log,p201801140000,1515945600) |+---------------------------------------------------------------+1 row in set (1.22 sec)+---------------------------------------------------------------+| msg                                                           |+---------------------------------------------------------------+| partition_create(zabbix,history_log,p201801150000,1516032000) |+---------------------------------------------------------------+1 row in set (1.44 sec)+---------------------------------------------------------------+| msg                                                           |+---------------------------------------------------------------+| partition_create(zabbix,history_log,p201801160000,1516118400) |+---------------------------------------------------------------+1 row in set (1.64 sec)+---------------------------------------------------------------+| msg                                                           |+---------------------------------------------------------------+| partition_create(zabbix,history_log,p201801170000,1516204800) |+---------------------------------------------------------------+1 row in set (1.85 sec)+---------------------------------------------------------------+| msg                                                           |+---------------------------------------------------------------+| partition_create(zabbix,history_log,p201801180000,1516291200) |+---------------------------------------------------------------+1 row in set (2.04 sec)+---------------------------------------------------------------+| msg                                                           |+---------------------------------------------------------------+| partition_create(zabbix,history_log,p201801190000,1516377600) |+---------------------------------------------------------------+1 row in set (2.23 sec)+---------------------------------------------------------------+| msg                                                           |+---------------------------------------------------------------+| partition_create(zabbix,history_log,p201801200000,1516464000) |+---------------------------------------------------------------+1 row in set (2.42 sec)+---------------------------------------------------------------+| msg                                                           |+---------------------------------------------------------------+| partition_create(zabbix,history_log,p201801210000,1516550400) |+---------------------------------------------------------------+1 row in set (2.62 sec)+---------------------------------------------------------------+| msg                                                           |+---------------------------------------------------------------+| partition_create(zabbix,history_log,p201801220000,1516636800) |+---------------------------------------------------------------+1 row in set (2.85 sec)+--------------------+--------------------+| table              | partitions_deleted |+--------------------+--------------------+| zabbix.history_log | N/A                |+--------------------+--------------------+1 row in set (3.10 sec)Query OK, 0 rows affected, 1 warning (3.10 sec)[url=][/url]

4、对想要分区的表进行表分区
[url=][/url]
DELIMITER $$CREATE PROCEDURE `partition_maintenance_all`(SCHEMA_NAME VARCHAR(32))BEGIN                CALL partition_maintenance(SCHEMA_NAME, 'history'72414);                CALL partition_maintenance(SCHEMA_NAME, 'history_log'72414);                CALL partition_maintenance(SCHEMA_NAME, 'history_str'72414);                CALL partition_maintenance(SCHEMA_NAME, 'history_text'72414);                CALL partition_maintenance(SCHEMA_NAME, 'history_uint'72414);                CALL partition_maintenance(SCHEMA_NAME, 'trends'3652414);                CALL partition_maintenance(SCHEMA_NAME, 'trends_uint'3652414);END$$DELIMITER ;
以上代码部分的含义为(库名,表名,保存多少天的数据,每隔多久生成一个分区,本次生成多少分区)
[url=][/url]

mysql> source /root/partition_maintenance_all.sql;Query OK, 0 rows affected (0.00 sec)mysql> CALL partition_maintenance_all('zabbix');
5、Housekeeper 设置
Zabbix用户界面中的 "Administration" -> "部分提供了所有选项。确保在右上角的下拉列表中选择"Housekeeping" 您应该看到类似于以下的屏幕:31Su6O4i62d5065d652d6.png
  • 确保“历史”和“趋势”两个选项的“启用内部管理”复选框未被选中。
  • 确保历史和趋势的检查标题为“覆盖项目<趋势/历史>期间”复选框。
  • 将历史和趋势的“数据存储期限(天数)”框设置为您保留两者的时间。在上面给出的表分区中,正确的值是7和365。
6、加入计划任务
不要让数据库用完你的分区,上面示例是如何创建14天额外分区的,在第15天,数据库将无法粘贴历史/趋势数据,因此会发生数据丢失。
所以每隔一段时间(通过cron或其他方法)重新运行这些存储过程。通过这样做,分区将始终存在,可以插入数据。
#Q-2018-1/9
30 4 * * 1  /usr/bin/mysql -uzabbix -pzabbix -e "use zabbix;" -e "CALL partition_maintenance_all('zabbix');"

实际在生产环境中上述操作运行一段时间后,Zabbix server的log文件会报如下错误,events表主键重复、主键不能自动增长、导致zabbix不能告警
  2581:20180208:213930.461 [Z3005] query failed: [1062] Duplicate entry '8703' for key 'PRIMARY' [insert into events (eventid,source,object,objectid,clock,ns,value) values (8703,0,0,19518,1518097170,457297996,1);
可以使用如下命令删除events记录
[root@Zabbix-Server zabbix]# mysql -u zabbix -pzabbix  -e "use zabbix;"  -e 'delete from events';
如果想要删除表的所有数据,truncate语句要比 delete 语句快。
因为 truncate 删除了表,然后根据表结构重新建立它,而 delete 删除的是记录,并没有尝试去修改表。
不过truncate命令虽然快,却不像delete命令那样对事务处理是安全的。

另外注意的是mysql数据库清空表默认是不回收空间的(对应步骤1)
回收表空间的命令
optimize table historyoptimize table history_uint
针对MySQL的不同数据库存储引擎,在optimize使用清除碎片,回收闲置的数据库空间,把分散存储(fragmented)的数据和索引重新挪到一起(defragmentation),对I/O速度有好处。
当然optimize在对表进行操作的时候,会加锁,所以不宜经常在程序中调用。

0 条评论

请先 登录 后评论
乐维君
乐维君

351 篇文章

作家榜 »

  1. 乐维君 351 文章
  2. YOHOHO 14 文章
  3. 机灵小和尚 13 文章
  4. 我是一只小菜鸡 12 文章
  5. 细雨闲花 11 文章
  6. 御前侍卫张五哥 9 文章
  7. 。。。 8 文章
  8. 小黄人 8 文章