阿里雲RDS數據庫利用物理備份搭建從庫

(一)、背景介紹
由於數據庫採用阿里雲的RDS數據庫,當所有的讀寫都在一臺服務器上的時候,出現了CPU負載較高,尤其在業務時間段內,併發的訪問較多,造成業務壓力比較大,經詢問可以採用主從複製,RDS數據庫只用於寫,其他的本地安裝的mysql數據庫用於讀,可以有多臺,後臺的程序還可以做讀寫分離,來減輕RDS數據庫的壓力負載。

安裝條件:

  1. 本地自建Mysql數據必須和雲mysql的大版本保持一致
  2. 64位的Linux系統
  3. MySQL 5.6及之前(含5.5)的版本需要安裝 Percona XtraBackup 2.3 ; MySQL 5.7版本需要安裝 Percona XtraBackup2.4
  4. 爲了快速恢復,本地mysql安裝都採用centos的yum安裝方式
    5.注意下面沒有說明哪個版本執行的就是都需要執行的
    6.搭建複製環境前需要保證ECS能夠訪問的通RDS,建議用RDS內網地址進行復制搭建
    一般來說我們的數據庫版本:mysql 5.6

(二)、安裝配置
1、mysql數據庫的安裝和配置(略),具體參考https://blog.51cto.com/liqingbiao/1692315
2、替換/etc/my.cnf配置內容,其中跳過權限表進行啓動

[appuser@inside-nginx152245 ~]$ cat /etc/my.cnf
[mysqld]
innodb_checksum_algorithm=crc32
innodb_data_file_path=ibdata1:200M:autoextend
innodb_log_files_in_group=2
innodb_log_file_size=524288000
innodb_undo_directory=/usr/local/mysql/
basedir=/usr/local/mysql
datadir=/data/mysql
innodb_undo_tablespaces=0
server_id=999999998
log_bin=mysql-log
gtid_mode=on
enforce_gtid_consistency=on
log-slave-updates=1
relay_log=relay-log
sql_mode=''
binlog_format=row
slave_skip_errors = 1062,1032
skip-grant-tables=1

#######注意server_id可以自己改,務必不要和RDS的一樣了

2、安裝配置Percona
1、安裝Percona 倉庫
yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm
2、查看percona庫
[appuser@predb ~]$ yum list|grep percona
3、安裝percona-xtrabackup-24包
yum install percona-xtrabackup-24

(二)、rpm 安裝

2.1、下載percona rpm包
wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.4/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.4-1.el7.x86_64.rpm

2.2、通過yum localinstall 進行安裝
yum localinstall percona-xtrabackup-24-2.4.4-1.el7.x86_64.rpm    #####5.6版本安裝這個
 yum -y install percona-xtrabackup-24.x86_64   #####5.7版本需要安裝這個

 2.3、percona卸載
yum remove percona-xtrabackup

3、下載備份文件並恢復。登錄雲服務器ECS,執行下載命令。wget -c '<數據備份文件外網下載地址>' -O <自定義文件名>.tar.gz -c啓用斷點續傳模式,-O將下載結果保存指定文件。

 [root@inside-nginx152245 data]#wget -c 'http://rxxxxxxxxxxxxxxxxxxxx/custins4543777/hins6010151_data_20190621021827.tar.gz?OSSAccessKeyId=LTAIyKzxtSYNknVO&Expires=1561261206&Signature=KgiZPWl9YlX5kOX0WChx3h6Yw18%3D' -O mysql20190621.tar.gz
[root@inside-nginx152245 data]# tar xf mysql20190621.tar.gz -C mysql
[root@inside-nginx152245 data]# innobackupex --defaults-file=/etc/my.cnf --apply-log /data/mysql20190402/
190621 16:16:41 innobackupex: Starting the apply-log operation

IMPORTANT: Please check that the apply-log run completes successfully.
           At the end of a successful apply-log run innobackupex
           prints "completed OK!".
[root@inside-nginx152245 data]# chown -R mysql:mysql /data/mysql        
[root@inside-nginx152245 data]# /etc/init.d/mysqld restart
Shutting down MySQL....                                    [  OK  ]
Starting MySQL.........                                    [  OK  ]

