This is a translation of the original English documentation page. Help us make it better.

3 データベースの主キーのアップグレード

概要

このセクションでは、既存のインストール環境のテーブルを手動で主キーにアップグレードする手順について説明します。

主キーにアップグレードすると、データのインデックス作成とアクセス方法が最適化され、クエリの高速化と容量節約につながります。 また、クラスタ化された環境でのデータ管理と同期が改善され、スケーリングが容易になり、一部のサーバーに障害が発生してもシステムの信頼性が維持されます。

このページに記載されている手順は上級ユーザー向けに設計されており、お客様の構成に合わせて調整する必要がある場合があります。 主キーへのアップグレードは時間がかかり、多くのリソースを消費する可能性があります。 十分な空きディスク容量があることを確認してください。データベースのサイズと保存データによっては、このプロセスに現在履歴テーブルで使用されている容量の最大2.5倍が必要になる場合があります。

Zabbix 6.0 以降の新規インストールでは、すべてのテーブルに主キーが使用されます。

主キーへのデータベースの自動アップグレードは行われませんが、既存のインストールは、Zabbix サーバーを 6.0 以降にアップグレードした後、手動でアップグレードできます。

Zabbix 7.0 以降、テーブルを主キーにアップグレードすると、倍精度データ型を使用するようにもアップグレードされます。

Zabbix 7.0 をお使いの場合、テーブルではすでに倍精度が使用されています。 ただし、このページの手順に従えば、既に倍精度を使用しているテーブルに影響を与えることなく、テーブルを主キーにアップグレードできます。

Zabbix 6.4 (またはそれ以前) をお使いの場合は、まずテーブルを倍精度にアップグレードすることを検討してください。 詳細については、拡張範囲の数値へのアップグレード を参照してください。

以下の手順が利用可能です:

重要事項

データベースのアップグレードは、以下の手順で実施します:

  1. Zabbix サーバーを停止します。

アップグレード中は Zabbix サーバーを停止する事を強く推奨します。 ただし、どうしても必要な場合は、サーバーを起動したままアップグレードを実行できます。(MySQL、MariaDB、およびTimescaleDBを使用しないPostgreSQLのみ)

  1. データベースをバックアップします。

PostgreSQL または TimescaleDB を使用している場合は、Zabbix 7.0.0-7.0.4 で作成された PostgreSQL または TimescaleDB バックアップの復元については、既知の問題を参照してください。

  1. お使いのZabbixバージョンと互換性のある最新のzabbix-sql-scriptsパッケージをインストールします(例:RHELの場合:「dnf install zabbix-sql-scripts」)。
  2. データベース用のスクリプトを実行します。
  3. Zabbixサーバーを起動します。

サーバーデータベースのみのスクリプトを実行してください。プロキシでこのアップグレードの実施は不要です。

データベースでパーティション分割を使用している場合は、DB管理者またはZabbixサポートにお問い合わせください。

プライマリキーへのアップグレードが成功したら、CSVファイルは削除できます。

オプションで、Zabbix フロントエンドを メンテナンス モードに切り替えることができます。

MySQL

セッションがドロップされないように、エクスポートとインポートは tmux/screen で実行する必要があります。

参照: 重要事項

mysqlsh を使用した MySQL 8.0+

この方法は実行中のZabbixサーバーで使用できますが、アップグレード中はサーバーを停止する事を推奨します。MySQL Shell(mysqlsh)がインストール済みで、DBに接続できる必要があります。

  • MySQLコンソールにroot(推奨)またはFILE権限を持つユーザーとしてログインします。

  • local_infile変数を有効にしてMySQLを起動します。

  • history_upgrade_prepare.sqlを実行して、古いテーブルの名前を変更し、新しいテーブルを作成します。

mysql -uzabbix -p<password> zabbix < /usr/share/zabbix-sql-scripts/mysql/option-patches/history_upgrade_prepare.sql
  • データのエクスポートとインポート

mysqlsh 経由で接続します。 ソケット接続を使用している場合は、パスの指定が必要になる場合があります。

sudo mysqlsh -uroot -S /run/mysqld/mysqld.sock --no-password -Dzabbix

次にJavaScriptモードに切り替えます:

\js

