最新文章:

首页 运维技术

zabbix之mysql表分区优化

发布时间:2018年11月20日 评论数:抢沙发 阅读数:4557

    随着主机越来越多,数据库中的历史数据越来越多了,出现了zabbix自带的housekeeper清理历史数据的时候,造成查数据慢并且触发了很多报警信息

    例如Zabbix housekeeper processes more than 75% busy,housekeeper默认一小时清理一次旧的数据。

    在server配置文件中这两行可以定义:
    HousekeepingFrequency=1   zabbix执行Housekeeping的频率,单位为hours
    MaxHousekeeperDelete=500  每次最多删除历史数据的行
    但是housekeeper清理过中,会导致数据库负载增高,从而影响读写性能。
    所以我们对几个历史数据表做分区表。按照时间(每天)为单位,把历史数据存到各个分区表中,这样做能加快查询速度、快速清理过去一时间的历史数据(删除分区表)

    先查看每个表所占容量和行数,可以看到history的表很大

    1.jpg

    下载分区脚本
    脚本默认详情数据保留30天,趋势数据保留12个月,如需修改,请修改以下内容:
    daily_history_min=30
    monthly_history_min=12
    脚本默认连接数据库信息,更改成zabbix的:
    DBHOST=localhost
    DBUSER=zabbix
    DBPASS=zabbix
    还需要更改zabbix的安装路径和zabbix服务名称,脚本模式zabbix-server,这里我的是zabbix_server
    2.jpg
    赋予脚本执行权限chmod +x 
    然后备份一下zbabix的数据库

    mysqldump -uroot -p zabbix>/root/zabbix_$(date +%F).sql

    停止zabbix服务

    service zabbix_server stop

    执行脚本./partitiontables_gt_zbx34.sh
    Ready to partition tables.
    Ready to update permissions of Zabbix user to create routines
    #mysql的root帐号和密码:
    Enter root DB user: root
    Enter root password: 123456
    #备份数据库的操作,已经手动备份:
    Do you want to backup the database (recommended) (Y/n): n
    Are you certain you have a backup (y/N): 
    y
    Ready to proceed:
    Starting yearly partioning at: 2018
    and ending at: 2018
    With 30 days of daily history
    Ready to proceed (Y/n): 
    y
    Altering table: history
    Altering table: history_log
    Altering table: history_str
    Altering table: history_text
    Altering table: history_uint
    Altering table: trends
    Altering table: trends_uint
    Creating monthly partitions for table: trends
    Creating monthly partitions for table: trends_uint
    Creating daily partitions for table: history
    Creating daily partitions for table: history_log
    Creating daily partitions for table: history_str
    Creating daily partitions for table: history_text
    Creating daily partitions for table: history_uint
    Ready to apply script to database, this may take a while.(Y/n): 
    y
    Altering tables
    history
    history_log
    history_str
    history_text
    history_uint
    trends
    trends_uint
    trends
    trends_uint
    history
    history_log
    history_str
    history_text
    history_uint
    Installing procedures
    If Zabbix Version = 2.0 
    Do you want to update the /etc/zabbix/zabbix_server.conf
    to disable housekeeping (Y/n): n
    Do you want to update the crontab (Y/n): y
    The crontab entry can be either in /etc/cron.daily, or added
    to the crontab for root
    Do you want to add this to the /etc/cron.daily directory (Y/n): y
    #设置清理报告通知邮箱:
    Enter email of who should get the daily housekeeping reports:xxxx@qq.com
    配置完成
    上面的脚本会在/etc/cron.daily目录下生成一个名称为zabbixhousekeeping的脚本:

    [root@localhost ~]# cat /etc/cron.daily/zabbixhousekeeping 

    #!/bin/bash

    /usr/local/zabbix/cron.d/housekeeping.sh


    上面的脚本会在/usr/local/zabbix/cron.d/目录下生成一个名称为housekeeping.sh脚本。

    [root@localhost ~]# cat /usr/local/zabbix/cron.d/housekeeping.sh 

    #!/bin/bash

    MAILTO=xxxx@qq.com

    tmpfile=/tmp/housekeeping$$date >$tmpfile

    /usr/bin/mysql --skip-column-names -B -h localhost -uzabbix -pzabbix zabbix -e "CALL create_zabbix_partitions();" >>$tmpfile 2>&1

    /usr/bin/mail -s "Zabbix MySql Partition Housekeeping" $MAILTO <$tmpfilerm -f $tmpfile

    这样的话,每天凌晨大概3点左右会执行,并且用mail程序发送执行结果到上面的你指定的邮箱。可以看到删除过期的数据分区表,并建立新的分区表。
    mail需要提前配置好,保证能正常发送邮件。

    执行以下SQL语句查看histroy表的分区情况:
    use zabbix;
    show create table history_log\G;
    3.jpg
    分区表成功,启动zabbix_server服务。

二维码加载中...
本文作者:Mr.linus      文章标题: zabbix之mysql表分区优化
本文地址:http://www.90qj.com/526.html  本文已经被百度收录,点击查看详情
版权声明:若无注明,本文皆为“挨踢 Blog”原创,转载请保留文章出处。
挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
提交评论

清空信息
关闭评论