本地Mysql和RDS搭建主从环境

上一篇文章中已经结合官方文档,说明了如何将RDS-Mysql物理备份恢复到本地,这篇文章是关于如何搭建和RDS—Mysql主从配置的步骤(对文档:https://yq.aliyun.com/articles/9044 进行的补充)。本篇文章以5.7为例。

但是有一些需要注意的地方:

  1. 使用这个方法搭建的主从环境,容易受到一些因素的影响,比方本地网络或者RDS本身出现的一些问题(binlog被删除),那么主从的搭建可能会中断。
  2. 所以如果想要搭建比较稳定的环境,在阿里云上可以参考使用DTS进行配置。

前提条件:

本地已经将RDS的物理文件进行了成功恢复。参考:https://yq.aliyun.com/articles/692056?spm=a2c4e.11155435.0.0.608333123an7eS

第一步

关于本地mysql的cnf文件内容,可以参考下面的格式:

#innodb_checksum_algorithm=crc32
#innodb_log_checksum_algorithm=strict_crc32
innodb_data_file_path=ibdata1:200M:autoextend
innodb_log_files_in_group=2
innodb_log_file_size=1048576000
#innodb_fast_checksum=false
#innodb_page_size=16384
#innodb_log_block_size=512
#innodb_undo_directory=./
#innodb_undo_tablespaces=0
server_id=197
#redo_log_version=1
#server_uuid=e4323bd1-072f-11e9-97a1-7cd30ad32d28
#master_key_id=0
binlog_format=ROW
master-info-repository=file
relay-log-info-repository=file
gtid_mode=on
enforce-gtid-consistency=true
#skip-grant-tables

第二步

打开备份解压文件可以看到文件xtrabackup_slave_info,其中第一行就是需要在本地MySQL执行的命令,它表示在备份结束时刻RDS当前GTID值’

本地Mysql和RDS搭建主从环境

下面执行的命令,请参考:

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed, 1 warning
mysql> truncate table  slave_relay_log_info;
Query OK, 0 rows affected (0.02 sec)

mysql> truncate table  mysql.slave_master_info;
Query OK, 0 rows affected (0.01 sec)

mysql> truncate table  mysql.slave_worker_info;
Query OK, 0 rows affected (0.00 sec)

mysql> reset slave;
Query OK, 0 rows affected (0.01 sec)

mysql> reset master;
Query OK, 0 rows affected (0.00 sec)
mysql> SET GLOBAL gtid_purged='6032a4a0-f50e-11e8-b374-7cd30a5a30b4:1-260056, 97e5b5fb-367d-11e9-af9e-7cd30aeb45ba:1-54309, bfe3ada0-1f7a-11e9-acd2-58605f589df3:1-161356, cf84fc60-072f-11e9-b0f9-7cd30ab8f5b0:1-310126, e4323bd1-072f-11e9-97a1-7cd30ad32d28:1-219582';
Query OK, 0 rows affected (0.01 sec)

mysql> change master to master_host='rm-2zeu5m7c190y65ppxx.mysql.rds.aliyuncs.com',master_user='ali',master_port=3306,master_password='xxx',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

第三步

进行验证:

然后执行 show slave status 就能够看到下面的结果了。 Slave_IO_Running: Yes Slave_SQL_Running: Yes
然后 Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates 说明主从现在的数据是一致的了。

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: rm-2zeu5m7c190y65ppglo.mysql.rds.aliyuncs.com
                  Master_User: ali
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000033
          Read_Master_Log_Pos: 192183
               Relay_Log_File: jun-relay-bin.000002
                Relay_Log_Pos: 108422
        Relay_Master_Log_File: mysql-bin.000033
             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: 192183
              Relay_Log_Space: 108627
              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: 1441442229
                  Master_UUID: 97e5b5fb-367d-11e9-af9e-7cd30aeb45ba
             Master_Info_File: /home/mysql/aliyun-mysql57/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: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set: 97e5b5fb-367d-11e9-af9e-7cd30aeb45ba:54310-54677
            Executed_Gtid_Set: 6032a4a0-f50e-11e8-b374-7cd30a5a30b4:1-260056,
97e5b5fb-367d-11e9-af9e-7cd30aeb45ba:1-54677,
bfe3ada0-1f7a-11e9-acd2-58605f589df3:1-161356,
cf84fc60-072f-11e9-b0f9-7cd30ab8f5b0:1-310126,
e4323bd1-072f-11e9-97a1-7cd30ad32d28:1-219582
                Auto_Position: 1
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>

在rds上创建新表,插入数据。
本地Mysql和RDS搭建主从环境

查看本地数据:
本地Mysql和RDS搭建主从环境

上一篇:验证RDS读写分离是否正确分发只读请求


下一篇:创建vue项目中,终端无法手动选择