zabbix的数据库优化

走zabbix的1.6版本开始测试,1.8的版本开始线上使用,线上使用过1.9、2.0、2.2、3.0、4.0的版本,使用或是测试过zabbix1.6之后的所有版本。个人也有之前的SA转变为DBA,就zabbix的运维走数据库层面有一些自己的心得,希望对读者有所帮助。

1:MySQL版本推荐

MySQL5.7及以上版本,便捷的在线DDL方便zabbix的快速升级
链接数据库方式:zabbix的server、proxy、MySQL数据库尽量使用域名方式连接,方便进行故障切换。

2:zabbix数据库的授权

读写权限,用作zabbix自身访问:
grant all privileges on zabbix. to ‘zabbix‘@‘1.1.1.1‘ identified by ‘zabbix‘;
只读权限,用作二次开发只读zabbix数据库:
grant SELECT on zabbix.
to ‘zabbix_ro‘@‘1.1.1.1‘ identified by ‘zabbixro‘;

3:MySQL配置文件需要调整的几个重要参数

innodb_log_files_in_group = 16
innodb_log_file_size = 1G
innodb_file_per_table = 1
max_allowed_packet = 64M
back_log = 1024
max-connections = 2000
sync_binlog = 0
innodb_flush_log_at_trx_commit = 0
binlog_format = row
expire_logs_days = 3

4:巧用SQL语句运维zabbix

使用SQL语句更新监控项或是触发器,提升变更效率,减少对数据库影响:
/
update zabbix.items set delay=5 where key=‘icmpping‘;
update zabbix.items t,zabbix.functions f set f.parameter=‘30‘ where f.itemid=t.itemid and t.key
=‘agent.ping‘
/

5:巧用读写分离和负载均衡

在做二次开发时,为避免对线上监控server端的影响,尽量使用独立的数据库从库读取数据(可能有大量通过API读取历史数据库等只读操作),需要一个只读的api接口,此时需要做zabbix数据库读写分离,可以在从库上过滤掉几张表,打开从库的写权限,这几张表主要用作用户登录的审计、session的记录,否则可能登录失败,:

replicate-wild-ignore-table =zabbix.auditlog
replicate-wild-ignore-table =zabbix.sessions
replicate-wild-ignore-table =zabbix.ids

同时zabbix web_api做负载均衡的时候,请使用IP hash的方式,因为涉及到session的问题。

6:zabbix升级注意事项

做zabbix的升级,其实是做zabbix数据库库表的变更,可能包含表结构的表更、索引的变更、数据的重组等操作,此时需要做一套新的数据库集群作为线上集群的存库存在,等数据同步完成,断掉同步,使用新的zabbix server程序连接新的数据库集群完成升级,升级完成后切换线上zabbix server、数据库域名到新的server和新的数据库上,这样能最低限度影响正常的监控。

7:数据库备份和数据清理

备份主要信息即可,监控数据和审计日志、events不需要备份,数据量太大。
历史数据的删除:包含监控历史详情数据和趋势数据,使用分区的方式进行删除;另外events表也要定时的清理,使用主键进行清理,保留5天左右的即可,并需要定期的进行碎片整理,否则监控大屏显示会逐步变慢。

8:zabbix server历史数据和趋势数据分区

分区sql脚本下载地址:
https://www.zabbix.org/wiki/Docs/howto/mysql_partition
/ 为历史表分区做准备。zabbix2.0/3.0 only,3.2以上的版本不需要执行 /

ALTER TABLE history_text DROP PRIMARY KEY, ADD INDEX (id), DROP INDEX history_text_2, ADD INDEX history_text_2 (itemid, id);
ALTER TABLE history_log DROP PRIMARY KEY, ADD INDEX (id), DROP INDEX history_log_2, ADD INDEX history_log_2 (itemid, id);

/ 在server的配置文件中设置HousekeepingFrequency=0,禁用zabbix自带的清理历史数据任务 /

/设置历史监控数据表中的时间字段为索引,方便后续按照时间取值二次开发/

