XtraBackup在线进行MySQL的主从部署一

【前言】MySQL的主从部署的方法有很多种,最常见的方法就是用mysqldump的方式进行,众所周知mysqldump方式构建主从的方法有两个很大的缺点:

1、主库需要锁表,这就意味着这期间主库不能做修改的操作(在做导出的时候需要停止相关的接口和应用,对于一些小公司可能没影响,但如果是电商平台,就是相当于停止服务;

2、当数据量较小的时候mysqldump的方法是很快的,但是当数据量超过20G的时候,整个过程就相当的慢。曾经用这种方法导一个70G的数据库,在使用的是高端存储的情况下,导出花费了50分钟。

Percona提供了xtrabackup开源备份工具,可以快速且无锁表地进行mysql的备份并且记录相应的log信息,特点如下:
•备份过程快速、可靠;
•备份过程不会打断正在执行的事务;
•能够基于压缩等功能节约磁盘空间和流量;
•自动实现备份检验;
•还原速度快;

本文档介绍通过xtrabackup进行搭建mysql的主从数据库;

【一】软件的安装

1.1 下载地址 https://www.percona.com/downloads/XtraBackup/

当前的版本已更新到2.2.11;

1.2 软件的安装

下载相应的rpm包后,运行:rpm -ivh percona-xtrabackup-2.2.11-1.el6.x86_64.rpm 

[root@OTO-DB-T02 share]#  rpm -ql  percona-xtrabackup
/usr/bin/innobackupex
/usr/bin/xbcrypt
/usr/bin/xbstream
/usr/bin/xtrabackup
 

1.3 软件的介绍

XtraBackup中主要包含了三个工具

xtrabackup:支持innodb存储引擎表,xtradb存储引擎表

innobackupex:支持innodb存储引擎表、xtradb存储引擎表、myisam存储引擎表。

1.4 配置datadir参数

[root@OTO-DB-T02 backup]#  more /etc/my.cnf |grep datadir
datadir=/data/mysql   

注:当datadir参数没有配置时还原的时候会报如下错误

innobackupex: got a fatal error with the following stacktrace: at /usr/bin/innobackupex line 2510
    main::copy_back(0) called at /usr/bin/innobackupex line 1570
innobackupex: Error: Backup data file '/data/backup/2015-06-15_14-06-59/ibdata1' does not exist. at /usr/bin/innobackupex line 2510.
 

【二】 工具的使用

[root@OTO-DB-T02 backup]# innobackupex  --help
Options:
    --apply-log
        Prepare a backup in BACKUP-DIR by applying the transaction log file
        named "xtrabackup_logfile" located in the same directory. Also,
        create new transaction logs. The InnoDB configuration is read from
        the file "backup-my.cnf".

    --backup-locks
        This option controls if backup locks should be used instead of FLUSH
        TABLES WITH READ LOCK on the backup stage. The option has no effect
        when backup locks are not supported by the server. This option is
        enabled by default, disable with --no-backup-locks.

    --close-files
        Do not keep files opened. This option is passed directly to
        xtrabackup. Use at your own risk.

    --compact
        Create a compact backup with all secondary index pages omitted. This
        option is passed directly to xtrabackup. See xtrabackup
        documentation for details.

    --compress
        This option instructs xtrabackup to compress backup copies of InnoDB
        data files. It is passed directly to the xtrabackup child process.
        Try 'xtrabackup --help' for more details.
 

2.1的完整备份及还原过程

步骤一:进行数据库的完整备份

innobackupex --defaults-file=/etc/my.cnf  --user=root    --password='123456'  /data/backup

2.2 InnoDB备份,使备份可用

innobackupex  --user=root    --password='123456'    --apply-log  /data/backup/2015-06-15_14-57-24  (步骤一备份完成后会产生2015-06-15_14-57-24目录)

说明:第一步备份相当于备份当前数据库的数据文件,但是备份期间系统的变更这个过程是不能备份的。所以要进行步骤二的备份,这个步骤可用记录在备份步骤一系统进行的所有变更。这样在进行恢复的过程会累加步骤一和步骤二的备份形成一个完整的备份;

2.3 数据库的还原

innobackupex    --defaults-file=/etc/my.cnf  --user=root    --password='123456'  --copy-back  /data/xtrabackup/2015-02-01_08-30-13/

2.4 文件的授权

chown –R mysql:mysql /data/mysql

2.5 启动数据库

service mysqld start;启动数据库

2.6 根据/data/mysql下面的信息连接主库

[root@OTO-DB-T02 mysql]# cat xtrabackup_binlog_pos_innodb
./mysql-bin.000027    17645

XtraBackup 的详细介绍:请点这里
XtraBackup 的下载地址:请点这里

相关推荐