xtrabackup 在线主从搭建

因为意外导致某个MySQL的从服务器宕机,且不可修复,因为是业务数据库,不能停机和锁表进行从库的搭建,所以考虑了使用xtrabackup 进行在线主从搭建。

一、数据库环境

 

注意:  主从搭建主库一定要开binlog 日志;     
             log-bin=mysql-bin        否则无法进行

 

数据库

数据库版本

外网IP

内网IP

同步用户

备份目录

主库

MYSQL-5.6.7

211.147.1.202

192.168.0.123

 

rep

 

 

从库

MYSQL-5.6.7

211.147.1.202

192.168.0.13

二、准备安装包

wget http://www.percona.com/downloads/XtraBackup/XtraBackup-2.1.9/binary/Linux/x86_64/percona-xtrabackup-2.1.9-744-Linux-x86_64.tar.gz

yum install perl-DBI  perl-DBD-MySQL  perl-Time-HiRes  perl-lO-Socket-SSL   perl-Digest-MD5   libaio  -y 

三、安装

1.1、安装 xtrabackup

[root@localhost] #tar -xvf percona-xtrabackup-2.1.9-744-Linux-x86_64.tar.gz

[root@localhost soft]# cd percona-xtrabackup-2.1.9-Linux-x86_64/

[root@localhost percona-xtrabackup-2.1.9-Linux-x86_64]# ls

bin  share

[root@localhost percona-xtrabackup-2.1.9-Linux-x86_64]# cd bin/

[root@localhost bin]# ls

innobackupex  innobackupex-1.5.1  xbcrypt  xbstream  xtrabackup  xtrabackup_55  xtrabackup_56

[root@localhost bin]# pwd

/data/soft/percona-xtrabackup-2.1.9-Linux-x86_64/bin

1.2、innobackupex,xtrabackup等文件拷贝到*/mysql/bin 的目录下

[root@localhost bin]# cp * /data/soft/mysql/bin/

[root@localhost bin]# ln -s /data/soft/mysql/bin/* /usr/bin/

主从环境都要搭建到此位置。

2、数据库 创建复制用户和备份用户

2.1 在主库上创建备份用户

mysql> CREATE USER 'bkpuser'@'localhost' IDENTIFIED BY 'bkppass';

mysql> GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT,PROCESS,SUPER ON *.* TO 'bkpuser'@'localhost';

mysql>  FLUSH PRIVILEGES;

2.2在主库上创建复制用户

mysql>grant replication slave,reload,super on  *.* to rep@192.168.0.13 identified by 'xxxxx'; 

mysql>flush privileges;

3.1 开始全量备份

需要修改下配置文件,但是不需要重启;加上以下俩个参数,否则在备份时候会报错。主从都要添加两行参数。

[root@localhost bin]# cat /etc/my.cnf

innodb_log_file_size = 5M

datadir=/data/mysql5

3.2 开始备份

[root@localhost soft]# export BKP_PASS="bkppass"

[root@localhost soft]# innobackupex --defaults-file=/etc/my.cnf --host='localhost' --port=3306 --user=bkpuser --password=${BKP_PASS}   /data/soft/(备份位置)

针对指定的数据库备份:

$INNOBACKUPEXFULL --defaults-file=$MY_CNF --use-memory=4G $MYSQL_CMD --database="bwlcfosda  bwlcultrdas  performance_schema bwlccmssda ultraxbwlcsdaw"

备份成功显示如下字样:

171024 09:07:45  innobackupex: Connection to database server closed

171024 09:07:45  innobackupex: completed OK!

3.3  把备份传到从库的服务器上

[root@localhost soft]# scp -r   2017-10-26_21-49-35   192.168.0.13:/data/soft

4、从库操作

4.1 全量恢复

 [root@localhost soft]#  innobackupex --use-memory=2G --apply-log  /data/soft/2017-10-26_21-49-35/

成功显示如下字样

171024 17:11:04  innobackupex: completed OK!

4.2  确认从库的是关闭的(停掉从库),datadir 目录是空的,开始恢复数据

 

[root@localhost bin]# cat /etc/my.cnf

 

 server-id = 2       #不要和主库的重复

 

[root@localhost mysql5]#  /etc/init.d/mysqld stop

[root@localhost mysql5]# cd /data/mysql5/

[root@localhost mysql5]# rm -rf *

[root@localhost mysql5]#  innobackupex  --defaults-file=/etc/my.cnf --copy-back    /data/soft/2017-10-26_21-49-35/

71024 17:13:09  innobackupex: completed OK!

4.3  修改数据的属组 属主

[root@localhost mysql5]# chown mysql:mysql  /data/mysql5 -R

4.4  启动数据库

[root@localhost mysql5]# /etc/init.d/mysqld start

Starting MySQL..                                           [确定]

[root@localhost mysql5]# mysql -uroot -pxxxxxx

5.1 、修改复制起始位置

root@localhost mysql5]# cd   /data/soft/2017-10-26_21-49-35/

root@localhost mysql5]#   cat xtrabackup_binlog_info 

mysql-bin.000002    532     (记住文件偏移量和位置)

 

mysql> change master to master_host='192.168.0.123',master_user='rep',master_password='xxxxxx',master_log_file='mysql-bin.000002',master_log_pos=532;

Query OK, 0 rows affected, 2 warnings (0.23 sec)

5.2  查看从库同步状态

mysql>  show slave status\G

*************************** 1. row ***************************

               Slave_IO_State:

                  Master_Host: 192.168.20.3

                  Master_User: rep

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000005

          Read_Master_Log_Pos: 1390

               Relay_Log_File: localhost-relay-bin.000001

                Relay_Log_Pos: 4

        Relay_Master_Log_File: mysql-bin.000005

             Slave_IO_Running: No

            Slave_SQL_Running: No

 

注意:要在主库赋予新的从库复制权限

在主库上执行:

mysql> grant all privileges on *.* to rep@'192.168.0.13' identified by 'xxxxxx';

在从库上开启复制进程:

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.20.3

                  Master_User: rep

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000005

          Read_Master_Log_Pos: 1390

               Relay_Log_File: localhost-relay-bin.000002

                Relay_Log_Pos: 267

        Relay_Master_Log_File: mysql-bin.000005

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

             

1 row in set (0.00 sec)

6、施工后校验

1: 主库新建测试表rep_test,并在从库查看是否创建成功

MASTER

#mysql -u root -p****** -h127.0.0.1

mysql>use laclocal;

mysql>create table rep_test (id int,name varchar(20));

SLAVE

#mysql -u root -p******

mysql>use laclocal;

mysql>show create table rep_test;

2: 主库对rep_test测试表进行增删改查测试,并在从库查看该表是否进行了相应的改动

3: 主库对rep_test进行删除表操作,并在从库查看是否删除成功

MASTER

#mysql -u root -p****** 

mysql>use laclocal;

mysql>drop table rep_test;

SLAVE

#mysql -u root -p******

mysql>use laclocal;

mysql>show columns from rep_test;

 

7、基于  xtrabackup 进行在线主从搭建施工完毕!

 

posted @ 2018-03-29 21:11  lixinliang  阅读(405)  评论(0编辑  收藏  举报