LoginSignup
28
33

More than 5 years have passed since last update.

[MySQL][MariaDB] DBのレプリケーション構築手順

Last updated at Posted at 2016-01-02

前提

  • 単体のサーバーでDBが動いている状態から説明を進めます

マスターの操作

マスターの設定

レプリケーション用のユーザーを作成します

  • レプリケーションをユーザー"repl"で実行します
  • レプリケーションを192.168.0.0のネットワークから許可します
  • レプリケーションのパスワードを"password"にします

MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.0/255.255.255.0' IDENTIFIED BY 'password';

my.cnfに以下の設定を追加します


log-bin=mysql-bin
server-id=1
expire_logs_days=5
  • log-bin でレプリケーションに必要なバイナリログを有効にします
  • server-id はレプリケーションを構成するサーバー内で被らないように割り振ります
  • expire_logs_days はバイナリログの保存日数です。レプリケーションに間が空いた時に復旧できるようにする期間を指定します

マスターのダンプ

  1. rootユーザーでDBにアクセスします
    
    $ mysql -u root -p
    Enter password:
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 34
    
  2. 読み書きロックを行います
    
    MariaDB [(none)]> FLUSH TABLES WITH READ LOCK;
    Query OK, 0 rows affected (0.00 sec)
    
  3. バイナリログのFileとPositionを確認します
    
    MariaDB [(none)]> SHOW MASTER STATUS;
    +------------------+----------+--------------+------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +------------------+----------+--------------+------------------+
    | mysql-bin.000183 |    32579 |              |                  |
    +------------------+----------+--------------+------------------+
    1 row in set (0.00 sec)
    
  4. mysqldump でマスターのDBをファイルに書き出します
    
    $ mysqldump -u root -p --all-databases --lock-all-tables > /tmp/dbdump.db
    Enter password:
    
  5. 読み書きロックを解除します
    
    MariaDB [(none)]> UNLOCK TABLES;
    

スレーブの構築

DBの復元

  1. mysql_install_db を実行します
    
    # mysql_install_db --user=mysql
    
  2. my.cnf に以下の設定を追加します
    
    server-id=2
    read_only
    
    ・ server-idはMySQLのホストを区別するための番号です。複数のスレーブを設置する場合、被らないように別々の数字を割り当ててください
    ・ read_onlyはデーターベースを読み込み専用にするためのオプションです。スレーブに誤って更新クエリを行ってしまうとレプリケーションが止まってしまうため、それを防止する目的で設定します
  3. mysqldを起動します
    
    # mysqld_safe &
    
  4. マスターで書き出したデーターを復元します
    
    # mysql --user=root < /tmp/dbdump.db
    
  5. rootのパスワードを変更します
    
    # mysqladmin -u root password 'password'
    

スレーブの設定

  1. マスターのホスト、レプリケーションのユーザー、パスワード、マスターのダンプ手順で確認したバイナリログのFileとPositionを設定します
    
    MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.0.241', MASTER_PORT=3306, MASTER_USER='repl', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000183', MASTER_LOG_POS=32579;
    Query OK, 0 rows affected (0.25 sec)
    
  2. スレーブをSTARTします
    
    MariaDB [(none)]> START SLAVE;
    Query OK, 0 rows affected, 1 warning (0.01 sec)
    

※ MySQLのバージョンが5.6以上になると、CHANGE MASTER クエリで MASTER_PASSWORD を指定するとwarningが出るそうです

以下のようにSTART SLAVEクエリでレプリケーションのユーザーとパスワードを指定するようにします。


mysql56> CHANGE MASTER TO MASTER_HOST='192.168.0.241', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000183', MASTER_LOG_POS=32579;
Query OK, 0 rows affected (0.04 sec)

mysql56> START SLAVE USER='repl' PASSWORD='password';
Query OK, 0 rows affected, 1 warning (0.01 sec)

状態の確認

レプリケーションが稼働していることを確認する方法を記します

  1. マスターで"SHOW MASTER STATUS"クエリを実行し、
    バイナリログのFileとPositionを確認します

    
    MariaDB [mysql]> SHOW MASTER STATUS;
    +------------------+----------+--------------+------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +------------------+----------+--------------+------------------+
    | mysql-bin.000187 |    45208 |              |                  |
    +------------------+----------+--------------+------------------+
    1 row in set (0.00 sec)
    
  2. スレーブで"SHOW SLAVE STATUS"クエリを実行し、以下のことを確認します。

  • Slave_IO_RunningとSlave_SQL_RunningがYesになっていること
  • Master_Log_File と Read_Master_Log_Pos がマスターで確認したFile、Positionと一致していること

MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.241
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000187
          Read_Master_Log_Pos: 45208
               Relay_Log_File: mysql-slave-relay-bin.000006
                Relay_Log_Pos: 45495
        Relay_Master_Log_File: mysql-bin.000187
             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: 45208
              Relay_Log_Space: 46163
              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
               Master_SSL_Crl:
           Master_SSL_Crlpath:
                   Using_Gtid: No
                  Gtid_IO_Pos:
1 row in set (0.00 sec)

※ 表示が横に長くなるので"\G"を末尾につけてます

28
33
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
33