LoginSignup
5
5

More than 5 years have passed since last update.

RDSをAuroraのスレーブにする

Last updated at Posted at 2016-07-10

AuroraをRDSのスレーブにする話ではなく、RDSをAuroraのスレーブにする話です。

2つのAuroraクラスタを同期したお話」というLTを見て、うーむ…と思っていたら一応公式ドキュメントにbinlogを作ってレプリカを作る方法が書いてあるようでした

文章がわかりにくいので、実際にRDSをAuroraのスレーブにしてみました。

1. Auroraインスタンスを立ち上げる

DB Cluster Parameter Groupでbinlogを有効にして、Auroraインスタンスを起動します。

スクリーンショット 2016-07-10 11.47.11.png

立ち上がったら適当にテーブルを作ってデータを入れ続けます。

~$ mysql -h test.cluster-....rds.amazonaws.com -uroot test
mysql> create table test (id int primary key auto_increment , num int not null);
Query OK, 0 rows affected (0.06 sec)
~$ SQL='insert into test (num) values (1)'
~$ while true; do mysql -h test.cluster-....rds.amazonaws.com -uroot test -e "$SQL"; sleep 0.1; done

binlogが作成されていることを確認。

mysql> show master status;
+----------------------------+----------+--------------+------------------+-------------------+
| File                       | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------------+----------+--------------+------------------+-------------------+
| mysql-bin-changelog.000003 |    28576 |              |                  |                   |
+----------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

2. スナップショットをとる

起動したAuroraインスタンスのスナップショットをとります。

スクリーンショット 2016-07-10 12.04.22.png

3. スナップショットからダンプ用のAuroraをリストア

データをダンプするためのAuroraインスタンスをスナップショットからリストアします。

このときパラメーターグループを指定できないので起動したら速攻でbinlogが有効なDB Cluster Parameter Groupに変更・再起動します(なんで指定できないんだろう…)

スクリーンショット 2016-07-10 12.18.31.png

ダンプ用Auroraインスタンスを再起動したらbinlogを確認します。

~$ mysql -h test2-cluster.cluster-....rds.amazonaws.com -u root
mysql> show master logs;
+----------------------------+-----------+
| Log_name                   | File_size |
+----------------------------+-----------+
| mysql-bin-changelog.000001 |       120 |
| mysql-bin-changelog.000002 |       852 |
| mysql-bin-changelog.000003 |    481507 |
| mysql-bin-changelog.000004 |       120 |
| mysql-bin-changelog.000005 |       120 |
+----------------------------+-----------+
5 rows in set (0.00 sec)

mysql> show binlog events in 'mysql-bin-changelog.000003' from 481507 limit 3 \G
Empty set (0.01 sec)

binlogの末尾はmysql-bin-changelog.000003:481507…と。

念のため、スナップショット元のAuroraを確認。

mysql> show binlog events in 'mysql-bin-changelog.000003' from 481507 limit 3 \G
*************************** 1. row ***************************
   Log_name: mysql-bin-changelog.000003
        Pos: 481507
 Event_type: Query
  Server_id: 257197836
End_log_pos: 481586
       Info: BEGIN
*************************** 2. row ***************************
   Log_name: mysql-bin-changelog.000003
        Pos: 481586
 Event_type: Intvar
  Server_id: 257197836
End_log_pos: 481618
       Info: INSERT_ID=1931
*************************** 3. row ***************************
   Log_name: mysql-bin-changelog.000003
        Pos: 481618
 Event_type: Query
  Server_id: 257197836
End_log_pos: 481725
       Info: use `test`; insert into test (num) values (1)
3 rows in set (0.01 sec)

スナップショット直後にINSERT_ID=1931でインサートされていることがわかります。

今度はダンプ用のAuroraのtestテーブルを確認。

mysql> select max(id) from test;
+---------+
| max(id) |
+---------+
|    1930 |
+---------+
1 row in set (0.01 sec)

IDが1930で終わっていることが確認できました。

4. ダンプ用AuroraからダンプをとってRDSにリストア

ダンプ用Aurora適当にダンプをとります。

~$ mysqldump -h test2-cluster.cluster-....rds.amazonaws.com -u root -q test | gzip > test-db.sql.gz

適当なRDSのインスタンスを作成して、ダンプしたデータからDBをリストアします。

~$ gzcat test-db.sql.gz | mysql -h aurora-slave.....rds.amazonaws.com -u root test

~$ mysql -h aurora-slave.....rds.amazonaws.com -u root test

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test           |
+----------------+
1 row in set (0.01 sec)

mysql> select max(id) from test;
+---------+
| max(id) |
+---------+
|    1930 |
+---------+
1 row in set (0.00 sec)

mysql>

5. RDSでAuroraからのレプリケーションを設定

作成したRDSインスタンスで、スナップショット元のAuroraからのレプリケーションを設定します。
ポジションは先ほど確認したmysql-bin-changelog.000003:481507

mysql> CALL mysql.rds_set_external_master ('test.cluster-....rds.amazonaws.com', 3306, 'repl', 'repl', 'mysql-bin-changelog.000003', 481507, 0);
Query OK, 0 rows affected (0.04 sec)

mysql> CALL mysql.rds_start_replication;
+-------------------------+
| Message                 |
+-------------------------+
| Slave running normally. |
+-------------------------+
1 row in set (1.01 sec)

Query OK, 0 rows affected (1.01 sec)

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: test.cluster-....rds.amazonaws.com
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin-changelog.000003
          Read_Master_Log_Pos: 3963982
               Relay_Log_File: relaylog.000002
                Relay_Log_Pos: 239582
        Relay_Master_Log_File: mysql-bin-changelog.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table: mysql.plugin,mysql.rds_monitor,mysql.rds_sysinfo,mysql.rds_replication_status,mysql.rds_history,innodb_memcache.config_options,innodb_memcache.cache_policies
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 720796
              Relay_Log_Space: 3483216
              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: 2263
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: 257197836
                  Master_UUID: 1f365ea0-45b9-3c05-89a9-f168fd7fa515
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: System lock
           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
1 row in set (0.00 sec)

mysql> select id from test where id >= 1929 limit 5;
+------+
| id   |
+------+
| 1929 |
| 1930 |
| 1931 |
| 1932 |
| 1933 |
+------+
5 rows in set (0.01 sec)

これでRDSをAuroraのスレーブにすることができました。

5
5
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
5
5