次に以下のコードを実行します。(CSVPATH は必要に応じて変更できます):

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 is not supported"というメッセージが表示される場合、インストールされているMySQL ShellではJavaScriptがサポートされていません。その場合は、Oracleの公式MySQL Shellパッケージをインストール(またはソースからビルド)して、JavaScriptモードを有効にしてください。

mysqlsh を使用しない MariaDB/MySQL 8.0+

このアップグレード方法は時間がかかるため、mysqlsh を使用したアップグレードが不可能な場合にのみ使用してください。

テーブルアップグレード
  • MySQLコンソールにroot(推奨)またはFILE権限を持つユーザーとしてログインします。

  • local_infile変数を有効にしてMySQLを起動します。

  • history_upgrade_prepare.sqlを実行して、古いテーブルの名前を変更し、新しいテーブルを作成します。

mysql -uzabbix -p<password> zabbix < /usr/share/zabbix-sql-scripts/mysql/option-patches/history_upgrade_prepare.sql
停止したサーバーでの移行

移行中のタイムアウトを回避するには、データを移行する前にmax_execution_timeを無効にする必要があります。

SET @@max_execution_time=0;
       
       INSERT IGNORE INTO history SELECT * FROM history_old;
       INSERT IGNORE INTO history_uint SELECT * FROM history_uint_old;
       INSERT IGNORE INTO history_str SELECT * FROM history_str_old;
       INSERT IGNORE INTO history_log SELECT * FROM history_log_old;
       INSERT IGNORE INTO history_text SELECT * FROM history_text_old;

移行後の手順 に従って、古いテーブルを削除します。

実行中のサーバーで移行

インポート/エクスポートが有効になっているパスを確認します:

mysql> SELECT @@secure_file_priv;
       +-----------------------+
       | @@secure_file_priv    |
       +-----------------------+
       | /var/lib/mysql-files/ |
       +-----------------------+

secure_file_privの値がディレクトリへのパスである場合、そのディレクトリ内のファイルに対してエクスポート/インポートが実行されます。この場合、クエリ内のファイルへのパスを編集するか、アップグレード時にsecure_file_privの値を空の文字列に設定してください。

secure_file_priv 値が空の場合、どの場所からでもエクスポート/インポートを実行できます。

secure_file_priv 値が NULL の場合、エクスポートされたテーブル データが含まれるパス(上記の例では '/var/lib/mysql-files/')に設定します。

詳細については、MySQLドキュメントを参照してください。

max_execution_time must be disabled before exporting data to avoid timeout during export.

エクスポート中のタイムアウトを回避するには、データをエクスポートする前にmax_execution_timeを無効にする必要があります。

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';

移行後の手順 に従って、古いテーブルを削除します。

PostgreSQL

セッションが切断されないようにするには、エクスポートとインポートは tmux/screen で実行する必要があります。 TimescaleDB をインストールする場合は、このセクションをスキップして PostgreSQL + TimescaleDB に進んでください。

参照: 重要事項

テーブルのアップグレード

  • history_upgrade_prepare.sql を使用してテーブルの名前を変更します:
sudo -u zabbix psql zabbix < /usr/share/zabbix-sql-scripts/postgresql/option-patches/history_upgrade_prepare.sql

停止したサーバーでの移行

  • 現在の監視データをエクスポートし、一時テーブルにインポートし、重複を無視して新しいテーブルにデータを挿入します:
INSERT INTO history SELECT * FROM history_old ON CONFLICT (itemid,clock,ns) DO NOTHING;
       
       INSERT INTO history_uint SELECT * FROM history_uint_old ON CONFLICT (itemid,clock,ns) DO NOTHING;
       
       INSERT INTO history_str SELECT * FROM history_str_old ON CONFLICT (itemid,clock,ns) DO NOTHING;
       
       INSERT INTO history_log SELECT * FROM history_log_old ON CONFLICT (itemid,clock,ns) DO NOTHING;
       
       INSERT INTO history_text SELECT * FROM history_text_old ON CONFLICT (itemid,clock,ns) DO NOTHING;

INSERT パフォーマンスを向上させるためのヒントを参照してください: PostgreSQL: Bulk Loading Huge Amounts of Data, Checkpoint Distance and Amount of WAL.

実行中のサーバーでの移行

  • 現在の監視データをエクスポートし、一時テーブルにインポートし、重複を無視して新しいテーブルにデータを挿入します:
