张宇,ZCP高级认证工程师
Zabbix 6.0 LTS版本官方正式发布已经有一段时间了,相信很多小伙伴都已经尝试了新搭建一套测试环境练练。对于正式环境的想要升级,又需要保留数据,这时候该如何对数据库同步升级呢?
从Zabbix 6.0开始,主键用于新版本的所有表。本节提供如何手动将现有安装中的历史表升级到主键的说明。
MySQL 5.7 /8.0
代码语言:javascript复制重命名旧表名创建一个新的表名。运行这个sql history_pk_prepare.sql. sql文件,二进制包安装方式地址:
/usr/share/doc/zabbix-sql-scripts/mysql/history_pk_prepare.sql
源码包里地址:zabbix-6.0.0/database/mysql/ history_pk_prepare.sql)
导出导入数据:
Mysqlsh应该安装。Mysqlsh(https://dev.mysql.com/doc/mysql-shell/8.0/en/mysql-shell-install-linux-quick.html)应该能够连接到数据库。如果连接是通过套接字完成的,则可能需要它显式地声明到它的路径。
代码语言:javascript复制 mysqlsh -uroot -S /run/mysqld/mysqld.sock --no-password -Dzabbix
运行:(CSVPATH功能需要被启用 参数local_infile = on):
代码语言:javascript复制CSVPATH="/var/lib/mysql-files";
util.exportTable("history_old", CSVPATH "/history.csv", { dialect: "csv" });
util.importTable(CSVPATH "/history.csv", {"dialect": "csv", "table": "history" });
util.exportTable("history_uint_old", CSVPATH "/history_uint.csv", { dialect: "csv" });
util.importTable(CSVPATH "/history_uint.csv", {"dialect": "csv", "table": "history_uint" });
util.exportTable("history_str_old", CSVPATH "/history_str.csv", { dialect: "csv" });
util.importTable(CSVPATH "/history_str.csv", {"dialect": "csv", "table": "history_str" });
util.exportTable("history_log_old", CSVPATH "/history_log.csv", { dialect: "csv" });
util.importTable(CSVPATH "/history_log.csv", {"dialect": "csv", "table": "history_log" });
util.exportTable("history_text_old", CSVPATH "/history_text.csv", { dialect: "csv" });
util.importTable(CSVPATH "/history_text.csv", {"dialect": "csv", "table": "history_text" });
验证每个步骤正常执行
删除旧的表:
代码语言:javascript复制DROP TABLE history_old;
DROP TABLE history_uint_old;
DROP TABLE history_str_old;
DROP TABLE history_log_old;
DROP TABLE history_text_old;
当MySQL <5.7, MariaDB (或者因为一些原因mysqlsh不能使用时)
这个选项更慢,更耗时,只有在有理由不使用mysqlsh时才使用。
重命名旧表,创建新的表执行 history_pk_prepare.sql。
代码语言:javascript复制mysql -uzabbix -p<password> zabbix < /usr/share/doc/zabbix-sql-scripts/mysql/history_pk_prepare.sql
导出和导入数据:
检查是否只对指定路径下的文件启用导入/导出:
代码语言:javascript复制mysql> SELECT @@secure_file_priv;
-----------------------
| @@secure_file_priv |
-----------------------
| /var/lib/mysql-files/ |
-----------------------
如果该值是目录的路径,则可以对该目录下的文件执行导出/导入操作。在这种情况下,应该相应地编辑查询中的文件路径。或者,secure_file_priv可以在升级期间禁用(设置为空字符串)。如果该值为空,则可以对位于任何位置的文件执行导出/导入操作。
*在导出数据之前应该禁用max_execution_time,以避免在导出期间超时 ***
代码语言:javascript复制SET @@max_execution_time=0;
SELECT * INTO OUTFILE '/var/lib/mysql-files/history.csv' FIELDS TERMINATED BY ',' ESCAPED BY '"' LINES TERMINATED BY 'n' FROM history_old;
LOAD DATA INFILE '/var/lib/mysql-files/history.csv' IGNORE INTO TABLE history FIELDS TERMINATED BY ',' ESCAPED BY '"' LINES TERMINATED BY 'n';
SELECT * INTO OUTFILE '/var/lib/mysql-files/history_uint.csv' FIELDS TERMINATED BY ',' ESCAPED BY '"' LINES TERMINATED BY 'n' FROM history_uint_old;
LOAD DATA INFILE '/var/lib/mysql-files/history_uint.csv' IGNORE INTO TABLE history_uint FIELDS TERMINATED BY ',' ESCAPED BY '"' LINES TERMINATED BY 'n';
SELECT * INTO OUTFILE '/var/lib/mysql-files/history_str.csv' FIELDS TERMINATED BY ',' ESCAPED BY '"' LINES TERMINATED BY 'n' FROM history_str_old;
LOAD DATA INFILE '/var/lib/mysql-files/history_str.csv' IGNORE INTO TABLE history_str FIELDS TERMINATED BY ',' ESCAPED BY '"' LINES TERMINATED BY 'n';
SELECT * INTO OUTFILE '/var/lib/mysql-files/history_log.csv' FIELDS TERMINATED BY ',' ESCAPED BY '"' LINES TERMINATED BY 'n' FROM history_log_old;
LOAD DATA INFILE '/var/lib/mysql-files/history_log.csv' IGNORE INTO TABLE history_log FIELDS TERMINATED BY ',' ESCAPED BY '"' LINES TERMINATED BY 'n';
SELECT * INTO OUTFILE '/var/lib/mysql-files/history_text.csv' FIELDS TERMINATED BY ',' ESCAPED BY '"' LINES TERMINATED BY 'n' FROM history_text_old;
LOAD DATA INFILE '/var/lib/mysql-files/history_text.csv' IGNORE INTO TABLE history_text FIELDS TERMINATED BY ',' ESCAPED BY '"' LINES TERMINATED BY 'n';
验证每个步骤正常执行
删除旧的表:
代码语言:javascript复制DROP TABLE history_old;
DROP TABLE history_uint_old;
DROP TABLE history_str_old;
DROP TABLE history_log_old;
DROP TABLE history_text_old;
提高性能一些参数设置:
在这两种情况下提高性能的案例:
***在[mysqld]部分的配置文件中增加bulk_insert_buffer_size缓冲区,或者在导入之前用set设置: ***
代码语言:javascript复制[mysqld]
bulk_insert_buffer_size=256M
mysql cli > SET SESSION bulk_insert_buffer_size= 1024 * 1024 * 256;
mysql cli > ... import queries ...
参见“优化InnoDB批量数据加载”:(MySQL5.7,MySQL8.0)
***禁用二进制日志记录(视情况而定): ***
代码语言:javascript复制mysql cli > SET SESSION SQL_LOG_BIN=0;
mysql cli > ... import queries ...
最后,祝大家升级一路畅通。 更多详细的数据库升级方案参考地址。https://www.zabbix.com/documentation/current/en/manual/appendix/install/db_primary_keys#mysql