さくらのVPS-MariaDBでレプリケーション設定をする
さくらのVPSでMariaDBのレプリケーション設定をしていきたいと思います。前提として下記の条件が整っているとします。
- DBサーバー1(Master):192.168.0.2
- DBサーバー2(Slave):192.168.0.3
- CentOS7
- MariaDB:mysql Ver 15.1 Distrib 5.5.47-MariaDB
DB2台のOS、MariaDBのバージョンは二つとも同じとします。
マスター側の設定
レプリケーションのユーザー設定
- レプリケーションユーザー:repl
- レプリケーションは192.168.0.0/24のネットワークからの許可
- レプリケーションユーザーのパスワードはpasswordとします
[c] MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.0/255.255.255.0' IDENTIFIED BY 'password'; [/c]
my.cnfに設定を追記
my.confに設定を追記します。ここで間違ってはいけないのは[mysqld]の下に追記します。[mysqld_safe]に追記すると失敗しますので気をつけてください。 [c] [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock
Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
Settings user and group are ignored when systemd is used.
If you need to run mysqld under a different user or group,
customize your systemd unit file for mariadb according to the
instructions in http://fedoraproject.org/wiki/Systemd
#ここに追記 #add log_bin=/var/log/mariadb/binlog server-id=1 expire_logs_days=5 [mysqld_safe] ・・・ [/c] このようにします。
- log-bin でレプリケーションに必要なバイナリログを有効にします
- server-id はレプリケーションを構成するサーバー内で被らないように割り振ります
- expire_logs_days はバイナリログの保存日数です。レプリケーションに間が空いた時に復旧できるようにする期間を指定します
マスターのダンプ
マスター側のデータベースをダンプするため設定をします。 rootユーザーでDBにアクセス [c] mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2862 Server version: 5.5.47-MariaDB-log MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> [/c]
読み書きロックを行います
[c] MariaDB [(none)]> FLUSH TABLES WITH READ LOCK; Query OK, 0 rows affected (0.00 sec) [/c]
バイナリログのFileとPositionを確認します
[c] SHOW MASTER STATUS; +---------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +---------------+----------+--------------+------------------+ | binlog.000001 | 33424169 | | | +---------------+----------+--------------+------------------+ [/c]
mysqldump でマスターのDBをファイルに書き出します
[c] mysqldump -u root -p --all-databases --lock-all-tables > /tmp/dbdump.db Enter password: [/c]
読み書きロックを解除します
[c] mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2862 Server version: 5.5.47-MariaDB-log MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> UNLOCK TABLES; [/c]
SCPでデータを送る
ダンプしたファイルをスレーブ側へ送ってあげます。 [c] [root@] # scp /tmp/dbdump.db root@192.168.0.3:/tmp/ password:パスワードを入力 [/c]
MariaDBの再起動
[c] [root@] # systemctl restart mariadb [/c] 再起動と転送できたらこれでマスター側の構築を終了します。次にスレーブ側の設定をします
スレーブ側の設定
スレーブ側は新しいターミナルを立ち上げても現在のマスターからsshでログインしてもどちらでも構いません [c] ssh root@192.168.0.3 root@192.168.20.3's password: password: [/c]
mysql_install_db を実行
[c] [root@] # mysql_install_db --user=mysql [/c]
my.cnfに設定を追加
my.cnfに設定を追加します。今回設定するのはサーバーIDとデータベースを読み込み専用にするためのものです。 [c] [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock
Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
Settings user and group are ignored when systemd is used.
If you need to run mysqld under a different user or group,
customize your systemd unit file for mariadb according to the
instructions in http://fedoraproject.org/wiki/Systemd
#add server-id=2 #追加 read_only #追加 [mysqld_safe] ・・・ [/c] これも設定の記述する位置に注意が必要です。mysqld_safeに記述しないように注意が必要です。
- server-idはMySQLのホストを区別するための番号です。複数のスレーブを設置する場合、被らないように別々の数字を割り当ててください
- read_onlyはデーターベースを読み込み専用にするためのオプションです。スレーブに誤って更新クエリを行ってしまうとレプリケーションが止まってしまうため、それを防止する目的で設定します
MariaDBの再起動
スレーブ側のMariaDBが起動していた場合は再起動をしてください。もし起動していない場合は起動してください。 [c] #再起動コマンド [root@] # systemctl restart mariadb #起動コマンド [root@] # systemctl start mariadb [/c] となります。
マスターで書き出したデータベースの復元
[c] mysql --user=root > /tmp/dbdump.db [/c]
rootのパスワードの変更
[c] mysqladmin -u root password 'password' [/c] passwordの所に新しいパスワードをいれてください。
スレーブDBの設定
[c] mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2862 Server version: 5.5.47-MariaDB-log MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.0.2', MASTER_PORT=3306, MASTER_USER='repl', MASTER_PASSWORD='password', MASTER_LOG_FILE='binlog.000001', MASTER_LOG_POS=33424169; Query OK, 0 rows affected (0.25 sec) [/c]
スレーブをスタートします
[c] MariaDB [(none)]> START SLAVE; Query OK, 0 rows affected, 1 warning (0.01 sec) [/c]
状態の確認
レプリケーションが無事にできているか確認します。
マスター側
[c] mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2862 Server version: 5.5.47-MariaDB-log MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> SHOW MASTER STATUS; +---------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +---------------+----------+--------------+------------------+ | binlog.000001 | 55101492 | | | +---------------+----------+--------------+------------------+ [/c]
スレーブ側
[c] mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2862 Server version: 5.5.47-MariaDB-log MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> SHOW SLAVE STATUS\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.2 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000001 Read_Master_Log_Pos: 55101492 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 55071500 Relay_Master_Log_File: binlog.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes 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: 55101492 Relay_Log_Space: 55071796 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: 1 1 row in set (0.00 sec) [/c]
- Slave_IO_RunningとSlave_SQL_RunningがYesになっていること
- Master_Log_File と Read_Master_Log_Pos がマスターで確認したFile、Positionと一致していること
一致していればレプリケーション成功です。
