ZABBIXのデータベースをバックアップする手段として、レプリケーションを使って見ることにしました。
そのときの手順をまとめました。
OS | データベース | IPアドレス | |
---|---|---|---|
ZABBIXサーバー | Raspbian Buster | MariaDB | 192.168.1.11 |
DBサーバー | Ubuntu 18.04 | MariaDB | 192.168.3.22 |
ZABBIXサーバー側の設定
ZABBIXサーバーのデータベースがレプリケーションのマスターサーバーになる。
cnfファイルの設定
- bind設定をコメント化して、サーバーIDとバイナリログの設定をアンコメントして修正
cd /etc/mysql/mariadb.conf.d
vi 50-server.cnf
[mysqld]
・
・
# bind-address = 127.0.0.1
・
・
server-id = 101
log_bin = /var/log/mysql/mysql-bin.log
- 設定内容を反映させるためにMySQLを再起動
systemctl restart mysql.service
スレーブサーバー用のアカウントを作成
mysqlコマンドに入る
mysql -u root -p
create user 'repl'@'192.168.3.22' identified by 'P@ssw0rd';
grant replication slave on *.* to 'repl'@'192.168.3.22';
zabbixデーターベースのスナップショットを取得
zabbixデーターベースの内容をスレーブサーバーにコピーするためスナップショットを取得します。
- スナップショット取得中にDBが更新されないようにテーブルの書き込みをロック
flush tables with read lock;
- バイナリログの状態を確認する
show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 123628 | | |
+------------------+----------+--------------+------------------+
mysqlコマンドを抜ける
\q
- zabbixテーブルのスナップショットを取得
mysqldump --single-transaction -u root -p zabbix > zabbix.dump
- テーブルの書き込みをロックを解除
mysqlコマンドに入る
mysql -u root -p
unlock tables;
mysqlコマンドを抜ける
\q
- zabbixテーブルのスナップショットをscpでスレーブサーバーに転送
scp zabbix.dump ubuntu@192.168.3.22:/home/ubuntu
ここまでで、ZABBIXサーバー側の設定は完了
バックアップ用サーバー側の設定
cnfファイルの設定
- サーバーIDの設定をアンコメントして修正
cd /etc/mysql/mysql.conf.d
sudo vi mysqld.cnf
[mysqld]
・
・
server-id = 102
- 設定内容を反映させるためにMySQLを再起動
systemctl restart mysql.service
zabbixデーターベースのスナップショットを反映
mysqlコマンドに入る
mysql -u root -p
create database zabbix;
use zabbix;
source /home/ubuntu/zabbix.dump
スレーブデーターベースの設定
- スレーブデーターベースからマスターデータベースへ接続するためのパラメータを設定
※見やすさのために改行していますが実際には1行で入力
change master to
master_host='192.168.1.11',
master_user='repl',
master_password='P@assw0rd',
master_log_file='mysql-bin.000001',
master_log_pos=123628;
- レプリケーション対象にzabbixデータベースを設定
change replication filter replicate_do_db = (zabbix);
- レプリケーションを開始
start slave;
- スレーブサーバーの状態を確認
特にエラーが出力されていなければ問題なくレプリケーションの設定が行われています。
エラーの内容は、Last_IO_Error:、Last_SQL_Error:に表示されます。
Slave_SQL_Running_State:に状態が表示されます。
show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1693614
Relay_Log_File: mysql-slave2-relay-bin.000002
Relay_Log_Pos: 1570431
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: zabbix
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: 1693614
Relay_Log_Space: 1570645
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: 0
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: 101
Master_UUID:
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
レプリケーションの動作確認
最後に、zabbixサーバのzabbixデーターベースにhogeテーブルを追加したときに正常にレプリケーションが行われるか確認します。
バックアップ用サーバーに、hogeテーブルがないことを確認します。
mysql -u root -p
use zabbix;
show tables;
Empty set (0.00 sec)
ZABBIXサーバーのzabbixデータベースにhogeテーブルを追加します。
mysql -u root -p
use zabbix;
create table hoge ( id INT not null );
show tables;
+-------------------+
| Tables_in_example |
+-------------------+
| hoge |
+-------------------+
・バックアップ用サーバーのzabbixデータベースを確認し、hogeテーブルが追加されていれば、正常にレプリケーションが行われています。
mysql -u root -p
show tables;
+-------------------+
| Tables_in_example |
+-------------------+
| hoge |
+-------------------+
以上で、設定は完了です。
レプリケーションの状態をZABBIXで監視する
下記のサイトを参考に設定する。
Zabbix MySQLスレーブサーバ(レプリケーション)の監視設定メモ
エラーで止まっているレプリケーションを再開する
エラーの状況をエラーログや「SHOW SLAVE STATUS」などで確認する。
スレーブ側でSQLがエラーになっていて無視してよいものであれば、スキップして次に進ませる。
STOP SLAVE
SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;
START SLAVE