将RDS转化为Aurora的从库
不是关于使Aurora成为RDS的从属数据库,而是将RDS变成Aurora的从属数据库的讨论。
「2つのAuroraクラスタを同期したお話」というLTを見て、うーむ…と思っていたら一応公式ドキュメントにbinlogを作ってレプリカを作る方法が書いてあるようでした
- Aurora と MySQL との間、または Aurora と別の Aurora DB クラスターとの間のレプリケーション – Amazon Relational Database Service
由於文章不容易理解,因此我實際上將RDS作為Aurora的從屬實例。
1. 创建Aurora实例
通过在DB Cluster Parameter Group中启用binlog来启动Aurora实例。
我一站起来就会随便建立一张表格,并持续输入数据。
~$ 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实例进行快照。
3. スナップショットからダンプ用のAuroraをリストア
从快照中恢复Aurora实例以进行数据转储。
由于在这种情况下无法指定参数组,因此启动后会立即更改和重新启动为启用binlog的DB Cluster参数组。(为什么无法指定呢…)
重新启动用于转储的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测试表。
mysql> select max(id) from test;
+---------+
| max(id) |
+---------+
| 1930 |
+---------+
1 row in set (0.01 sec)
我确认了ID以1930结尾。
从Aurora数据库中进行导出,并将导出的数据恢复到RDS数据库中。
我将使用Aurora适用于垃圾清理来处理垃圾。
~$ mysqldump -h test2-cluster.cluster-....rds.amazonaws.com -u root -q test | gzip > test-db.sql.gz
创建适当的RDS实例,并从导出的数据中恢复数据库。
~$ 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 > 进入MySQL数据库。
在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的从库。