ALTER TABLE history ADD INDEX clock (clock);
ALTER TABLE history_log ADD INDEX clock (clock);
ALTER TABLE history_str ADD INDEX clock (clock);
ALTER TABLE history_str_sync ADD INDEX clock (clock);
ALTER TABLE history_sync ADD INDEX clock (clock);
ALTER TABLE history_text ADD INDEX clock (clock);
ALTER TABLE history_uint ADD INDEX clock (clock);
ALTER TABLE history_uint_sync ADD INDEX clock (clock);
ALTER TABLE trends ADD INDEX clock (clock);
ALTER TABLE trends_uint ADD INDEX clock (clock);
ALTER TABLE EVENTS ADD INDEX objectid (objectid,VALUE,clock);

/ 如果存在这五个存储过程则删除 /

DROP PROCEDURE IF EXISTS partition_create;
DROP PROCEDURE IF EXISTS partition_drop;
DROP PROCEDURE IF EXISTS partition_maintenance;
DROP PROCEDURE IF EXISTS partition_maintenance_all;
DROP PROCEDURE IF EXISTS partition_verify;

/第一次执行存储过程/

CALL partition_maintenance_all(‘zabbix‘);

/
inser to crontab 每天执行一次存储过程,生成新的分区和删除老的分区
#!/bin/sh
/xxxxx/mysql/bin/mysql -uzabbix -pzabbixmonitor --socket=/xxxx/mysql/tmp/mysql.sock --database=zabbix -e "CALL partition_maintenance_all(‘zabbix‘);"
/

存储过程详情如下:

/ partition_create /

DELIMITER $$
CREATE PROCEDURE partition_create(SCHEMANAME VARCHAR(64), TABLENAME VARCHAR(64), PARTITIONNAME VARCHAR(64), CLOCK INT)
BEGIN
/
SCHEMANAME = The DB schema in which to make changes
TABLENAME = The table with partitions to potentially delete
PARTITIONNAME = The name of the partition to create
/
/
Verify that the partition does not already exist
/

    DECLARE RETROWS INT;
    SELECT COUNT(1) INTO RETROWS
    FROM information_schema.partitions
    WHERE table_schema = SCHEMANAME AND TABLE_NAME = TABLENAME AND partition_description >= CLOCK;

    IF RETROWS = 0 THEN
            /*
               1. Print a message indicating that a partition was created.
               2. Create the SQL to create the partition.
               3. Execute the SQL from #2.
            */
            SELECT CONCAT( "partition_create(", SCHEMANAME, ",", TABLENAME, ",", PARTITIONNAME, ",", CLOCK, ")" ) AS msg;
            SET @SQL = CONCAT( ‘ALTER TABLE ‘, SCHEMANAME, ‘.‘, TABLENAME, ‘ ADD PARTITION (PARTITION ‘, PARTITIONNAME, ‘ VALUES LESS THAN (‘, CLOCK, ‘));‘ );
            PREPARE STMT FROM @SQL;
            EXECUTE STMT;
            DEALLOCATE PREPARE STMT;
    END IF;

END$$
DELIMITER ;

/ partition_drop /

