使用innobackupex基于从库搭建mysql主从架构

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:        MySQL的主从搭建大家有很多种方式,传统的mysqldump方式是很多人的选择之一。但对于较大的数据库则该方式并非理想的选择。使用Xtrabackup可以快速轻松的构建或修复mysql主从架构。


       MySQL的主从搭建大家有很多种方式,传统的mysqldump方式是很多人的选择之一。但对于较大的数据库则该方式并非理想的选择。使用Xtrabackup可以快速轻松的构建或修复mysql主从架构。本文描述了基于现有的从库来快速搭建主从,即作为原主库的一个新从库。该方式的好处是对主库无需备份期间导致的相关性能压力。搭建过程中使用了快速流备份方式来加速主从构建以及描述了加速流式备份的几个参数,供大家参考。


    有关流式备份可以参考:Xtrabackup 流备份与恢复


1、备份从库
###远程备份期间使用了等效性验证,因此应先作相应配置,这里我们使用的是mysql用户
$ innobackupex --user=root --password=xxx --slave-info --safe-slave-backup \
--compress-threads=3 --parallel=3 --stream=xbstream \
--compress /log | ssh -p50021 mysql@172.16.16.10 "xbstream -x -C /log/recover"


###备份期间使用了safe-slave-backup参数,可以看到SQL thread被停止,完成后被启动
$ mysql -uroot -p -e "show slave status \G"|egrep 'Slave_IO_Running|Slave_SQL_Running'
Enter password:
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it


###复制my.cnf文件到新从库
$ scp -P50021 /etc/my.cnf mysql@172.16.16.10:/log/recover


2、主库授予新从库复制账户
master@MySQL> grant replication slave,replication client on *.* to repl@'172.16.%.%' identified by 'repl';


3、新从库prepare        
###由于使用了流式压缩备份,因此需要先解压
###下载地址 
http://www.quicklz.com/
# tar -xvf qpress-11-linux-x64.tar
qpress
# cp qpress /usr/bin/
$ innobackupex --decompress /log/recover                               ###解压
$ innobackupex --apply-log --use-memory=2G /log/recover    ###prepare备份


4、准备从库配置文件my.cnf
###根据需要修改相应参数,这里的修改如下,
skip-slave-start
datadir = /log/recover
port = 3307
server_id = 24                    
socket = /tmp/mysql3307.sock
pid-file=/log/recover/mysql3307.pid
log_error=/log/recover/recover.err


5、启动从库及修改change master
# chown -R mysql:mysql /log/recover
# /app/soft/mysql/bin/mysqld_safe --defaults-file=/log/recover/my.cnf &

mysql> system more /log/recover/xtrabackup_slave_info
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000658', MASTER_LOG_POS=925384099
mysql> CHANGE MASTER TO
    -> MASTER_HOST='172.16.16.10',       ### Author: Leshami
    -> MASTER_USER='repl',                     ### Blog  :
http://blog.csdn.net/leshami
    -> MASTER_PASSWORD='repl',
    -> MASTER_PORT=3306,
    -> MASTER_LOG_FILE='mysql-bin.000658',
    -> MASTER_LOG_POS=925384099;
Query OK, 0 rows affected, 2 warnings (0.31 sec)

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


6、基于从库备份相关参数及加速流备份参数

The --slave-info option This option is useful when backing up a replication slave server. It prints the binary
log position and name of the master server. It also writes this information to the xtrabackup_slave_info file
as a CHANGE MASTER statement.
This is useful for setting up a new slave for this master can be set up by starting a slave server on this backup and
issuing the statement saved in the xtrabackup_slave_info file.


The --safe-slave-backup option In order to assure a consistent replication state, this option stops the slave
SQL thread and wait to start backing up until Slave_open_temp_tables in SHOW STATUS is zero. If there are
no open temporary tables, the backup will take place, otherwise the SQL thread will be started and stopped until there
are no open temporary tables. The backup will fail if Slave_open_temp_tables does not become zero after
--safe-slave-backup-timeout seconds (defaults to 300 seconds). The slave SQL thread will be restarted
when the backup finishes.
Using this option is always recommended when taking backups from a slave server.


