Zabbix升级版本后界面及告警乱码问题

应用旧版本新版本
Zabbix Server5.06.0
Mysql5.7.308.0.37

参考文档:https://www.zabbix.com/documentation/6.0/zh/manual/appendix/install/db_charset_coll

问题

数据库及Zabbix升级完成后,出现部分中文乱码,其中包括告警、主机名等地方的中文乱码。

经过排查发现Zabbix 5.0在Mysql 5.7中使用的是utf8mb3的字符集,但是在mysql 8.0中该字符集已被启用,并使用了utf8mb4的字符集。

解决方案

  1. 查看现有数据库字符集及字符序。
SELECT @@character_set_database, @@collation_database;

如果看到此处字符集已经是utf8mb4 则可能是其他问题

  1. 停止 Zabbix 服务
  2. 数据库进行备份
  3. 使用如下命令修复数据库的字符集及字符序:
alter database zabbix character set utf8mb4 collate utf8mb4_bin;

再次查看可以看到字符集已经被修改

  1. 导入下面的 sql 来修复每张表中各个列的字符集
/* ChangeLog:
   2020.08.19 - initial release
   2020.09.04 - fixed syntax for running on MySQL
   2022.01.13 - updated the script to use standard UTF8 instead of utf8mb3
*/

DELIMITER $$
CREATE PROCEDURE zbx_convert_utf8 (
)

BEGIN
	declare cmd varchar(255) default "";
	declare finished integer default 0;

	declare cur_command cursor for 
		SELECT command
		FROM
		    (/* This 'select' statement deals with 'text' type columns to prevent
		        their automatic conversion into 'mediumtext' type.
		        The goal is to produce statements like
		         ALTER TABLE zabbix.hosts MODIFY COLUMN description text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin not null;
		     */
		     SELECT table_name AS sort1,
		                   'A' AS sort2,
		            CONCAT('ALTER TABLE ', table_schema, '.', table_name,
		                   ' MODIFY COLUMN ', column_name, ' ', column_type,
		                   ' CHARACTER SET utf8mb4 COLLATE utf8mb4_bin',
		                case
		                    when column_default is null then ''
		                    else concat(' default ', column_default, ' ')
		                end,
		                case
		                    when is_nullable = 'no' then ' not null '
		                    else ''
		                end,
		            ';') AS command
		        FROM information_schema.columns
		        WHERE table_schema = @ZABBIX_DATABASE        
		           AND column_type = 'text'
		    UNION
		     /* This 'select' statement deals with setting character set and collation for
		        each table and converting varchar fields on a per-table basis.
		        It is necessary to process all tables (even those with numeric-only columns)
		        otherwise in future Zabbix upgrades text (e.g. varchar) columns may be added
		        to these tables or numeric columns can be turned into text ones and
		        the old character set/collation can reappear again.
		        The goal is to produce statements like
		         ALTER TABLE zabbix.hosts CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
		     */
		     SELECT table_name AS sort1,
		                   'B' AS sort2,
		            CONCAT('ALTER TABLE ', table_schema, '.', table_name,
		                   ' CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;') AS command
		        FROM information_schema.tables
		        WHERE table_schema = @ZABBIX_DATABASE) s
		/* Sorting is important: 'MODIFY COLUMN' statements should precede 'CONVERT TO' ones
		   for each table. */
		ORDER BY sort1, sort2;
	
	declare continue handler for not found set finished = 1;

	open cur_command;
	cmd_loop: loop
		fetch cur_command into cmd;
		if finished = 1 then
			leave cmd_loop;
		end if;
		SET @value = cmd;
		PREPARE stmt FROM @value;
		EXECUTE stmt;
		DEALLOCATE PREPARE stmt;
	end loop cmd_loop;
	close cur_command;

END$$

DELIMITER ;

  1. 执行以下命令
SET @ZABBIX_DATABASE = 'zabbix';
set innodb_strict_mode = OFF; # mariadb需要执行     
CALL zbx_convert_utf8();
set innodb_strict_mode = ON;  # mariadb需要执行    
drop procedure zbx_convert_utf8;

如果没有错误信息 ,则表示成功

  1. 启动zabbix服务,等待字符集自动修改过来即可
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值