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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: MySQL的主从搭建大家有很多种方式,传统的mysqldump方式是很多人的选择之一。但对于较大的数据库则该方式并非理想的选择。

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
目录
相关文章
|
24天前
|
调度
【嵌入式开源库】timeslice的使用,完全解耦的时间片轮询框架构(二)
【嵌入式开源库】timeslice的使用,完全解耦的时间片轮询框架构
|
1月前
|
关系型数据库 MySQL API
Flink CDC产品常见问题之mysql整库同步到starrock时任务挂掉如何解决
Flink CDC(Change Data Capture)是一个基于Apache Flink的实时数据变更捕获库,用于实现数据库的实时同步和变更流的处理;在本汇总中,我们组织了关于Flink CDC产品在实践中用户经常提出的问题及其解答,目的是辅助用户更好地理解和应用这一技术,优化实时数据处理流程。
|
1月前
|
关系型数据库 MySQL 开发工具
MySQL5.7主从配置(Docker)
MySQL5.7主从配置(Docker)
728 0
|
1月前
|
网络协议 Linux
Linux DNS服务详解——DNS主从架构配置
Linux DNS服务详解——DNS主从架构配置
407 4
|
1月前
|
SQL 关系型数据库 MySQL
解决MySQL主从慢同步问题的常见的解决方案:
解决MySQL主从慢同步问题的方法有很多,以下是一些常见的解决方案: 1. 检查网络连接:确保主从服务器之间的网络连接稳定,避免网络延迟或丢包导致数据同步缓慢。 2. 优化数据库配置:调整MySQL的配置参数,如增大binlog文件大小、调整innodb_flush_log_at_trx_commit等参数,以提高主从同步性能。 3. 检查IO线程和SQL线程状态:通过SHOW SLAVE STATUS命令检查IO线程和SQL线程的状态,确保它们正常运行并没有出现错误。 4. 检查主从日志位置:确认主从服务器的binlog文件和位置是否正确,避免由于错误的日志位置导致同步延迟。 5.
122 1
|
1月前
|
分布式计算 DataWorks 关系型数据库
DataWorks支持将ODPS表拆分并回流到MySQL的多个库和表中
【2月更文挑战第14天】DataWorks支持将ODPS表拆分并回流到MySQL的多个库和表中
59 8
|
2月前
|
分布式计算 DataWorks 关系型数据库
DataWorks支持将ODPS表拆分并回流到MySQL的多个库和表中
DataWorks支持将ODPS表拆分并回流到MySQL的多个库和表中
30 4
|
1天前
|
存储 SQL 关系型数据库
不停止MySQL服务增加从库的两种方式
不停止MySQL服务增加从库的两种方式
|
9天前
|
SQL 关系型数据库 MySQL
用MySQL创建公司资料库表格
创建了员工、分支、客户及工作关系的数据库表格。员工与分支间有works_with表记录销售数据,外键关联并处理删除操作(set null或cascade)。插入数据后,通过SQL查询获取员工、客户信息,使用聚合函数、通配符、联合查询和JOIN操作。子查询用于复杂条件筛选。数据库设计确保了数据完整性和参照完整性。
15 0
|
10天前
|
关系型数据库 MySQL
MySQL全局库表查询准确定位字段
information_schema.COLUMNS 详细信息查询
199 4