AWS EC2 MySQL遷移到RDS案例


Amazon Relational Database Service (Amazon RDS) 是一種Web 服務,可讓用戶更輕松地在雲中設置、操作和擴展關系數據庫。它可以為行業標准關系數據庫提供經濟高效且可以調節大小的容量,並管理常見

數據庫管理任務。

現有的環境如下,計划將MySQL主庫billingdb遷移到RDS上,減少運維成本.

計划遷移之后的環境如下

現在開始遷移過程

1. 在當前的RDS slave上記錄需要過濾的表(RDS有自己的系統表,當RDS slave變成master后,這些表不需要被同步到EC2上的slave上)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.188.102.92
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000861
          Read_Master_Log_Pos: 18626649
               Relay_Log_File: relaylog.005026
                Relay_Log_Pos: 685
        Relay_Master_Log_File: mysql-bin.000861
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: mysql.rds_sysinfo,innodb_memcache.cache_policies,mysql.rds_replication_status,innodb_memcache.config_options,mysql.rds_history,mysql.plugin,mysql.rds_monitor

2. 在RDS slave上創建復制及相關賬戶(創建RDS作為備庫的時候,並沒有導入系統表,所以不會有主庫上的數據庫用戶)

mysql> grant replication slave on *.* to 'repl'@'%'  identified by 'repl';
mysql> flush privileges;

3. 修改RDS slave上二進制日志保留時間(默認情況下,RDS並不會保留二進制日志,這樣RDS切為master后就沒有二進制日志,也就談不上將EC2轉換為slave連上RDS了)

mysql> call mysql.rds_show_configuration;
mysql> call mysql.rds_set_configuration('binlog retention hours', 72);

4.  記錄當前master的session連接信息

/usr/local/mysql/bin/mysql -u root -p -e "show processlist"|awk '{print $3}'|grep ":"|awk '{print substr($0,0,index($0,":")-1) }'|sort|uniq -c
      1 10.188.100.36
      1 10.188.104.196
      1 ec2-backoffice-01.mypna.com
     20 ec2-billingcommerce-01.mypna.com
     20 ec2-billingcommerce-02.mypna.com
     10 ec2-billingen-01.mypna.com
     10 ec2-billingen-02.mypna.com
     11 ec2-billingengines-01.mypna.com
     10 ec2-billingentest-01.mypna.com
     10 ec2-billingentest-02.mypna.com
     25 ec2-billingiden-01.mypna.com
     25 ec2-billingiden-02.mypna.com
     20 ec2-billingoffer-01.mypna.com
     20 ec2-billingoffer-02.mypna.com
     20 ec2-billingoffer-03.mypna.com
     32 ec2-billingui-01.mypna.com
     31 ec2-billingui-02.mypna.com
      1 ec2-cliwebreport-01.mypna.com
      1 ec2-dbmon-01.mypna.com

5. 准備第4步主機名到IP地址的解析(RDS並不能解析出客戶端主機名,遷移到RDS后,檢查session連接的時候會方便很多)

IP address

Hostname

10.188.100.36

ec2-billingdb-04

10.188.104.196

ec2-csr-tracking-01

10.188.3.22

ec2-backoffice-01.mypna.com

10.188.1.102

ec2-billingcommerce-01.mypna.com

10.188.1.105

ec2-billingcommerce-02.mypna.com

10.188.1.252

ec2-billingen-01.mypna.com

10.188.1.59

ec2-billingen-02.mypna.com

10.188.1.189

ec2-billingengines-01.mypna.com

10.188.1.246

ec2-billingentest-01.mypna.com

10.188.1.232

ec2-billingentest-02.mypna.com

10.188.1.15

ec2-billingiden-01.mypna.com

10.188.1.16

ec2-billingiden-02.mypna.com

10.188.101.54

ec2-billingoffer-01.mypna.com

10.188.101.128

ec2-billingoffer-02.mypna.com

10.188.101.63

ec2-billingoffer-03.mypna.com

10.188.1.235

ec2-billingui-01.mypna.com

10.188.1.236

ec2-billingui-02.mypna.com

10.188.102.92

ec2-cliwebreport-01.mypna.com

10.188.100.65

ec2-dbmon-01.mypna.com

10.188.102.92

ec2-cliwebreport-01.mypna.com

6. 將主庫master重啟到只讀狀態,並記錄當前的master日志位置信息(這樣做是為了讓所有的slave都達到一致性狀態)

/etc/init.d/mysql restart --read-only

mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000865
         Position: 120
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

7. 稍等片刻,檢查所有EC2 slave,確保所有的EC2的slave已經趕上master的二進制日志位置

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.188.100.73
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000865          #看該行
          Read_Master_Log_Pos: 120                       #看該行
               Relay_Log_File: ec2-billingdb-04-relay-bin.000047
                Relay_Log_Pos: 279
        Relay_Master_Log_File: mysql-bin.000865
             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: 120                             #看該行
              Relay_Log_Space: 618
              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                     #看該行

8. EC2的機器ec2-billingdb-06是RDS的master,所以RDS的二進制日志要看是否追上ec2-billingdb-06

  • 在ec2-billingdb-06查看
mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000861
         Position: 18626649
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)
  • 在RDS上查看
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.188.102.92
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000861     #這里和上一步驟06機器的二進制日志一致
          Read_Master_Log_Pos: 18626649             #這里和上一步驟06的機器的二進制日志一致
               Relay_Log_File: relaylog.005026
                Relay_Log_Pos: 685
        Relay_Master_Log_File: mysql-bin.000861
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: mysql.rds_sysinfo,innodb_memcache.cache_policies,mysql.rds_replication_status,innodb_memcache.config_options,mysql.rds_history,mysql.plugin,mysql.rds_monitor
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 18626649            #這里和上一步驟06機器的二進制日志一致
              Relay_Log_Space: 79732
              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

8. 將master主庫數據庫關機

/etc/init.d/mysql stop 

9. 記錄RDS slave的二進制日志信息

mysql> show master status;
*************************** 1. row ***************************
             File: mysql-bin-changelog.007492
         Position: 120
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

 10. 清除RDS的slave信息,使其作為獨立的主庫運行

mysql>CALL mysql.rds_stop_replication;
mysql>CALL mysql.rds_reset_external_master;
mysql>show slave status\G;
mysql>show master status\G;
File: mysql-bin-changelog.007492
         Position: 1025

11. 將步驟1中需要過濾的表添加到原master的配置文件/etc/my.cnf里

replicate-ignore-table=mysql.rds_sysinfo
replicate-ignore-table=innodb_memcache.cache_policies
replicate-ignore-table=mysql.rds_replication_status
replicate-ignore-table=innodb_memcache.config_options
replicate-ignore-table=mysql.rds_history
replicate-ignore-table=mysql.plugin
replicate-ignore-table=mysql.rds_monitor
replicate-ignore-table=mysql.rds_heartbeat2

12. 將原master以只讀的方式啟動(確保應用不會連接進來寫臟數據)

/etc/init.d/mysql start --read-only 

13. 將原master作為slave連接到RDS

mysql> change master to   
     master_host='RDS endpoint name',
     master_user='repl',
     master_password='repl',
     master_port=3306, 
     master_log_file='mysql-bin-changelog.xxxxxx', 
     master_log_pos=xxx; 
mysql> start slave;
mysql> show slave status\G;

14, 將應用解析到RDS上,並檢查RDS的session信息,確保應用可以正常接入


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM