随心一记

一二三四五,上山打老鼠


  • 首页

  • 归档

  • 标签
ywcsb

ywcsb

游戏可以不玩,小说不能不看。

153 日志
3 分类
42 标签
RSS
GitHub 知乎 随心一记
Links
  • 随心一记
  • 追梦人物的
  • MSDN

Zabbix MySQL分区表配置

发表于 2019-12-18 | 阅读 1464 | 分类于 系统运维 |

在使用zabbix的过程中,随着时间的推移,数据库中的历史数据会越来越多,会发现打开页面,查询数据等会变慢。zabbix 自带的 housekeeper会定时清理(默认一小时清理一次)旧的数据。不过在 housekeeper清理过中,会导致数据库负载极具增加。这里介绍另外一种办法,就是对几个历史数据表做分区表(partition table)可按照时间(每天)为维度,把历史数据存到各个分区表中,这样做有以下好处:

  1. 加快查询速度
  2. 快速清理过去一时间的历史数据(删除分区表)

配置

zabbix3.4分区脚本

脚本下载

下载脚本到并赋予可执行权限,默认脚本详情数据保留30天,趋势数据保留12个月,可根据实际情况修改。修改脚本以下地方即可

daily_history_min=30
monthly_history_min=12

脚本里默认的zabbix数据库主机为localhost,用户为zabbix,密码为zabbix,如果你的信息不是这个,可以修改脚本里的如下对应信息

DBHOST=localhost
DBUSER=zabbix
DBPASS=zabbix

修改好之后,赋予可执行权限。

分区

注意:执行分区表之前建议先停止zabbix server,如果不停止可能导致意外事件,备份好zabbix server数据库。因此建议安装好zabbix server之后就进行分区表。

./partitiontables_gt_zbx34.sh

完整执行过程如下

[root@localhost opt]# ./partitiontables_gt_zbx34.sh 
Ready to partition tables.

Ready to update permissions of Zabbix user to create routines

Enter root DB user: root         
Enter root password: root
Warning: Using a password on the command line interface can be insecure.

以上要输入数据库的root用户和root密码

Do you want to backup the database (recommended) (Y/n): n
Are you certain you have a backup (y/N): 
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
Warning: Using a password on the command line interface can be insecure.
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: 18910288715@163.com

如果无错误,表示分区表创建成功。

确认

查看/etc/cron.daily/目录下是否有zabbixhousekeeping脚本,内容为

#!/bin/bash
/usr/local/zabbix/cron.d/housekeeping.sh

查看housekeeping.sh脚本,内容如下

#!/bin/bash

MAILTO=18910288715@163.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 <$tmpfile
rm -f $tmpfile

每次分区大约在每晚凌晨3点左右执行,执行成功后,脚本会用mail程序把执行结果发送到上面的邮箱,如果不需要可以删除mail 一行即可。

邮件内容大概如图

image

即可看到删除过期的数据分区表,并建立新的分区表。

查看

如果要具体查看分区表的情况,可以进入zabbix数据库,执行以下SQL语句查看histroy表的分区情况

use zabbix;
select 
  partition_name part,  
  partition_expression expr,  
  partition_description descr,  
  table_rows  
from information_schema.partitions  where 
  table_schema = schema()  
  and table_name='history';

界面设置

采用表分区,需要关闭Housekeeping功能,关闭流程如下:Administration ---> General ---> Housekeepin image image

觉得不错,支持一下!
geerniya WeChat Pay

微信打赏

geerniya Alipay

支付宝打赏

# zabbix # MySQL
centos同步yum源到本地
Django 表结构修改

发表评论

共 0 条评论

    暂无评论
© 2018 - 2022 ywcsb
冀ICP备17022045号-1
Supported by 腾讯云