DELIMITER $$
CREATE PROCEDURE partition_drop(SCHEMANAME VARCHAR(64), TABLENAME VARCHAR(64), DELETE_BELOW_PARTITION_DATE BIGINT)
BEGIN
/
SCHEMANAME = The DB schema in which to make changes
TABLENAME = The table with partitions to potentially delete
DELETE_BELOW_PARTITION_DATE = Delete any partitions with names that are dates older than this one (yyyy-mm-dd)
/
DECLARE done INT DEFAULT FALSE;
DECLARE drop_part_name VARCHAR(16);

    /*
       Get a list of all the partitions that are older than the date
       in DELETE_BELOW_PARTITION_DATE.  All partitions are prefixed with
       a "p", so use SUBSTRING TO get rid of that character.
    */
    DECLARE myCursor CURSOR FOR
            SELECT partition_name
            FROM information_schema.partitions
            WHERE table_schema = SCHEMANAME AND TABLE_NAME = TABLENAME AND CAST(SUBSTRING(partition_name FROM 2) AS UNSIGNED) < DELETE_BELOW_PARTITION_DATE;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    /*
       Create the basics for when we need to drop the partition.  Also, create
       @drop_partitions to hold a comma-delimited list of all partitions that
       should be deleted.
    */
    SET @alter_header = CONCAT("ALTER TABLE ", SCHEMANAME, ".", TABLENAME, " DROP PARTITION ");
    SET @drop_partitions = "";

    /*
       Start looping through all the partitions that are too old.
    */
    OPEN myCursor;
    read_loop: LOOP
            FETCH myCursor INTO drop_part_name;
            IF done THEN
                    LEAVE read_loop;
            END IF;
            SET @drop_partitions = IF(@drop_partitions = "", drop_part_name, CONCAT(@drop_partitions, ",", drop_part_name));
    END LOOP;
    IF @drop_partitions != "" THEN
            /*
               1. Build the SQL to drop all the necessary partitions.
               2. Run the SQL to drop the partitions.
               3. Print out the table partitions that were deleted.
            */
            SET @full_sql = CONCAT(@alter_header, @drop_partitions, ";");
            PREPARE STMT FROM @full_sql;
            EXECUTE STMT;
            DEALLOCATE PREPARE STMT;

            SELECT CONCAT(SCHEMANAME, ".", TABLENAME) AS `table`, @drop_partitions AS `partitions_deleted`;
    ELSE
            /*
               No partitions are being deleted, so print out "N/A" (Not applicable) to indicate
               that no changes were made.
            */
            SELECT CONCAT(SCHEMANAME, ".", TABLENAME) AS `table`, "N/A" AS `partitions_deleted`;
    END IF;

END$$
DELIMITER ;

/ partition_maintenance /
DELIMITER $$
CREATE PROCEDURE partition_maintenance(SCHEMA_NAME VARCHAR(32), TABLE_NAME VARCHAR(32), KEEP_DATA_DAYS INT, HOURLY_INTERVAL INT, CREATE_NEXT_INTERVALS INT)
BEGIN
DECLARE OLDER_THAN_PARTITION_DATE VARCHAR(16);
DECLARE PARTITION_NAME VARCHAR(16);
DECLARE LESS_THAN_TIMESTAMP INT;
DECLARE CUR_TIME INT;

    CALL partition_verify(SCHEMA_NAME, TABLE_NAME, HOURLY_INTERVAL);
    SET CUR_TIME = UNIX_TIMESTAMP(DATE_FORMAT(NOW(), ‘%Y-%m-%d 00:00:00‘));

    SET @__interval = 1;
    create_loop: LOOP
            IF @__interval > CREATE_NEXT_INTERVALS THEN
                    LEAVE create_loop;
            END IF;

            SET LESS_THAN_TIMESTAMP = CUR_TIME + (HOURLY_INTERVAL * @__interval * 3600);
            SET PARTITION_NAME = FROM_UNIXTIME(CUR_TIME + HOURLY_INTERVAL * (@__interval - 1) * 3600, ‘p%Y%m%d%H00‘);
            CALL partition_create(SCHEMA_NAME, TABLE_NAME, PARTITION_NAME, LESS_THAN_TIMESTAMP);
            SET @[email protected]__interval+1;
    END LOOP;

    SET OLDER_THAN_PARTITION_DATE=DATE_FORMAT(DATE_SUB(NOW(), INTERVAL KEEP_DATA_DAYS DAY), ‘%Y%m%d0000‘);
    CALL partition_drop(SCHEMA_NAME, TABLE_NAME, OLDER_THAN_PARTITION_DATE);

END$$
DELIMITER ;