\copy history_old TO '/tmp/history.csv' DELIMITER ',' CSV
       CREATE TEMP TABLE temp_history (
           itemid                   bigint                                    NOT NULL,
           clock                    integer         DEFAULT '0'               NOT NULL,
           value                    DOUBLE PRECISION DEFAULT '0.0000'          NOT NULL,
           ns                       integer         DEFAULT '0'               NOT NULL
       );
       \copy temp_history FROM '/tmp/history.csv' DELIMITER ',' CSV
       INSERT INTO history SELECT * FROM temp_history ON CONFLICT (itemid,clock,ns) DO NOTHING;
       
       \copy history_uint_old TO '/tmp/history_uint.csv' DELIMITER ',' CSV
       CREATE TEMP TABLE temp_history_uint (
           itemid                   bigint                                    NOT NULL,
           clock                    integer         DEFAULT '0'               NOT NULL,
           value                    numeric(20)     DEFAULT '0'               NOT NULL,
           ns                       integer         DEFAULT '0'               NOT NULL
       );
       \copy temp_history_uint FROM '/tmp/history_uint.csv' DELIMITER ',' CSV
       INSERT INTO history_uint SELECT * FROM temp_history_uint ON CONFLICT (itemid,clock,ns) DO NOTHING;
       
       \copy history_str_old TO '/tmp/history_str.csv' DELIMITER ',' CSV
       CREATE TEMP TABLE temp_history_str (
           itemid                   bigint                                    NOT NULL,
           clock                    integer         DEFAULT '0'               NOT NULL,
           value                    varchar(255)    DEFAULT ''                NOT NULL,
           ns                       integer         DEFAULT '0'               NOT NULL
       );
       \copy temp_history_str FROM '/tmp/history_str.csv' DELIMITER ',' CSV
       INSERT INTO history_str (itemid,clock,value,ns) SELECT * FROM temp_history_str ON CONFLICT (itemid,clock,ns) DO NOTHING;
       
       \copy history_log_old TO '/tmp/history_log.csv' DELIMITER ',' CSV
       CREATE TEMP TABLE temp_history_log (
           itemid                   bigint                                    NOT NULL,
           clock                    integer         DEFAULT '0'               NOT NULL,
           timestamp                integer         DEFAULT '0'               NOT NULL,
           source                   varchar(64)     DEFAULT ''                NOT NULL,
           severity                 integer         DEFAULT '0'               NOT NULL,
           value                    text            DEFAULT ''                NOT NULL,
           logeventid               integer         DEFAULT '0'               NOT NULL,
           ns                       integer         DEFAULT '0'               NOT NULL
       );
       \copy temp_history_log FROM '/tmp/history_log.csv' DELIMITER ',' CSV
       INSERT INTO history_log SELECT * FROM temp_history_log ON CONFLICT (itemid,clock,ns) DO NOTHING;
       
       \copy history_text_old TO '/tmp/history_text.csv' DELIMITER ',' CSV
       CREATE TEMP TABLE temp_history_text (
           itemid                   bigint                                    NOT NULL,
           clock                    integer         DEFAULT '0'               NOT NULL,
           value                    text            DEFAULT ''                NOT NULL,
           ns                       integer         DEFAULT '0'               NOT NULL
       );
       \copy temp_history_text FROM '/tmp/history_text.csv' DELIMITER ',' CSV
       INSERT INTO history_text SELECT * FROM temp_history_text ON CONFLICT (itemid,clock,ns) DO NOTHING;

PostgreSQL + TimescaleDB

セッションが切断されないようにするには、エクスポートとインポートはtmux/screenで実行する必要があります。 アップグレード中はZabbixサーバーを停止する必要があります。