Warning: Make sure your slave is a true replica of the master before using it as a source for backup. A good tool
to validate a slave is pt-table-checksum.


--compress
        This option instructs xtrabackup to compress backup copies of InnoDB
        data files. It is passed directly to the xtrabackup child process.
        ###注compress方式是一种相对粗糙的压缩方式,压缩为.gp文件,没有gzip压缩比高


--compress-threads
        This option specifies the number of worker threads that will be used
        for parallel compression. It is passed directly to the xtrabackup
        child process. Try 'xtrabackup --help' for more details.


--decompress
        Decompresses all files with the .qp extension in a backup previously
        made with the --compress option.


 --parallel=NUMBER-OF-THREADS
        On backup, this option specifies the number of threads the
        xtrabackup child process should use to back up files concurrently.
        The option accepts an integer argument. It is passed directly to
        xtrabackup's --parallel option. See the xtrabackup documentation for
        details.

        On --decrypt or --decompress it specifies the number of parallel
        forks that should be used to process the backup files. 

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
消息中间件 数据采集 监控
ELK搭建(四):监控mysql慢查询、错误日志日志
因为mysql免费、稳定以及还不错的性能,是当前市面上多数公司的数据库选择。在实际的生产环境中我们更需要及时知道数据库中的报错日志、慢日志等信息,来帮助我们进行排错和优化。 普通的到服务器上去查看日志的方式并不方便,特别是涉及到分布式部署时,因此我们需要一个统一的监控平台来实时、方便的查看这些日志数据。
745 0
ELK搭建(四):监控mysql慢查询、错误日志日志
|
canal 存储 NoSQL
mysql进阶:canal搭建主从|集群架构
之前我们讲解过canal的各种应用,但是对于生产环境来讲,服务高可用是必须保证的。因此canal单节点是不能满足我们的需求的。就需要搭建canal集群。
586 2
mysql进阶:canal搭建主从|集群架构
|
关系型数据库 MySQL Shell
MySQL笔记 | 2.Docker下搭建MySQL&查看BinLog文件
这篇文章主要是在Docker下搭建一个MySQL环节,为了后续的数据库学习做准备。
|
SQL 机器学习/深度学习 分布式计算
CDH 搭建_ Hadoop _ MySQL|学习笔记
快速学习 CDH 搭建_ Hadoop _ MySQL
114 0
CDH 搭建_ Hadoop _ MySQL|学习笔记
|
SQL 存储 消息中间件
ELK搭建(六):搭建mysql性能、执行效率监控平台
mysql作为市场的主流数据库,承载了大部分公司的核心业务数据,同时也是大多数业务的底层存储。 针对mysql运行情况的监控必不可少,之前我们讲解了如何搭建mysql慢日志、错误日志的监控平台。 那么本期,我们针对mysql集群、性能、各类sql语句执行情况、服务状态等指标来搭建一个可视化的监控平台,方便我们实时了解mysql资源利用率、sql执行效率、访问压力等等。
331 0
ELK搭建(六):搭建mysql性能、执行效率监控平台
|
关系型数据库 MySQL Linux
Mysql主从复制与高可用主备切换搭建完整详细版
Mysql主从复制与高可用主备切换搭建完整详细版
|
SQL 关系型数据库 MySQL
Mysql主从复制搭建
Mysql主从复制搭建
130 0
|
安全 关系型数据库 MySQL
【Docker 基础教程】Mysql主从服务搭建------Mysql容器闪退及容器名冲突系列问题
【Docker 基础教程】Mysql主从服务搭建------Mysql容器闪退及容器名冲突系列问题
194 0
【Docker 基础教程】Mysql主从服务搭建------Mysql容器闪退及容器名冲突系列问题
|
关系型数据库 MySQL Linux
Centos7中搭建MySQL环境
安装社区套件yum源地址
220 0
|
Java 关系型数据库 MySQL
搭建3层架构实战 MySQL(二)|学习笔记
快速学习搭建3层架构实战 MySQL(二)
90 0