/ partition_verify /
DELIMITER $$
CREATE PROCEDURE partition_verify(SCHEMANAME VARCHAR(64), TABLENAME VARCHAR(64), HOURLYINTERVAL INT(11))
BEGIN
DECLARE PARTITION_NAME VARCHAR(16);
DECLARE RETROWS INT(11);
DECLARE FUTURE_TIMESTAMP TIMESTAMP;

    /*
     * Check if any partitions exist for the given SCHEMANAME.TABLENAME.
     */
    SELECT COUNT(1) INTO RETROWS
    FROM information_schema.partitions
    WHERE table_schema = SCHEMANAME AND TABLE_NAME = TABLENAME AND partition_name IS NULL;

    /*
     * If partitions do not exist, go ahead and partition the table
     */
    IF RETROWS = 1 THEN
            /*
             * Take the current date at 00:00:00 and add HOURLYINTERVAL to it.  This is the timestamp below which we will store values.
             * We begin partitioning based on the beginning of a day.  This is because we don‘t want to generate a random partition
             * that won‘t necessarily fall in line with the desired partition naming (ie: if the hour interval is 24 hours, we could
             * end up creating a partition now named "p201403270600" when all other partitions will be like "p201403280000").
             */
            SET FUTURE_TIMESTAMP = TIMESTAMPADD(HOUR, HOURLYINTERVAL, CONCAT(CURDATE(), " ", ‘00:00:00‘));
            SET PARTITION_NAME = DATE_FORMAT(CURDATE(), ‘p%Y%m%d%H00‘);

            -- Create the partitioning query
            SET @__PARTITION_SQL = CONCAT("ALTER TABLE ", SCHEMANAME, ".", TABLENAME, " PARTITION BY RANGE(`clock`)");
            SET @__PARTITION_SQL = CONCAT(@__PARTITION_SQL, "(PARTITION ", PARTITION_NAME, " VALUES LESS THAN (", UNIX_TIMESTAMP(FUTURE_TIMESTAMP), "));");

            -- Run the partitioning query
            PREPARE STMT FROM @__PARTITION_SQL;
            EXECUTE STMT;
            DEALLOCATE PREPARE STMT;
    END IF;

END$$
DELIMITER ;

/ partition_maintenance_all /
DELIMITER $$
CREATE PROCEDURE partition_maintenance_all(SCHEMA_NAME VARCHAR(32))
BEGIN
CALL partition_maintenance(SCHEMA_NAME, ‘history‘, 28, 24, 14);
CALL partition_maintenance(SCHEMA_NAME, ‘history_log‘, 28, 24, 14);
CALL partition_maintenance(SCHEMA_NAME, ‘history_str‘, 28, 24, 14);
CALL partition_maintenance(SCHEMA_NAME, ‘history_text‘, 28, 24, 14);
CALL partition_maintenance(SCHEMA_NAME, ‘history_uint‘, 28, 24, 14);
CALL partition_maintenance(SCHEMA_NAME, ‘trends‘, 730, 24, 14);
CALL partition_maintenance(SCHEMA_NAME, ‘trends_uint‘, 730, 24, 14);
END$$
DELIMITER ;

原文地址:https://blog.51cto.com/wangwei007/2458114

时间: 2024-10-18 04:43:15

zabbix的数据库优化的相关文章

Zabbix配置文件的参数优化和Zabbix的数据库优化

Zabbix配置文件的参数优化 StartPollers=60 StartPollersUnreacheable=80 StartTrappers=20 StartPingers=100 StartDiscoverers=120 #zabbix提示进程繁忙时修改此参数,最大二百五,建议100左右,值大消耗cpu性能大 CacheSize=1024M StartDBSyncers=16 HistoryCacheSize=1024M TrendCacheSize=1024M HistoryTextC

zabbix数据库优化脚本

分享一个zabbix数据库的优化脚本,适合2.0版本. 对history,hostory_uint按日分区,trends,trends_uint按月分区: 关闭Houserkeeper: vim zabbix_server.conf DisableHousekeeper=1 对history,hostory_uint每日建立第二天的分区并删除45天前的分区 对trends,trends_uint每月20号建立下一个月的分区,并删除12个月前的分区 时间可以自己修改 由于events表的大小对仪表