参照: 重要事項

  • history_upgrade_prepare.sql を使用してテーブルの名前を変更します。
    • 圧縮が有効になっている場合 (デフォルトのインストール) は、/usr/share/zabbix-sql-scripts/postgresql/timescaledb/option-patches/with-compression からスクリプトを実行します:

      cat /usr/share/zabbix-sql-scripts/postgresql/timescaledb/option-patches/with-compression/history_upgrade_prepare.sql | sudo -u zabbix psql zabbix
    • 圧縮が無効になっている場合は、/usr/share/zabbix-sql-scripts/postgresql/timescaledb/option-patches/without-compression からスクリプトを実行します:

      cat /usr/share/zabbix-sql-scripts/postgresql/timescaledb/option-patches/without-compression/history_upgrade_prepare.sql | sudo -u zabbix psql zabbix
  • 圧縮設定に基づいて TimescaleDB ハイパーテーブル移行スクリプトを実行します:
    • 圧縮が有効になっている場合(デフォルトのインストール)、/usr/share/zabbix-sql-scripts/postgresql/timescaledb/option-patches/with-compression からスクリプトを実行します:

      cat /usr/share/zabbix-sql-scripts/postgresql/timescaledb/option-patches/with-compression/history_upgrade.sql | sudo -u zabbix psql zabbix
             cat /usr/share/zabbix-sql-scripts/postgresql/timescaledb/option-patches/with-compression/history_upgrade_uint.sql | sudo -u zabbix psql zabbix
             cat /usr/share/zabbix-sql-scripts/postgresql/timescaledb/option-patches/with-compression/history_upgrade_log.sql | sudo -u zabbix psql zabbix
             cat /usr/share/zabbix-sql-scripts/postgresql/timescaledb/option-patches/with-compression/history_upgrade_str.sql | sudo -u zabbix psql zabbix
             cat /usr/share/zabbix-sql-scripts/postgresql/timescaledb/option-patches/with-compression/history_upgrade_text.sql | sudo -u zabbix psql zabbix
             cat /usr/share/zabbix-sql-scripts/postgresql/timescaledb/option-patches/with-compression/trends_upgrade.sql | sudo -u zabbix psql zabbix
    • 圧縮が無効になっている場合は、/usr/share/zabbix-sql-scripts/postgresql/timescaledb/option-patches/without-compression からスクリプトを実行します:

      cat /usr/share/zabbix-sql-scripts/postgresql/timescaledb/option-patches/without-compression/history_upgrade.sql | sudo -u zabbix psql zabbix
             cat /usr/share/zabbix-sql-scripts/postgresql/timescaledb/option-patches/without-compression/history_upgrade_uint.sql | sudo -u zabbix psql zabbix
             cat /usr/share/zabbix-sql-scripts/postgresql/timescaledb/option-patches/without-compression/history_upgrade_log.sql | sudo -u zabbix psql zabbix
             cat /usr/share/zabbix-sql-scripts/postgresql/timescaledb/option-patches/without-compression/history_upgrade_str.sql | sudo -u zabbix psql zabbix
             cat /usr/share/zabbix-sql-scripts/postgresql/timescaledb/option-patches/without-compression/history_upgrade_text.sql | sudo -u zabbix psql zabbix
             cat /usr/share/zabbix-sql-scripts/postgresql/timescaledb/option-patches/without-compression/trends_upgrade.sql | sudo -u zabbix psql zabbix

参照: INSERT パフォーマンスを向上させるためのTips

Oracle

Zabbix 7.0以降、Oracle DB のサポートは非推奨になりました。

セッションが切断されないようにするには、エクスポートとインポートは tmux/screen で実行する必要があります。 アップグレード中は Zabbix サーバーを停止する必要があります。

参照: 重要事項

テーブルのアップグレード

パフォーマンスについては、ドキュメントOracle Data Pump Performanceを参照してください。

  • history_upgrade_prepare.sql を使用してテーブルの名前を変更します。
cd /path/to/zabbix-sources/database/oracle/option-patches
       sqlplus zabbix/password@oracle_host/service
       sqlplus> @history_upgrade_prepare.sql

ヒストリテーブルの一括移行

  • Data Pump 用のディレクトリを準備します。

Data Pump には、これらのディレクトリに対する読み取り権限と書き込み権限が必要です。

例:

mkdir -pv /export/history
       chown -R oracle:oracle /export
  • ディレクトリオブジェクトを作成し、Zabbix認証に使用するユーザー(以下の例では「zabbix」)にこのオブジェクトの読み取りおよび書き込み権限を付与します。sysdba ロールで、以下を実行します:
create directory history as '/export/history';
       grant read,write on directory history to zabbix;
  • テーブルをエクスポートします。Nを目的のスレッド数に置き換えます。
