阿里云RDS数据库利用物理备份搭建从库

(一)、背景介绍
因为数据库采用阿里云的RDS数据库,当全部的读写都在一台服务器上的时候,出现了CPU负载较高,尤为在业务时间段内,并发的访问较多,形成业务压力比较大,经询问能够采用主从复制,RDS数据库只用于写,其余的本地安装的mysql数据库用于读,能够有多台,后台的程序还能够作读写分离,来减轻RDS数据库的压力负载。mysql

安装条件:linux

  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

(二)、安装配置
一、mysql数据库的安装和配置(略),具体参考https://blog.51cto.com/liqingbiao/1692315
二、替换/etc/my.cnf配置内容,其中跳过权限表进行启动nginx

[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的同样了

二、安装配置Percona
一、安装Percona 仓库
yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm
二、查看percona库
[appuser@predb ~]$ yum list|grep percona
三、安装percona-xtrabackup-24包
yum install percona-xtrabackup-24sql

(二)、rpm 安装数据库

2.一、下载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.二、经过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.三、percona卸载
yum remove percona-xtrabackup

三、下载备份文件并恢复。登陆云服务器ECS,执行下载命令。wget -c '<数据备份文件外网下载地址>' -O <自定义文件名>.tar.gz -c启用断点续传模式,-O将下载结果保存指定文件。vim

[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  ]

四、直接进入mysql更新root密码centos

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)
##############通常使用##########
mysql>update user set host='localhost' where host='::1';
mysql>update user set host='%' where host='127.0.0.1';

五、删除/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  ]

六、经过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/local/mysql/share/mysql_system_tables.sql;  ##5.6执行,
mysql>exit
/etc/init.d/mysqld restart      ###########5.6版本必须重启,切记执行
备注:若是yum安装的话,能够找安装路径下mysql_system_tables.sql这个文件通常都在share目录下。mysql> source /tmp/mysql-5.6.43-linux-glibc2.12-x86_64/share/mysql_system_tables.sql      #######5.6版本执行。后边的路径是mysql安装的路径

七、查看xtrbackup_slave_info里边的set global gtid_purged语句,把这个copy到后面复制用app

[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数据库利用物理备份搭建从库
三、在数据库中依次执行以下语句

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

相关文章
相关标签/搜索