zabbix切换数据库思路

zabbix切换数据库操作 1.安装mysql-server数据库 ,讲三个表结构复制过去 将schema.sql  images.sql   data.sql  数据库从zabbix服务器上复制到/root路径下面 然后导入表结构 [[email protected] ~]#  /usr/bin/mysql -uzabbix -pzabbix zabbix < /root/schema.sql Warning: Using a password on the command line inte

数据库优化

本文介绍一点,关于数据库优化方面的经验总结,希望对需要的人有所帮助! 优化请掌握以下几点(高手请补充): 1.表上的字段不要太多,能独立出去的尽量独立出去,虽然表上的字段多,写代码会比较方便, 但是运行效率就差了 2.将字符串的比较变成数字型比较每个系统都会有用户管理,其中必然有 昵称,密码,邮件等的字符串类型数据比较的问题.在数 据库操作中,字符串比较的效率是相当低下的.因此遇到字符串的比较,必须将其转换为数字型 比较.  具体做法是:在数据库表中增加相应的数字字段,比如 cNickname

从Linux系统磁盘空间不足引发的Zabbix服务器数据库迁移

之前一直没有去关心Zabbbix服务器存储空间问题,最近Zabbix报警提示/根目录磁盘空间不足,于是登录Zabbix看了一下,发现根目录只有1.3MB了,这怎么办了?第一个想到的是扩展根目录,结果发现不是用的LVM,采用的是标准的,心想也还可以救,就和Windows一样将D盘.E盘删除,再将D盘的空间扩展给C盘就OK啊,于是开始着手操作(注意提前备份虚拟机,最好做一个快照,出问题了好恢复) [[email protected] /]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /

单机数据库优化的一些实践(mysql)

数据库优化有很多可以讲,按照支撑的数据量来分可以分为两个阶段:单机数据库和分库分表,前者一般可以支撑500W或者10G以内的数据,超过这个值则需要考虑分库分表.另外,一般大企业面试往往会从单机数据库问起,一步一步问到分库分表,中间会穿插很多数据库优化的问题.本文试图描述单机数据库优化的一些实践,数据库基于mysql,如有不合理的地方,欢迎指正. 1.表结构优化 在开始做一个应用的时候,数据库的表结构设计往往会影响应用后期的性能,特别是用户量上来了以后的性能.因此,表结构优化是一个很重要的步骤.

MySQL数据库存储引擎与数据库优化

存储引擎 (1)MySQL可以将数据以不同的技术存储在文件(内存)中,这种技术就成为存储引擎. 每种存数引擎使用不同的存储机制.索引技巧.锁定水平,最终提供广泛且不同的功能. (2)使用不同的存储引擎也可以说不同类型的表 (3)MySQL支持的存储引擎 MyISAM InnoDB Memory CSV Archive 查看数据表的创建语句: SHOW CREATE TABLE 表名 相关概念: (1).并发控制:一个人读数据,另外一个人在删除这个数据. 当多个连接对记录进行修改时保证数据的一致性

[mysql][【优化集合】mysql数据库优化集合

三个层面: 1.系统层面 2.mysql配置参数 3.sql语句优化 =========================================================== 一.系统层面 =========================================================== 二.mysql参数层面 http://www.oicto.com/mysql-explain-show/ 2.1slowlog 配置slowlog 配置文件: log-slow

数据库优化处理

数据库的优化程度影响了一个程序的执行力和用户的体验感,所以数据库的优化显得格外重要. 一.框架 根据业务需求选择合适的开发框架,不近对数据库的优化有帮助,而且对于程序后期的维护也很有帮助,根据项目的需求,看项目需要满足多少人的访问量,并发量到多少.不是说小公司就不需要分布式.大数据这些,考虑长期的问题. 将一些固定不常变化的值,设置为常量:采用缓存机制,减少对于数据库的访问(连接池):服务器的优化(队列): 二.数据库本身 1.表结构的设计 表的设计来源于需求,同时表的建立也会影响需求的实现,一