Zabbixで使用しているMySQLのオンラインバックアップとレプリケーションを設定する
Zabbixのアクションやユーザマクロなどテンプレートのエクスポートだけではカバーできない箇所までバックアップを取りたい場合、バックエンドのMySQLのデータをバックアップとして取って置くのが良いかと思います。
今回、既にある程度のデータが蓄積された状態でバックアップ取得とそのデータを用いたレプリケーション構成(Slave構築)を検証した際のメモです。
以下、全体の流れです。 - MySQL現状確認 - レプリケーション設定(マスター側) - リストア用設定 - xtrabackupインストール - xtrabackupを使ったバックアップ - xtrabackupを使ったリストア - レプリケーション設定(スレーブ側) - リストアデータの確認
MySQL現状確認
zabbixユーザでMySQLを立ち上げてエンジンを確認します。 こちらの環境では、以下の通りInnoDBを使用していました。
mysql> use information_schema; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select table_name, engine from tables where table_schema = "zabbix"; +-----------------------+--------+ | table_name | engine | +-----------------------+--------+ | acknowledges | InnoDB | | actions | InnoDB | | alerts | InnoDB | | application_template | InnoDB | 〜 略 〜 | users | InnoDB | | users_groups | InnoDB | | usrgrp | InnoDB | | valuemaps | InnoDB | +-----------------------+--------+ 108 rows in set (0.27 sec)
バージョンも忘れず確認します。
mysql> SELECT version(); +------------+ | version() | +------------+ | 5.5.37-log | +------------+ 1 row in set (0.01 sec)
テーブルサイズも確認しておきます。
mysql> USE zabbix; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select table_schema, sum(data_length+index_length) /1024 /1024 as MB from information_schema.tables where table_schema = database(); +--------------+----------------+ | table_schema | MB | +--------------+----------------+ | zabbix | 36573.98437500 | +--------------+----------------+ 1 row in set (5.24 sec)
レプリ設定(マスター側)
マスター側ではbinファイルの設定とサーバIDの設定をしておきます。 1はサーバIDを設定していない場合のデフォルト値なので1001を使用しています。
$ sudo vim /etc/my.cnf [mysqld] log-bin=mysql-bin server-id=1001 $ sudo service mysqld restart mysqld を停止中: [ OK ] mysqld を起動中: [ OK ]
スレーブがアクセスしてくる際の設定をしておきます。
mysql > GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.0/255.255.255.0' IDENTIFIED BY 'slavepass';
リストア用設定
リストアの際は書き込みに失敗したら再度リストアをすれば良いという考えの元、書き込みログやdoublewriteも必要なしとして設定しています。リストア後に忘れずに戻してください。
mysql> SHOW GLOBAL VARIABLES LIKE '%log_bin%'; +---------------------------------+-------+ | Variable_name | Value | +---------------------------------+-------+ | log_bin | ON | | log_bin_trust_function_creators | OFF | | sql_log_bin | OFF | +---------------------------------+-------+ 3 rows in set (0.00 sec) $ sudo vim /etc/my.cnf innodb_flush_log_at_trx_commit = 2 skip_innodb_doublewrite read_buffer_size = 2G read_rnd_buffer_size = 2G innodb_file_per_table innodb_autoextend_increment = 64 innodb_data_file_path = ibdata1:1G:autoextend innodb_data_file_path = ibdata1:1G:autoextend innodb_write_io_threads = 8 innodb_read_io_threads = 8 innodb_log_buffer_size = 32
xtrabackupインストール
yumでインストールできます。マスター、スレーブの両方で必要になりますのでインストールします。
$ rpm -Uhv http://www.percona.com/downloads/percona-release/percona-release-0.0-1.x86_64.rpm $ yum install xtrabackup
xtrabackupを使用したバックアップ
一応、myslqdumpでバックアップを取っておきます。 データベースをロックする必要があるので、止められない環境の人は実施しないでください。
合わせて現在のBinlogファイルと位置情報を確認しておきます。
mysql> FLUSH TABLES WITH READ LOCK; Query OK, 0 rows affected (1.39 sec) mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000002 | 9953953 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
ダウンプファイルを生成してロックを完了したら解除します。 このテーブルサイズで2時間程度でした。これをリストア使用とすると2日はかかります・・・。
mysqldump -u zabbix -p --all-databases --lock-all-tables > dbdump.db mysql > UNLOCK TABLES;
任意の場所にバックアップを取得します。innobackupexはextrabackupのwrapperです。先ほどのextrabackupのインストールで同時にインストールされます。
$ sudo time /usr/bin/innobackupex --user root /backup/xtraback up/ InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy and Percona LLC and/or its affiliates 2009-2013. All Rights Reserved. 〜 略 〜 At the end of a successful backup run innobackupex prints "completed OK!". innobackupex: Using mysql server version 5.5.37-log xtrabackup: Transaction log of lsn (449773900208) to (449851827464) was copied. 140928 11:04:56 innobackupex: All tables unlocked innobackupex: Backup created in directory '/backup/xtrabackup/2014-09-28_10-32-14' innobackupex: MySQL binlog position: filename 'mysql-bin.000004', position 51212055 innobackupex: Connection to database server closed innobackupex: completed OK!
completed OK!が出れば完了です。binlogのファイル名とpositionも出力されますので確認してください。時間は30分くらいです。
xtrabackupを使用したリストア
先ほどのバックアップフォルダをSCPでスレーブにコピーし、xtrabackupでリストアします。 だいたい1時間くらいでした。早いです。
$ sudo /usr/bin/innobackupex --apply-log 2014-09-28_ 10-32-14 InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy and Percona LLC and/or its affiliates 2009-2013. All Rights Reserved. 〜 略 〜 InnoDB: Last MySQL binlog file position 0 51212055, file name ./mysql-bin.000004 xtrabackup: starting shutdown with innodb_fast_shutdown = 1 140928 12:49:32 InnoDB: Starting shutdown... 140928 12:49:36 InnoDB: Shutdown completed; log sequence number 449854104891 140928 12:49:36 innobackupex: completed OK! $ sudo /usr/bin/innobackupex --copy-back 2014-09-28_10-32-14 InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy and Percona LLC and/or its affiliates 2009-2013. All Rights Reserved. This software is published under the GNU GENERAL PUBLIC LICENSE Version 2, June 1991. IMPORTANT: Please check that the copy-back run completes successfully. At the end of a successful copy-back run innobackupex prints "completed OK!". 〜 略 〜 2014-09-28_10-32-14/ib_logfile0' to '/var/lib/mysql' innobackupex: Finished copying back files. 140928 13:00:52 innobackupex: completed OK!
エラーが出た場合、データフォルダが空になっているかどうか。 アクセス権限がしっかり設定されているか確認してください。
リストアデータの確認
スレーブ側にデータがリストアできているか確認します。
$ mysql -u root Welcome to the MySQL monitor. Commands end with ; or ¥g. Your MySQL connection id is 1 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '¥h' for help. Type '¥c' to clear the current input statement. mysql> use information_schema; Database changed mysql> select table_name, engine from tables where table_schema = "zabbix"; +-----------------------+--------+ | table_name | engine | +-----------------------+--------+ | acknowledges | InnoDB | | actions | InnoDB | | alerts | InnoDB | 〜 略 〜 | triggers | InnoDB | | user_history | InnoDB | | users | InnoDB | | users_groups | InnoDB | | usrgrp | InnoDB | | valuemaps | InnoDB | +-----------------------+--------
MySQLレプリ設定
マスターのステータスを確認し現在のマスターログのファイルと位置を指定してスレーブ側に設定します。最初に設定したパスワードも必要になるので確認してください。
mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.1', MASTER_USER='slave', MASTER_PASSWORD='slavepass', MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=133762325; Query OK, 0 rows affected (0.53 sec) mysql> SHOW SLAVE STATUS¥G *************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.0.1 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 133762325 Relay_Log_File: localhost-relay-bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: No Slave_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 133762325 Relay_Log_Space: 107 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0 1 row in set (0.00 sec)
ステータスを見てエラーになっていないようであれば、スレーブをスタートさせます。
mysql> START SLAVE; Query OK, 0 rows affected (0.00 sec)
MySQLレプリ確認
MySQLのデータ(Zabbixの更新データ)がマスターからスレーブに正しくレプリケーションされているか確認します。
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | zabbix | +--------------------+ 3 rows in set (0.00 sec) mysql> USE zabbix; Database changed mysql> select host, hostid from hosts; +------------------------------+--------+ | host | hostid | +------------------------------+--------+ | Template App HTTP Service | 10187 | | Template App HTTPS Service | 10188 | | Template App Zabbix Agent | 10129 | | Template App Zabbix Server | 10128 | | Template OS Linux | 10130 | | Template OS Mac OS X | 10079 | | Template SNMP Device | 10066 | | Template SNMP Disks | 10068 | | Template SNMP Generic | 10065 | | Template SNMP Interfaces | 10060 | | Template SNMP OS Linux | 10069 | | Template SNMP OS Windows | 10067 | | Template SNMP Processors | 10132 | +------------------------------+--------+
上記でホスト一覧を確認し、ZabbixのGUIからホストを追加して同じコマンドで変化があるか確認します。
mysql> select host, hostid from hosts; +------------------------------+--------+ | host | hostid | +------------------------------+--------+ | Template App HTTP Service | 10187 | | Template App HTTPS Service | 10188 | | Template App Zabbix Agent | 10129 | | Template App Zabbix Server | 10128 | | Template OS Linux | 10130 | | Template OS Mac OS X | 10079 | | Template SNMP Device | 10066 | | Template SNMP Disks | 10068 | | Template SNMP Generic | 10065 | | Template SNMP Interfaces | 10060 | | Template SNMP OS Linux | 10069 | | Template SNMP OS Windows | 10067 | | Template SNMP Processors | 10132 | | Test-20140928 | 10216 | +------------------------------+--------+
追加したTest-0140928が追加されていました。同様の確認方法のため省略しますが、削除も問題なく反映されていました。
その他
スレーブ側はリードオンリーに設定しておきましょう
$ vim /etc/my.cnf [mysqld] read_only
これで現時点のバックアップ取得ができました。今後、バックアップはスレーブから取るようにします。
シェアして頂けると嬉しいです。
参考になったという方がいれば是非お願いしますm(_ _ )m
モチベーション維持の観点で非常に励みになります。
- 作者: 遠藤俊裕,坂井恵,館山聖司,鶴長鎮一,とみたまさひろ,班石悦夫,松信嘉範
- 出版社/メーカー: 翔泳社
- 発売日: 2011/08/26
- メディア: 大型本
- 購入: 9人 クリック: 82回
- この商品を含むブログ (9件) を見る
改訂版 Zabbix統合監視実践入門 ~障害通知、傾向分析、可視化による省力運用 (Software Design plus)
- 作者: 寺島広大
- 出版社/メーカー: 技術評論社
- 発売日: 2014/06/17
- メディア: 大型本
- この商品を含むブログ (1件) を見る