4、直接進入mysql更新root密碼

mysql> update user set password=PASSWORD('xxxxx') where user='root';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

5、刪除/etc/my.cnf裏面的skip-grant-tables=1,然後重啓

[root@inside-nginx152245 data]# vim /etc/my.cnf
[root@inside-nginx152245 data]# /etc/init.d/mysqld restart
Shutting down MySQL..                                      [  OK  ]
Starting MySQL.                                            [  OK  ]

6、通過root進行登錄,執行如下操作。

mysql>use mysql;  5.6執行mysql>drop table slave_master_info;    5.6執行
mysql>drop table slave_relay_log_info;   5.6執行
mysql>drop table slave_worker_info;    5.6執行
mysql>drop table innodb_index_stats;   5.6執行 
mysql>drop table innodb_table_stats;    5.6執行
mysql>source /usr/share/mysql/mysql_system_tables.sql;   ########## 5.6執行
mysql> source /tmp/mysql-5.6.43-linux-glibc2.12-x86_64/share/mysql_system_tables.sql      #######5.6版本執行。後邊的路徑是mysql安裝的路徑
mysql>exit
/etc/init.d/mysqld restart      ###########5.6版本必須重啓,切記執行

7、查看xtrbackup_slave_info裏邊的set global gtid_purged語句,把這個copy到後面複製用
[root@inside-nginx152245 share]# cat /data/mysql/xtrabackup_slave_info
SET GLOBAL gtid_purged='5b61ae19-a34d-11e7-810f-5cb901891368:1-3823344, 70f8ce71-31fb-11e8-a373-7cd30ae014ac:1-4846380, 7302d9cf-a34d-11e7-810f-244c075fefa9:1-2828134';
CHANGE MASTER TO MASTER_AUTO_POSITION=1;
阿里雲RDS數據庫利用物理備份搭建從庫
3、在數據庫中依次執行如下語句


mysql> reset master
    -> ;
Query OK, 0 rows affected (0.02 sec)

mysql> SET GLOBAL gtid_purged='5b61ae19-a34d-11e7-810f-5cb901891368:1-3823344, 70f8ce71-31fb-11e8-a373-7cd30ae014ac:1-4846380, 7302d9cf-a34d-11e7-810f-244c075fefa9:1-2828134';
Query OK, 0 rows affected (0.02 sec)

mysql> reset slave;
ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

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

mysql>  change master to master_host ='rm-uf64n5y6yet9b22s1.mysql.rds.aliyuncs.com',master_port=3306,master_user='dowadmin',master_password="70537449c232!dow",master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
#############注意:上面的master_host  master_port   master_user  master_password 根據RDS主庫的信息各自填寫自己的信息,不要照着上面執行;
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: rm-uf64n5y6yet9b22s1.mysql.rds.aliyuncs.com
                  Master_User: dowadmin
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.001807
          Read_Master_Log_Pos: 906900
               Relay_Log_File: relay-log.000004
                Relay_Log_Pos: 431601
        Relay_Master_Log_File: mysql-bin.001805
             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: 431471
              Relay_Log_Space: 20108553
              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: 35647
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: 459412040
                  Master_UUID: 70f8ce71-31fb-11e8-a373-7cd30ae014ac
             Master_Info_File: /data/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Reading event from the relay log
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set: 70f8ce71-31fb-11e8-a373-7cd30ae014ac:4846381-4867752
            Executed_Gtid_Set: 5b61ae19-a34d-11e7-810f-5cb901891368:1-3823344,
70f8ce71-31fb-11e8-a373-7cd30ae014ac:1-4848839,
7302d9cf-a34d-11e7-810f-244c075fefa9:1-2828134
                Auto_Position: 1
1 row in set (0.00 sec)

ERROR:
No query specified
#####搭建完成show slave status\G看到的結果應該如下,slave io/sql線程都是YES狀態:

如有問題,請查看阿里雲rds數據庫文檔(https://yq.aliyun.com/articles/689032?spm=a2c4e.11153940.0.0.48991670fK7Lg2

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章