[Oracle Cloud] Amazon Aurora から MDS へ Inbound Replication してみた

# はじめに

[前の記事](https://qiita.com/sugimount/items/8ee44168935c620edabd)では、Oracle Cloud の MySQL マネージドサービス (MySQL Database Service) 同士の、Inbound Replication を検証しました。今回の記事では、AWS の Aurora (MySQL互換) から、Oracle Cloud の MDS へ Inbound Replication への接続を検証してみます。

Oracle Cloud の Document には明記されていない構成ですが、自分の検証した環境では正常に Inbound Replication が出来たので、その手順を残しておきます。

(RDS for MySQL は未検証です)



# 前提

IPsec VPN で AWS の VPC と OCI の VCN 間で、Private 接続済み
AWS VPC : 10.1.0.0/16
OCI VCN : 10.0.0.0/16



# Source : Security Group

Aurora に設定する Security Group を事前に用意しておきます。VPC と Oracle Cloud 間で 3306(MySQL) 接続を許可します。

![1611545507373.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/ecd0dacc-f6eb-951e-edd5-ae2ede616416.png)



# Source : RDS Prameter groups

Oracle Cloud の MDS では、GTID ベースのレプリケーションのみサポートされているので、Aurora では GTID を有効にしないといけません。Aurora の GTID レプリケーションは、version 2.04 以降の MySQL 5.7 互換でサポートされています。 

> GTID-based replication is supported for MySQL 5.7-compatible clusters in Aurora MySQL version 2.04 and later. GTID-based replication isn't supported for MySQL 5.6-compatible clusters in Aurora MySQL version 1.

https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/mysql-replication-gtid.html



Aurora で、GTID を有効にするには、Parameter group の作成が必要です。5.7 をベースにして、GTID を有効化する Parameter group を作成します。

![1611464541093.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/ac5f4c7e-5400-895f-d31a-67bf5cd83af6.png)



各パラメータを入れて、Create します。

![1612007645787.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/c5464b64-e9f0-36c6-3746-971c22a75a5a.png)



Edit

![1612053830029.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/87dbc9ee-f7c4-4b08-f18e-f9783f64ce06.png)



`gtid` で検索して、`enforce_gtid_consistency` と `gtid-mode` を ON にします。

![1612053925259.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/8025f847-53bf-abcc-8187-fcfcc1b89046.png)



binlog_format を `MIXED`から、`ROW` に変更

![1611739515688.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/3ec83b52-850b-d5ac-f49d-4858305ec5bd.png)



Save changes を押します。

![1611464768479.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/6b802431-d6dd-a475-209d-a7dfc2927460.png)



# Source : Aurora 作成

RDS のサービスで、Create database を押します。

![1611464839959.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/96504cf7-83a6-0188-79bf-5d6dd7d5b9f0.png)



各種パラメータ

- MySQL 5.7 互換の最新
- VPC の Private Subnet に設定
- Security Group を適切なものを設定
- RDS Public Access を無効
- DB parameter は、GTID を有効にしたものを設定

![1612054891153.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/497e5388-1ce9-4132-6afa-715eb073753c.png)



Aurora が作成開始されます。Aurora に書き込み・読み込み用の Endpoint が生成されています。

![1612054445726.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/6b966829-a60e-31d7-5b8e-413c917b7707.png)



2つの Endpoint の名前解決を確認します。書き込みも読み込みも両方とも、同一のIPアドレス `10.1.100.113` を指しています。

書き込み Endpoint

```
[opc@onprem-server01 ~]$ dig aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com A | grep "ANSWER SECTION" -A2
;; ANSWER SECTION:
aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 3 IN CNAME aurora-source-01-instance-1.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com.
aurora-source-01-instance-1.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 3 IN A 10.1.100.113
```



読み込み Endpoint

```
[opc@onprem-server01 ~]$ dig aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com A | grep "ANSWER SECTION" -A2
;; ANSWER SECTION:
aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 1 IN CNAME aurora-source-01-instance-1.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com.
aurora-source-01-instance-1.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 5 IN A 10.1.100.113
```



`10.1.100.113` は、Aurora 配下の Primary Instance です。

![1612055512036.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/2d1476f8-3701-5925-022f-a2a243be9986.png)



Primary Instance の IP アドレスを確認

```
[opc@onprem-server01 ~]$ dig aurora-source-01-instance-1.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com A | grep "ANSWER SECTION" -A2
;; ANSWER SECTION:
aurora-source-01-instance-1.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 5 IN A 10.1.100.113
```



RDS 以外のサーバーから、nc コマンドを使って、3306 ポートにアクセス確認をします。書き込みエンドポイント、読み込みエンドポイントの両方にアクセスしてみます。何やら文字列が返ってくれば、3306 ポートで通信出来ていることがわかります。

書き込み Endpoint

```
[opc@onprem-server01 ~]$ nc aurora-source01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com 3306
N
5.7.12-log|xl9%q���%gUIomysql_native_password^C
[opc@onprem-server01 ~]$
```



読み込み Endpoint

```
[opc@onprem-server01 ~]$ nc aurora-source01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com 3306
N
5.7.12-log0/x
i9b���q5(XHo#-qmysql_native_password
^C
[opc@onprem-server01 ~]$
```



実際に MySQL Client で接続します。

```
mysql -u admin -h aurora-source01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com -p
```



接続例

```
[opc@onprem-server01 ~]$ mysql -u admin -h aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.12-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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



GTID Mode が有効化されています

```
mysql> show variables like '%gtid%';
+----------------------------------+-----------+
| Variable_name                    | Value     |
+----------------------------------+-----------+
| binlog_gtid_simple_recovery      | ON        |
| enforce_gtid_consistency         | ON        |
| gtid_executed                    |           |
| gtid_executed_compression_period | 1000      |
| gtid_mode                        | ON        |
| gtid_next                        | AUTOMATIC |
| gtid_owned                       |           |
| gtid_purged                      |           |
| session_track_gtids              | OFF       |
+----------------------------------+-----------+
9 rows in set (0.01 sec)

mysql>
```



binlog_format も ROW になっています。

```
mysql> show variables like 'binlog_format';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+
1 row in set (0.01 sec)

mysql>
```



# Source : スキーマ作成

データ移行用のスキーマを作成します

```
create database sugitest;
```



無事作成されています

```
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sugitest           |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql>
```





# Source : レプリケーションユーザーの設定

Aurora と MDS 間でレプリケーションするためのユーザーを作成します。Source 側の Aurora でユーザーを作成します。

```
CREATE USER rpluser001@'%' IDENTIFIED BY 'Rpl001#!';
```



REPLICATION SLAVE の権限付与

```
GRANT REPLICATION SLAVE on *.* to rpluser001@'%';
```



# Source : Dump ファイルを Export

Aurora から Dump ファイルを取得するために、Aurora に MySQL Shell で接続します。(MySQL Shell ではなくて、mysqldump で取得しても大丈夫です。)

```
mysqlsh admin@aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com
```



dumpInstance コマンドで、Oracle Cloud Object Storage に Aurora の DumpFile を出力します。 

```
util.dumpInstance("RDStoMDS15", {osBucketName: "mysqldump", osNamespace: "nrefeaanptgn", threads: 4, ocimds: true, compatibility: ["strip_restricted_grants"]})
```



実行例

```
 MySQL  aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com:3306 ssl  JS > util.dumpInstance("RDStoMDS15", {osBucketName: "mysqldump", osNamespace: "nrefeaanptgn", threads: 4, ocimds: true, compatibility: ["strip_restricted_grants"]})
Acquiring global read lock
NOTE: Error acquiring global read lock: MySQL Error 1045 (28000): Access denied for user 'admin'@'%' (using password: YES)
WARNING: The current user lacks privileges to acquire a global read lock using 'FLUSH TABLES WITH READ LOCK'. Falling back to LOCK TABLES...
Locking instance for backup
NOTE: Backup lock is not supported in MySQL 5.7 and DDL changes will not be blocked. The dump may fail with an error or not be completely consistent if schema changes are made while dumping.
Table locks acquired
Gathering information - done
All transactions have been started
WARNING: The dumped value of gtid_executed is not guaranteed to be consistent
Global read lock has been released
Checking for compatibility with MySQL Database Service 8.0.23
NOTE: MySQL Server 5.7 detected, please consider upgrading to 8.0 first. You can check for potential upgrade issues using util.checkForServerUpgrade().
NOTE: User 'admin'@'%' had restricted privileges (INVOKE COMPREHEND, INVOKE LAMBDA, INVOKE SAGEMAKER, LOAD FROM S3, RELOAD, SELECT INTO S3) removed
NOTE: User 'rdsadmin'@'localhost' had restricted privileges (CREATE TABLESPACE, FILE, RELOAD, SHUTDOWN, SUPER) removed
Compatibility issues with MySQL Database Service 8.0.23 were found and repaired. Please review the changes made before loading them.
Writing global DDL files
Writing users DDL
Running data dump using 4 threads.
Writing DDL for schema `sugitest`
NOTE: Progress information uses estimated values and may not be accurate.
?% (0 rows / ?), 0.00 rows/s, 0.00 B/s uncompressed, 0.00 B/s compressed
Duration: 00:00:01s
Schemas dumped: 1
Tables dumped: 0
Uncompressed data size: 0 bytes
Compressed data size: 0 bytes
Compression ratio: 0.0
Rows written: 0
Bytes written: 0 bytes
Average uncompressed throughput: 0.00 B/s
Average compressed throughput: 0.00 B/s
 MySQL  aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com:3306 ssl  JS >
```



Oracle Cloud の Object Storageに出力されている

![1612056129520.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/3164aaa5-b3b8-cdb6-b4dd-2ee31891289a.png)



# Replica : MDS 作成

レプリケーション先として、Oracle Cloud の MDS を作成します。

![1611546582108.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/b74f4b58-8f98-3f92-30bd-e6a883d7afcf.png)



各種パラメータを入れて、Next を押します

![1612056431274.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/fb3f6c7d-9534-9502-e10f-17709eb3f741.png)



各種パラメータを入れて、Next を押します。

![1612056501235.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/2b0bf98c-1a43-d020-de28-b74e56aca519.png)



Create

![1612056549977.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/f89b9000-42db-a241-0e03-ce2716b985e0.png)



Creating となり、約15分後に Available に変化します。

![1612057356982.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/73c616dc-f3f5-5128-8890-d605c9d2056c.png)



MDS が所属する Subnet で、3306 を Open にしておきます。

![1611556665734.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/8b211241-e3ee-efdb-1ff5-4df4be1ec910.png)



# Replica : Dumpfile を Import

Replica 用 MDS に Dumpfile を Import します。MySQL Shell で接続します。

```
mysqlsh admin@aurora-replica-01.privatesubnet01.onpremis.oraclevcn.com
```



Dumpfile を Import します

- ignoreVersion を true

```
util.loadDump("RDStoMDS15", {osBucketName: "mysqldump", osNamespace: "nrefeaanptgn", threads: 4, ignoreVersion: true})
```



実行例

```
 MySQL  aurora-replica-01.privatesubnet01.onpremis.oraclevcn.com:33060+ ssl  JS > util.loadDump("RDStoMDS15", {osBucketName: "mysqldump", osNamespace: "nrefeaanptgn", threads: 4, ignoreVersion: true})
Loading DDL and Data from OCI ObjectStorage bucket=mysqldump, prefix='RDStoMDS15' using 4 threads.
Opening dump...
Target is MySQL 8.0.23-cloud (MySQL Database Service). Dump was produced from MySQL 5.7.12-log
WARNING: Destination MySQL version is newer than the one where the dump was created. Loading dumps from different major MySQL versions is not fully supported and may not work. The 'ignoreVersion' option is enabled, so loading anyway.
Fetching dump data from remote location...
Fetching 0 table metadata files for schema `sugitest`...
Checking for pre-existing objects...
Executing common preamble SQL
Executing DDL script for schema `sugitest`
Executing common postamble SQL

No data loaded.
0 warnings were reported during the load.
 MySQL  aurora-replica-01.privatesubnet01.onpremis.oraclevcn.com:33060+ ssl  JS >
```



GTID ベースのレプリケーションでは、どこまでトランザクションをレプリケーションしたかを、GTID で管理しています。MySQL Shell のユーティリティでは、Replica 側で、GTID 関連の設定が必要です。
Object Storage 内の json メタデータから、`gtidExecuted`を取得してメモします。

Object Storage の`@.json` ファイルをダウンロードします。

![1612056750884.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/6fcd2ec4-9f70-9e48-5815-162d0b0f00dd.png)



この `@.json` ファイルの中身は以下。この中の `gtidExecuted` をメモっておく

```
{
    "dumper": "mysqlsh Ver 8.0.23 for Linux on x86_64 - for MySQL 8.0.23 (MySQL Community Server (GPL))",
    "version": "1.0.2",
    "origin": "dumpInstance",
    "schemas": [
        "sugitest"
    ],
    "basenames": {
        "sugitest": "sugitest"
    },
    "users": [
        "'admin'@'%'",
        "'rdsadmin'@'localhost'",
        "'rpluser001'@'%'"
    ],
    "defaultCharacterSet": "utf8mb4",
    "tzUtc": true,
    "bytesPerChunk": 64000000,
    "user": "admin",
    "hostname": "onprem-server01",
    "server": "ip-172-23-1-47",
    "serverVersion": "5.7.12-log",
    "gtidExecuted": "bc4ffe98-cad6-372e-a768-51115c2b1e87:1-5",
    "gtidExecutedInconsistent": true,
    "consistent": true,
    "mdsCompatibility": true,
    "begin": "2021-01-31 01:21:11"
}
```



メモった値を使って、MDS 側で gtid_purged を行います。MDS に MySQL Client (MySQL Shell じゃないよ) を使って接続します。

```
mysql -u admin -h aurora-replica-01.privatesubnet01.onpremis.oraclevcn.com -p
```



gtid_purged が空白なことを確認します

```
mysql> select @@gtid_purged;
+---------------+
| @@gtid_purged |
+---------------+
|               |
+---------------+
1 row in set (0.00 sec)

mysql>
```



別の変数でも確認します。

```
mysql> show global variables like 'GTID%';
+----------------------------------+----------------------------------------+
| Variable_name                    | Value                                  |
+----------------------------------+----------------------------------------+
| gtid_executed                    | efd1edbe-6364-11eb-a0b6-020017015d95:1 |
| gtid_executed_compression_period | 0                                      |
| gtid_mode                        | ON                                     |
| gtid_owned                       |                                        |
| gtid_purged                      |                                        |
+----------------------------------+----------------------------------------+
5 rows in set (0.00 sec)

mysql>
```



メモって来た値を使って、`sys.set_gtid_purged` を発行します。

実行例

```
mysql> call sys.set_gtid_purged("bc4ffe98-cad6-372e-a768-51115c2b1e87:1-5");
Query OK, 0 rows affected (0.00 sec)

mysql>
```



確認1

```
mysql> select @@gtid_purged;
+------------------------------------------+
| @@gtid_purged                            |
+------------------------------------------+
| bc4ffe98-cad6-372e-a768-51115c2b1e87:1-5 |
+------------------------------------------+
1 row in set (0.00 sec)

mysql>
```



確認2

```
mysql> show global variables like 'GTID%';
+----------------------------------+----------------------------------------------------------------------------------+
| Variable_name                    | Value                                                                            |
+----------------------------------+----------------------------------------------------------------------------------+
| gtid_executed                    | bc4ffe98-cad6-372e-a768-51115c2b1e87:1-5,
efd1edbe-6364-11eb-a0b6-020017015d95:1 |
| gtid_executed_compression_period | 0                                                                                |
| gtid_mode                        | ON                                                                               |
| gtid_owned                       |                                                                                  |
| gtid_purged                      | bc4ffe98-cad6-372e-a768-51115c2b1e87:1-5                                         |
+----------------------------------+----------------------------------------------------------------------------------+
5 rows in set (0.00 sec)

mysql>
```



# Replica : Channel の作成

OCI Console 上で、レプリケーションを行うために、Create Channel を行います。

![1612057662780.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/468044f7-5efa-8f1e-6799-5cae4ac8a228.png)



Source で作成したレプリケーション用のユーザーを指定します

```
aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com

rpluser001
Rpl001#!
SSL無し
```



Create Channel

![1612057754617.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/0be1aa0e-7a60-6b27-05bb-69f1e52c8a95.png)



Create Channel

![1612057795251.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/688d7d01-def5-cad5-0f75-8350c5ab7fda.png)



Creating になります。

![1612057811198.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/ef0b844d-aed0-bcfc-8738-6d917eee5161.png)



無事にレプリケーションが構成されると、Actice Status に変わります。

![1612057931712.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/229d6c77-851c-d840-a843-db9cfba35c9d.png)



正常時の SHOW SLAVE STATUS の結果はこんな感じです。レプリケーションがエラーになったときには、このコマンドでエラーメッセージを拾えるので、見てみましょう。

```
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com
                  Master_User: rpluser001
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin-changelog.000003
          Read_Master_Log_Pos: 1113
               Relay_Log_File: relay-log-replication_channel.000002
                Relay_Log_Pos: 508
        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:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1113
              Relay_Log_Space: 731
              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: 5974158
                  Master_UUID: bc4ffe98-cad6-372e-a768-51115c2b1e87
             Master_Info_File: mysql.slave_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: 0
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set: bc4ffe98-cad6-372e-a768-51115c2b1e87:1-5,
efd1edbe-6364-11eb-a0b6-020017015d95:1
                Auto_Position: 1
         Replicate_Rewrite_DB:
                 Channel_Name: replication_channel
           Master_TLS_Version: TLSv1.2,TLSv1.3
       Master_public_key_path:
        Get_master_public_key: 1
            Network_Namespace: mysql
1 row in set, 1 warning (0.00 sec)

mysql>
```



# レプリケーション確認

レプリケーションされているか確認するために、Source である Aurora にテストデータを入れてみます

```
mysql -u admin -h aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com -p
```



レプリケーション元の Aurora で、適当にテーブルを作成します。

```
use sugitest;

CREATE TABLE sample(
 id INT(11) NOT NULL AUTO_INCREMENT,
 value INT(5) NOT NULL DEFAULT 0,
 PRIMARY KEY (id)
);

INSERT INTO sample(value) VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
```



Aurora 上でテストデータが生成されています。

```
mysql> select * from sugitest.sample;
+----+-------+
| id | value |
+----+-------+
|  1 |     1 |
|  2 |     2 |
|  3 |     3 |
|  4 |     4 |
|  5 |     5 |
|  6 |     6 |
|  7 |     7 |
|  8 |     8 |
|  9 |     9 |
| 10 |    10 |
+----+-------+
10 rows in set (0.00 sec)

mysql>
```



replica 先である MDS へデータがコピーされているか確認します

```
mysql -h aurora-replica-01.privatesubnet01.onpremis.oraclevcn.com -u admin -p
```



無事にコピーされています

```
mysql> select * from sugitest.sample;
+----+-------+
| id | value |
+----+-------+
|  1 |     1 |
|  2 |     2 |
|  3 |     3 |
|  4 |     4 |
|  5 |     5 |
|  6 |     6 |
|  7 |     7 |
|  8 |     8 |
|  9 |     9 |
| 10 |    10 |
+----+-------+
10 rows in set (0.00 sec)

mysql>
```



# Aurora の各種イベント発生検証

Aurora 側で、フェールオーバーや Aurora Replica の追加など、各種イベントが発生します。MDS によるレプリケーションがこれらに追従し、継続してレプリケーションされるか確認します。



## Aurora Replica の追加

まず、Aurora は今は Single-AZ 構成になっているので、Multi-AZ 構成にしてみます。読み込み用の Endpoint の FQDN は変わりませんが、Aレコードが指し示すIPアドレスが変化するので、それに MDS のレプリケーションが追従するか確認します。



Singile-AZ の時の Read Endpoint です。`10.1.100.113` を指しています。

```
[opc@onprem-server01 ~]$ dig aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com A | grep "ANSWER SECTION" -A2
;; ANSWER SECTION:
aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 30 IN CNAME aurora-source-01-instance-1.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com.
aurora-source-01-instance-1.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 30 IN A 10.1.100.113
```



ついでに、関係ないですが、Write Endpoint も確認します。`10.1.100.113` を指しています。

```
[opc@onprem-server01 ~]$ dig aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com A | grep "ANSWER SECTION" -A2
;; ANSWER SECTION:
aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 30 IN CNAME aurora-source-01-instance-1.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com.
aurora-source-01-instance-1.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 30 IN A 10.1.100.113
```



Aurora の構成上でも、Multi-AZ が No になっています。

![1612059186100.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/58584b40-25e2-6549-2a3d-475bea0434d4.png)



Add reader を押して、Aurora Replica を追加して、Multi-AZ 構成にします。

![1612058261295.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/ad3c7a00-8351-acff-dcb9-16eaf4d9e4d1.png)



名前など適当にいれて、Aurora Replica を構成します。

![1612058517798.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/2c882de1-bb4a-2ab1-ab3b-18bafb81b8cc.png)



Creating になります。

![1612058547510.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/ac84bcda-dd30-422f-2653-531940de2ba1.png)



Aurora クラスター上でも、Multi-AZ が 2 Zones になっています。

![1612059238792.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/cb2a5bb8-5d49-b353-9511-2b396bb17c64.png)



読み込み Endpoint が、`10.1.100.113`から `10.1.101.130` に切り替わっています。 (書き込み Endpoint は変化なし)

```
[opc@onprem-server01 ~]$ dig +dnssec @8.8.4.4 aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com A | grep "ANSWER SECTION" -A2
;; ANSWER SECTION:
aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 0 IN CNAME aurora-source-01-instance-2.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com.
aurora-source-01-instance-2.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN A 10.1.101.130
```



これは、追加した Aurora Replica の Instance IP アドレスに変化しています。

```
[opc@onprem-server01 ~]$ dig +dnssec @8.8.4.4 aurora-source-01-instance-2.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com A | grep "ANSWER SECTION" -A2;; ANSWER SECTION:
aurora-source-01-instance-2.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN A 10.1.101.130
```



追加した Aurora Replica の Endpoint

![1612059398701.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/f1b405c4-bd62-5527-42fa-0aa082c346dc.png)



では、MDS からのレプリケーション先が指し占めす IP アドレスが変化しましたが、継続してレプリケーションされているか確認します。

Aurora にテストデータを書き込みます。

```
mysql> INSERT INTO sugitest.sample(value) VALUES (11);
Query OK, 1 row affected (0.01 sec)

mysql>
```



MDS へ正常にレプリケーションされています。

```
mysql> SELECT * FROM sugitest.sample;
+----+-------+
| id | value |
+----+-------+
|  1 |     1 |
|  2 |     2 |
|  3 |     3 |
|  4 |     4 |
|  5 |     5 |
|  6 |     6 |
|  7 |     7 |
|  8 |     8 |
|  9 |     9 |
| 10 |    10 |
| 11 |    11 |
+----+-------+
11 rows in set (0.00 sec)

mysql>
```



SHOW SLAVE STATUS コマンドでも、異常ありません。

```
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com
                  Master_User: rpluser001
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin-changelog.000003
          Read_Master_Log_Pos: 1992
               Relay_Log_File: relay-log-replication_channel.000002
                Relay_Log_Pos: 1387
        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:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1992
              Relay_Log_Space: 1610
              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: 5974158
                  Master_UUID: bc4ffe98-cad6-372e-a768-51115c2b1e87
             Master_Info_File: mysql.slave_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: 0
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set: bc4ffe98-cad6-372e-a768-51115c2b1e87:6-8
            Executed_Gtid_Set: bc4ffe98-cad6-372e-a768-51115c2b1e87:1-8,
efd1edbe-6364-11eb-a0b6-020017015d95:1
                Auto_Position: 1
         Replicate_Rewrite_DB:
                 Channel_Name: replication_channel
           Master_TLS_Version: TLSv1.2,TLSv1.3
       Master_public_key_path:
        Get_master_public_key: 1
            Network_Namespace: mysql
1 row in set, 1 warning (0.01 sec)

mysql>
```





## 手動 Failover

では、Aurora が Multi-AZ になったので、手動で Failover をしてみます。その前に、DNS レコードで Endpoint を確認してみましょう。

書き込み Endpoint

```
[opc@onprem-server01 ~]$ dig +dnssec @8.8.4.4 aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com A | grep "ANSWER SECTION" -A2
;; ANSWER SECTION:
aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN CNAME aurora-source-01-instance-1.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com.
aurora-source-01-instance-1.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN A 10.1.100.113
```

読み込み Endpoint

```
[opc@onprem-server01 ~]$ dig +dnssec @8.8.4.4 aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com A | grep "ANSWER SECTION" -A2
;; ANSWER SECTION:
aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 0 IN CNAME aurora-source-01-instance-2.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com.
aurora-source-01-instance-2.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN A 10.1.101.130
```



それでは、Failover 操作をします。Endpoint が指し示す IP アドレスが入れ替わります。

![1612059848944.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/bb133fb0-1892-e7bd-afc7-20f110d63fcd.png)



Failover

![1612015101992.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/191ad07c-f75e-1815-e802-ed39533a7f03.png)



約1分後、Failover がされており、Writer と Reader が入れ替わっています

![1612059924084.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/355d413d-15c5-1c38-d352-a07ab8a746d7.png)



DNSも切り替わっています。

書き込み

```
[opc@onprem-server01 ~]$ dig +dnssec @8.8.4.4 aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com A | grep "ANSWER SECTION" -A2
;; ANSWER SECTION:
aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN CNAME aurora-source-01-instance-2.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com.
aurora-source-01-instance-2.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN A 10.1.101.130
```



読み込み

```
[opc@onprem-server01 ~]$ dig +dnssec @8.8.4.4 aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com A | grep "ANSWER SECTION" -A2
;; ANSWER SECTION:
aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 0 IN CNAME aurora-source-01-instance-1.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com.
aurora-source-01-instance-1.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN A 10.1.100.113
```



では、レプリケーションが正常に構成されているか、Aurora でテストデータを書き込みます。

```
INSERT INTO sugitest.sample(value) VALUES (12);
```



MDS 側へ、正常にレプリケーションされています。

```
mysql> SELECT * FROM sugitest.sample;
+----+-------+
| id | value |
+----+-------+
|  1 |     1 |
|  2 |     2 |
|  3 |     3 |
|  4 |     4 |
|  5 |     5 |
|  6 |     6 |
|  7 |     7 |
|  8 |     8 |
|  9 |     9 |
| 10 |    10 |
| 11 |    11 |
| 12 |    12 |
+----+-------+
12 rows in set (0.00 sec)

mysql>
```



SHOW SLAVE STATUS コマンドでも、異常ありません。

```
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com
                  Master_User: rpluser001
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin-changelog.000004
          Read_Master_Log_Pos: 518
               Relay_Log_File: relay-log-replication_channel.000003
                Relay_Log_Pos: 753
        Relay_Master_Log_File: mysql-bin-changelog.000004
             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: 518
              Relay_Log_Space: 2207
              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: 5974158
                  Master_UUID: bc4ffe98-cad6-372e-a768-51115c2b1e87
             Master_Info_File: mysql.slave_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: 0
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set: bc4ffe98-cad6-372e-a768-51115c2b1e87:6-9
            Executed_Gtid_Set: bc4ffe98-cad6-372e-a768-51115c2b1e87:1-9,
efd1edbe-6364-11eb-a0b6-020017015d95:1
                Auto_Position: 1
         Replicate_Rewrite_DB:
                 Channel_Name: replication_channel
           Master_TLS_Version: TLSv1.2,TLSv1.3
       Master_public_key_path:
        Get_master_public_key: 1
            Network_Namespace: mysql
1 row in set, 1 warning (0.00 sec)

mysql>
```



## Aurora Relica の削除

読み込み処理を提供している、Aurora Replica を削除します。DNS レコードで Endpoint を確認してみましょう。

書き込み Endpoint の確認

```
[opc@onprem-server01 ~]$ dig +dnssec @8.8.4.4 aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com A | grep "ANSWER SECTION" -A2
;; ANSWER SECTION:
aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN CNAME aurora-source-01-instance-2.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com.
aurora-source-01-instance-2.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN A 10.1.101.130
```



読み込み Endpoint の確認

```
[opc@onprem-server01 ~]$ dig +dnssec @8.8.4.4 aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com A | grep "ANSWER SECTION" -A2
;; ANSWER SECTION:
aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 0 IN CNAME aurora-source-01-instance-1.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com.
aurora-source-01-instance-1.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN A 10.1.100.113
```



Aurora Replica の Delete

![1612060336558.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/561f8353-f98d-0b1e-2e68-8e672a075881.png)



delete me

![1612015799746.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/f100c7ff-97f4-95cd-2fe2-2a2fc542627b.png)



Deleting 

![1612060370469.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/9030f76d-3088-fcf9-0323-4305c69ad9cf.png)



書き込み Endpoint は変化ありません。

```
[opc@onprem-server01 ~]$ dig +dnssec @8.8.4.4 aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com A | grep "ANSWER SECTION" -A2
;; ANSWER SECTION:
aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN CNAME aurora-source-01-instance-2.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com.
aurora-source-01-instance-2.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN A 10.1.101.130
```



読み込み Endpoint は、Aurora Replica が削除されたことにより、`10.1.100.113` から `10.1.101.130` へ変わっています。

```
[opc@onprem-server01 ~]$ dig +dnssec @8.8.4.4 aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com A | grep "ANSWER SECTION" -A2
;; ANSWER SECTION:
aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 0 IN CNAME aurora-source-01-instance-2.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com.
aurora-source-01-instance-2.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN A 10.1.101.130
```



Aurora に書き込みます。

```
mysql> INSERT INTO sugitest.sample(value) VALUES (13);
Query OK, 1 row affected (0.01 sec)

mysql>
```



MDS へ正常にレプリケーションされています。

```
mysql> SELECT * FROM sugitest.sample;
+----+-------+
| id | value |
+----+-------+
|  1 |     1 |
|  2 |     2 |
|  3 |     3 |
|  4 |     4 |
|  5 |     5 |
|  6 |     6 |
|  7 |     7 |
|  8 |     8 |
|  9 |     9 |
| 10 |    10 |
| 11 |    11 |
| 12 |    12 |
| 13 |    13 |
+----+-------+
13 rows in set (0.00 sec)

mysql>
```



## 再度、Aurora Replica の追加

Single-AZ 構成に戻ったので、後の検証のために、Aurora Replica を再度追加します。

![1612061162364.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/1ff63f3c-79f6-8b53-3447-722841ee3ce1.png)



## Primary Instance を削除

では次に、Aurora の Primary Instance を削除してみます。DNS レコードで Endpoint を確認してみましょう。

書き込み Endpoint

```
[opc@onprem-server01 ~]$ dig +dnssec @8.8.4.4 aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com A | grep "ANSWER SECTION" -A2
;; ANSWER SECTION:
aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN CNAME aurora-source-01-instance-2.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com.
aurora-source-01-instance-2.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN A 10.1.101.130
```



読み込み Endpoint

```
[opc@onprem-server01 ~]$ dig +dnssec @8.8.4.4 aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com A | grep "ANSWER SECTION" -A2
;; ANSWER SECTION:
aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 0 IN CNAME aurora-source-01-instance-3.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com.
aurora-source-01-instance-3.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN A 10.1.100.110
```



Primary Instance を削除

![1612061253215.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/5a06b868-216b-679c-ee31-2642100ccf63.png)



delete

![1612017028808.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/7e397b8a-982f-20e0-6086-104f17e738f4.png)



Deleting

![1612061277950.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/104247/73b5daf1-961b-560b-193b-bea17371a2a2.png)



MDS の `SHOW SLAVE STATUS\G`を確認すると、connecting となっており、再度接続をしています。これは、Failover や Aurora Replica を追加していることにより、Writer の IP アドレスをレプリケーション先として、MDS が保持していたと思われます。

```
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Reconnecting after a failed master event read
                  Master_Host: aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com
                  Master_User: rpluser001
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin-changelog.000004
          Read_Master_Log_Pos: 1042
               Relay_Log_File: relay-log-replication_channel.000003
                Relay_Log_Pos: 1277
        Relay_Master_Log_File: mysql-bin-changelog.000004
             Slave_IO_Running: Connecting
            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: 1042
              Relay_Log_Space: 2731
              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: 2003
                Last_IO_Error: error reconnecting to master 'rpluser001@aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com:3306' - retry-time: 60 retries: 1 message: Can't connect to MySQL server on 'aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com' (111)
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 5974158
                  Master_UUID: bc4ffe98-cad6-372e-a768-51115c2b1e87
             Master_Info_File: mysql.slave_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: 0
                  Master_Bind:
      Last_IO_Error_Timestamp: 210131 02:48:20
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set: bc4ffe98-cad6-372e-a768-51115c2b1e87:6-11
            Executed_Gtid_Set: bc4ffe98-cad6-372e-a768-51115c2b1e87:1-11,
efd1edbe-6364-11eb-a0b6-020017015d95:1
                Auto_Position: 1
         Replicate_Rewrite_DB:
                 Channel_Name: replication_channel
           Master_TLS_Version: TLSv1.2,TLSv1.3
       Master_public_key_path:
        Get_master_public_key: 1
            Network_Namespace: mysql
1 row in set, 1 warning (0.01 sec)

mysql>
```



MDS のレプリケーションは、読み込みエンドポイントを指定しているため、すぐに再接続されます。

```
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com
                  Master_User: rpluser001
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin-changelog.000005
          Read_Master_Log_Pos: 256
               Relay_Log_File: relay-log-replication_channel.000004
                Relay_Log_Pos: 491
        Relay_Master_Log_File: mysql-bin-changelog.000005
             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: 256
              Relay_Log_Space: 1835
              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: 5974158
                  Master_UUID: bc4ffe98-cad6-372e-a768-51115c2b1e87
             Master_Info_File: mysql.slave_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: 0
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set: bc4ffe98-cad6-372e-a768-51115c2b1e87:6-11
            Executed_Gtid_Set: bc4ffe98-cad6-372e-a768-51115c2b1e87:1-11,
efd1edbe-6364-11eb-a0b6-020017015d95:1
                Auto_Position: 1
         Replicate_Rewrite_DB:
                 Channel_Name: replication_channel
           Master_TLS_Version: TLSv1.2,TLSv1.3
       Master_public_key_path:
        Get_master_public_key: 1
            Network_Namespace: mysql
1 row in set, 1 warning (0.00 sec)

mysql>
```



書き込み Endpoint が、IP アドレスが変わっています。

```
[opc@onprem-server01 ~]$ dig +dnssec @8.8.4.4 aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com A | grep "ANSWER SECTION" -A2
;; ANSWER SECTION:
aurora-source-01.cluster-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN CNAME aurora-source-01-instance-3.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com.
aurora-source-01-instance-3.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN A 10.1.100.110
```



読み込み  Endpoint

```
[opc@onprem-server01 ~]$ dig +dnssec @8.8.4.4 aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com A | grep "ANSWER SECTION" -A2
;; ANSWER SECTION:
aurora-source-01.cluster-ro-comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 0 IN CNAME aurora-source-01-instance-3.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com.
aurora-source-01-instance-3.comulzzpkhpz.ap-northeast-1.rds.amazonaws.com. 4 IN A 10.1.100.110
```



Aurora にテストデータを書き込み

```
mysql> INSERT INTO sugitest.sample(value) VALUES (15);
Query OK, 1 row affected (0.02 sec)

mysql>
```



MDS 側で正常にレプリケーションされています

```
mysql> SELECT * FROM sugitest.sample;
+----+-------+
| id | value |
+----+-------+
|  1 |     1 |
|  2 |     2 |
|  3 |     3 |
|  4 |     4 |
|  5 |     5 |
|  6 |     6 |
|  7 |     7 |
|  8 |     8 |
|  9 |     9 |
| 10 |    10 |
| 11 |    11 |
| 12 |    12 |
| 13 |    13 |
| 14 |    14 |
| 15 |    15 |
+----+-------+
15 rows in set (0.00 sec)

mysql>
```



# 参考URL

Using GTID-based replication for Amazon RDS MySQL
https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/mysql-replication-gtid.html

https://aws.amazon.com/jp/premiumsupport/knowledge-center/rds-cannot-connect/

MySQL ShellのdumpInstance()、dumpSchemas()をAWS RDS＆AuroraMySQLに対して使用する
https://atsuizo.hatenadiary.jp/entry/2020/09/01/090000

https://dev.mysql.com/doc/mysql-shell/8.0/en/mysql-shell-utilities-load-dump.html