目标:从自建MySQL数据库迁移到Amazon Aurora并要求最小停机时间
如果客户是在EC2或者本地数据中心自己部署的MySQL数据库,希望迁移到Amazon Aurora数据库,同时没有足够的停机时间来通过备份恢复的方式完成整个迁移。这种场景相对比较复杂,采用binlog的方式同步。
创建Aurora从库(即读副本),通过binlog与MySQL主库保持同步,如下
图片来自AWS博客1.自建MySQL,并修改相关my.cnf文件的配置信息
2.MySQL为主,Aurora为从的关系建立起来以后,持续进行数据同步。在这个阶段,Aurora数据库只能进行读操作,不能进行写操作,可以把Aurora的read_only参数设置为1强制只读,或者也可以保持0,而是从应用程序端进行控制,禁止其在Aurora数据库里进行写操作。
3.当需要进行切换的时候,也就是业务低谷的时候,停止应用程序在源MySQL数据库里的写入操作,然后等到Aurora从库的数据与MySQL主库的数据完全一致以后,修改应用程序的连接字符串,使其指向Aurora从库,使得Aurora数据库变为主要的写入数据库(如果你之前把Aurora数据库的read_only设置为了1,则需要把其改回到0,从而允许写入Aurora数据库)。而原来的MySQL数据库则可以被销毁。
具体步骤:
1.在MySQL上启用binlog日志
1.1> 停止mysqld服务
[root@ip-172-30-0-56 ~]# systemctl stop mysqld.service
1.2> 修改my.cnf文件
[root@ip-172-30-0-56 ~]#vim /etc/my.cnf
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
log-bin=mysql-bin
server-id=100
innodb_flush_log_at_trx_commit=1
sync_binlog=1
1.3> 配置完成后,启动mysqld服务
[root@ip-172-30-0-56 ~]# systemctl start mysqld.service
1.4> 登录Mysql并查看master状态
mysql> SHOW MASTER STATUS;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 2
Current database: sakila
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.02 sec)
2. MySQL数据库创建快照
2.1> 创建快照之前,需要确保快照的二进制日志位置随源实例中的数据保持最新状态。必须先使用以下命令停止对实例进行的任何写入操作
mysql> flush tables with read lock;
2.2> 使用 mysqldump 命令创建 MySQL 数据库的转储
[root@ip-172-30-0-56 mysql]# mysqldump --databases sakila --master-data=2 --single-transaction --order-by-primary -r backup.sql -uroot -p
Enter password:
2.3> 查看backup.sql文件,一般情况会在/var/lib/mysql/下
2.4> 创建快照之后,使用命令解锁 MySQL 数据库中的表
mysql> unlock tables;
3.将创建的backup.sql文件加载到Aurora数据库
4.创建用于复制的mysql user,在Master(Mysql)节点创建用户,并赋予权限。在Aurora数据库上会需要使用
4.1> 创建复制用户 repluser,并分配replication client及replication slave 的权限
mysql> create user 'repluser'@'%' identified by 'repluser';
Query OK, 0 rows affected (0.69 sec)
mysql> GRANT REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'repluser'@'%';
Query OK, 0 rows affected (0.07 sec)
5.从Aurora 数据库集群中启用复制
5.1> 连接到数据库集群
mysql> CALL mysql.rds_set_external_master ('ec2-161-189-202-44.cn-northwest-1.compute.amazonaws.com.cn', 3306, 'repluser', 'xxxxxxxx', 'mysql-bin.000001
Query OK, 0 rows affected (0.17 sec)
5.2> 启动复制
mysql> CALL mysql.rds_start_replication;
+-------------------------+
| Message |
+-------------------------+
| Slave running normally. |
+-------------------------+
1 row in set (1.06 sec)
Query OK, 0 rows affected (1.06 sec)
5.3> 查看slave状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: ec2-161-189-202-44.cn-northwest-1.compute.amazonaws.com.cn
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 323
Relay_Log_File: relaylog.000004
Relay_Log_Pos: 236
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table: mysql.rds_replication_status,mysql.rds_monitor,mysql.rds_sysinfo,mysql.rds_configuration,mysql.rds_history
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 323
Relay_Log_Space: 518
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: 2133421
Master_UUID: cf702f68-a370-11eb-a6a3-02ddc7051b40
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 the slave I/O thread to update it
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.06 sec)
5.4> 停止复制
mysql> CALL mysql.rds_stop_replication;
6.检查两端数据库
网友评论