expdp zabbix/password@oracle_host/service \
           DIRECTORY=history \
           TABLES=history_old,history_uint_old,history_str_old,history_log_old,history_text_old \
           PARALLEL=N
  • テーブルをインポートします。Nを目的のスレッド数に置き換えます。
impdp zabbix/password@oracle_host/service \
           DIRECTORY=history \
           TABLES=history_uint_old \
        REMAP_TABLE=history_old:history,history_uint_old:history_uint,history_str_old:history_str,history_log_old:history_log,history_text_old:history_text \
           data_options=SKIP_CONSTRAINT_ERRORS table_exists_action=APPEND  PARALLEL=N CONTENT=data_only

ヒストリテーブルの個別移行

  • ヒストリテーブルごとにData Pump用のディレクトリを用意してください。 Data Pump には、これらのディレクトリに対する読み取り権限と書き込み権限が必要です。

例:

mkdir -pv /export/history /export/history_uint /export/history_str /export/history_log /export/history_text
       chown -R oracle:oracle /export
  • ディレクトリオブジェクトを作成し、Zabbix認証に使用するユーザー(以下の例では「zabbix」)にこのオブジェクトの読み取りおよび書き込み権限を付与します。sysdba ロールで、以下を実行します:
create directory history as '/export/history';
       grant read,write on directory history to zabbix;
       
       create directory history_uint as '/export/history_uint';
       grant read,write on directory history_uint to zabbix;
       
       create directory history_str as '/export/history_str';
       grant read,write on directory history_str to zabbix;
       
       create directory history_log as '/export/history_log';
       grant read,write on directory history_log to zabbix;
       
       create directory history_text as '/export/history_text';
       grant read,write on directory history_text to zabbix;
  • 各テーブルをエクスポートおよびインポートします。Nを目的のスレッド数に置き換えます。
expdp zabbix/password@oracle_host:1521/xe DIRECTORY=history TABLES=history_old PARALLEL=N
       
       impdp zabbix/password@oracle_host:1521/xe DIRECTORY=history TABLES=history_old REMAP_TABLE=history_old:history data_options=SKIP_CONSTRAINT_ERRORS table_exists_action=APPEND PARALLEL=N CONTENT=data_only
       
       expdp zabbix/password@oracle_host:1521/xe DIRECTORY=history_uint TABLES=history_uint_old PARALLEL=N
       
       impdp zabbix/password@oracle_host:1521/xe DIRECTORY=history_uint TABLES=history_uint_old REMAP_TABLE=history_uint_old:history_uint data_options=SKIP_CONSTRAINT_ERRORS table_exists_action=APPEND PARALLEL=N CONTENT=data_only
       
       expdp zabbix/password@oracle_host:1521/xe DIRECTORY=history_str TABLES=history_str_old PARALLEL=N
       
       impdp zabbix/password@oracle_host:1521/xe DIRECTORY=history_str TABLES=history_str_old REMAP_TABLE=history_str_old:history_str data_options=SKIP_CONSTRAINT_ERRORS table_exists_action=APPEND PARALLEL=N CONTENT=data_only
       
       expdp zabbix/password@oracle_host:1521/xe DIRECTORY=history_log TABLES=history_log_old PARALLEL=N
       
       impdp zabbix/password@oracle_host:1521/xe DIRECTORY=history_log TABLES=history_log_old REMAP_TABLE=history_log_old:history_log data_options=SKIP_CONSTRAINT_ERRORS table_exists_action=APPEND PARALLEL=N CONTENT=data_only
       
       expdp zabbix/password@oracle_host:1521/xe DIRECTORY=history_text TABLES=history_text_old PARALLEL=N
       
       impdp zabbix/password@oracle_host:1521/xe DIRECTORY=history_text TABLES=history_text_old REMAP_TABLE=history_text_old:history_text data_options=SKIP_CONSTRAINT_ERRORS table_exists_action=APPEND PARALLEL=N CONTENT=data_only

移行後の手順

すべてのデータベースで、移行完了後に次の手順を実施します。

  • すべてが想定どおりに機能することを確認します。

  • 古いテーブルを削除:

DROP TABLE history_old;
       DROP TABLE history_uint_old;
       DROP TABLE history_str_old;
       DROP TABLE history_log_old;
       DROP TABLE history_text_old;

参照