XtraBackup 实现不停机不锁表搭建mysql主从

2024-02-14 10:58

本文主要是介绍XtraBackup 实现不停机不锁表搭建mysql主从,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简介

Xtrabackup是由 Percona 开发的一个开源软件,可实现对 InnoDB 的数据备份,支持在线热备份(备份时不影响数据读写)。备份时,Xtrabackup 会将 Master 的 binlog 信息记录在 xtrabackup_slave_info 文件中,通过此信息可以方便的搭建主从复制。

XtraBackup 有两个工具:xtrabackup 和 innobackupex:

  • xtrabackup 本身只能备份 InnoDB 和 XtraDB ,不能备份 MyISAM;
  • innobackupex 本身是 Hot Backup 脚本修改而来,同时可以备份 MyISAM 和 InnoDB,但是备份 MyISAM 需要加读锁。

官网:http://www.percona.com/software/percona-xtrabackup
文档:http://www.percona.com/doc/percona-xtrabackup/2.2/index.html

注:本文服务器环境为 CentOS,其他环境请自行修改实现。

修改主库、从库 MySQL 配置文件

1、Master

vim /etc/my.cnf

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;"><span class="hljs-setting">datadir=<span class="hljs-value">/var/lib/mysql</span></span>
<span class="hljs-setting">server-id=<span class="hljs-value"><span class="hljs-number" style="color: rgb(42, 161, 152);">1</span></span></span>
<span class="hljs-setting">log-bin=<span class="hljs-value">mysql-bin</span></span>
</code>

2、Slave:

vim /etc/my.cnf

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;"><span class="hljs-setting">server-id=<span class="hljs-value"><span class="hljs-number" style="color: rgb(42, 161, 152);">2</span></span></span>
<span class="hljs-setting">datadir=<span class="hljs-value">/var/lib/mysql</span></span>
</code>

安装 XtraBackup

1、添加源

yum install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm

检查是否添加成功:

yum list | grep percona

如果执行正确,其输出信息通常类似:

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;">percona-<span class="hljs-operator"><span class="hljs-keyword" style="color: rgb(133, 153, 0);">release</span>.x86_64                     <span class="hljs-number" style="color: rgb(42, 161, 152);">0.0</span>-<span class="hljs-number" style="color: rgb(42, 161, 152);">1</span>                       installed
...
Percona-<span class="hljs-keyword" style="color: rgb(133, 153, 0);">Server</span>-client-<span class="hljs-number" style="color: rgb(42, 161, 152);">51.</span>x86_64            <span class="hljs-number" style="color: rgb(42, 161, 152);">5.1</span><span class="hljs-number" style="color: rgb(42, 161, 152);">.47</span>-rel11<span class="hljs-number" style="color: rgb(42, 161, 152);">.1</span><span class="hljs-number" style="color: rgb(42, 161, 152);">.51</span>.rhel5     percona
Percona-<span class="hljs-keyword" style="color: rgb(133, 153, 0);">Server</span>-devel-<span class="hljs-number" style="color: rgb(42, 161, 152);">51.</span>x86_64             <span class="hljs-number" style="color: rgb(42, 161, 152);">5.1</span><span class="hljs-number" style="color: rgb(42, 161, 152);">.47</span>-rel11<span class="hljs-number" style="color: rgb(42, 161, 152);">.1</span><span class="hljs-number" style="color: rgb(42, 161, 152);">.51</span>.rhel5     percona
Percona-<span class="hljs-keyword" style="color: rgb(133, 153, 0);">Server</span>-<span class="hljs-keyword" style="color: rgb(133, 153, 0);">server</span>-<span class="hljs-number" style="color: rgb(42, 161, 152);">51.</span>x86_64            <span class="hljs-number" style="color: rgb(42, 161, 152);">5.1</span><span class="hljs-number" style="color: rgb(42, 161, 152);">.47</span>-rel11<span class="hljs-number" style="color: rgb(42, 161, 152);">.1</span><span class="hljs-number" style="color: rgb(42, 161, 152);">.51</span>.rhel5     percona
Percona-<span class="hljs-keyword" style="color: rgb(133, 153, 0);">Server</span>-shared-<span class="hljs-number" style="color: rgb(42, 161, 152);">51.</span>x86_64            <span class="hljs-number" style="color: rgb(42, 161, 152);">5.1</span><span class="hljs-number" style="color: rgb(42, 161, 152);">.47</span>-rel11<span class="hljs-number" style="color: rgb(42, 161, 152);">.1</span><span class="hljs-number" style="color: rgb(42, 161, 152);">.51</span>.rhel5     percona
Percona-<span class="hljs-keyword" style="color: rgb(133, 153, 0);">Server</span>-test-<span class="hljs-number" style="color: rgb(42, 161, 152);">51.</span>x86_64              <span class="hljs-number" style="color: rgb(42, 161, 152);">5.1</span><span class="hljs-number" style="color: rgb(42, 161, 152);">.47</span>-rel11<span class="hljs-number" style="color: rgb(42, 161, 152);">.1</span><span class="hljs-number" style="color: rgb(42, 161, 152);">.51</span>.rhel5     percona
...
xtrabackup.x86_64                          <span class="hljs-number" style="color: rgb(42, 161, 152);">1.2</span>-<span class="hljs-number" style="color: rgb(42, 161, 152);">22.</span>rhel5                percona
</span></code>

2、安装 xtrabackup

yum install percona-xtrabackup

创建备份

innobackupex --user=DBUSER --password=DBUSERPASS /path/to/BACKUP-DIR/

如果执行正确,其输出信息通常类似:

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;">innobackupex: <span class="hljs-operator"><span class="hljs-keyword" style="color: rgb(133, 153, 0);">Backup</span> created <span class="hljs-keyword" style="color: rgb(133, 153, 0);">in</span> directory <span class="hljs-string" style="color: rgb(42, 161, 152);">'/path/to/BACKUP-DIR/2015-03-03_00-00-09'</span>
innobackupex: MySQL <span class="hljs-keyword" style="color: rgb(133, 153, 0);">binlog</span> <span class="hljs-keyword" style="color: rgb(133, 153, 0);">position</span>: filename <span class="hljs-string" style="color: rgb(42, 161, 152);">'mysql-bin.000003'</span>, <span class="hljs-keyword" style="color: rgb(133, 153, 0);">position</span> <span class="hljs-number" style="color: rgb(42, 161, 152);">1946</span>
<span class="hljs-number" style="color: rgb(42, 161, 152);">111225</span> <span class="hljs-number" style="color: rgb(42, 161, 152);">00</span>:<span class="hljs-number" style="color: rgb(42, 161, 152);">00</span>:<span class="hljs-number" style="color: rgb(42, 161, 152);">53</span>  innobackupex: completed OK!
</span></code>

备份时,innobackupex 会调用 xtrabackup 备份 InnoDB 表的数据,并且会复制 MyISAM, MERGE,CSV 和 ARCHIVE 表的表定义文件(.frm 文件)、数据文件。同时还会备份触发器和数据库配置信息相关的文件。这些文件将会保存在指定备份目录中一个以时间戳命名的目录下。

准备备份

一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。因此,此时数据文件仍处理不一致状态。“准备”的主要作用正是通过回滚未提交的事务及同步已经提交的事务至数据文件也使得数据文件处于一致性状态。

innobackupex --apply-log /path/to/BACKUP-DIR

如果执行正确,其最后输出的几行信息通常如下:

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;">xtrabackup: starting <span class="hljs-keyword" style="color: rgb(133, 153, 0);">shutdown</span> with innodb_fast_shutdown = <span class="hljs-number" style="color: rgb(42, 161, 152);">1</span>
<span class="hljs-number" style="color: rgb(42, 161, 152);">120407</span>  <span class="hljs-number" style="color: rgb(42, 161, 152);">9</span>:<span class="hljs-number" style="color: rgb(42, 161, 152);">01</span>:<span class="hljs-number" style="color: rgb(42, 161, 152);">36</span>  InnoDB: Starting <span class="hljs-keyword" style="color: rgb(133, 153, 0);">shutdown</span>...
<span class="hljs-number" style="color: rgb(42, 161, 152);">120407</span>  <span class="hljs-number" style="color: rgb(42, 161, 152);">9</span>:<span class="hljs-number" style="color: rgb(42, 161, 152);">01</span>:<span class="hljs-number" style="color: rgb(42, 161, 152);">40</span>  InnoDB: Shutdown completed; <span class="hljs-keyword" style="color: rgb(133, 153, 0);">log</span> sequence number <span class="hljs-number" style="color: rgb(42, 161, 152);">92036620</span>
<span class="hljs-number" style="color: rgb(42, 161, 152);">120407</span> 09:<span class="hljs-number" style="color: rgb(42, 161, 152);">01</span>:<span class="hljs-number" style="color: rgb(42, 161, 152);">40</span>  innobackupex: completed OK!
</code>

在实现“准备”的过程中,innobackupex 通常还可以使用 --use-memory 选项来指定其可以使用的内存的大小,默认通常为 100M。如果有足够的内存可用,可以多划分一些内存给 prepare 的过程,以提高其完成速度。

恢复备份

将数据复制到从服务器上:
scp -r /path/to/BACKUP-DIR root@slave_host:/data/

在从服务器中恢复备份数据:
innobackupex --copy-back /path/to/BACKUP-DIR

如果服务器剩余空间不足,你可以使用 --move-back 替换掉 --copy-back

如果执行正确,其输出信息的最后几行通常如下:

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;">innobackupex: Starting to copy InnoDB <span class="hljs-built_in" style="color: rgb(38, 139, 210);">log</span> files
innobackupex: <span class="hljs-keyword" style="color: rgb(133, 153, 0);">in</span> <span class="hljs-string" style="color: rgb(42, 161, 152);">'/backup/2012-04-07_08-17-03'</span>
innobackupex: back to original InnoDB <span class="hljs-built_in" style="color: rgb(38, 139, 210);">log</span> directory <span class="hljs-string" style="color: rgb(42, 161, 152);">'/mydata/data'</span>
innobackupex: Finished copying back files.
...
<span class="hljs-number" style="color: rgb(42, 161, 152);">120407</span> <span class="hljs-number" style="color: rgb(42, 161, 152);">09</span>:<span class="hljs-number" style="color: rgb(42, 161, 152);">36</span>:<span class="hljs-number" style="color: rgb(42, 161, 152);">10</span>  innobackupex: completed OK!
</code>

启动从库 MySQL,设置主库信息

当数据恢复至数据目录以后,还需要确保所有数据文件的属主和属组均为正确的用户,如mysql,否则,在启动mysqld之前还需要事先修改数据文件的属主和属组。如:

chown -R mysql:mysql /mydata/data/

启动从库:

/etc/init.d/mysqld start

在主库中开设主从用的账号和权限:

GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.0.1' IDENTIFIED BY 'slave';

查看备份文件 xtrabackup_binlog_info 中的日志文件以及position。

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;"><span class="hljs-operator"><span class="hljs-keyword" style="color: rgb(133, 153, 0);">CHANGE</span> <span class="hljs-keyword" style="color: rgb(133, 153, 0);">MASTER</span> <span class="hljs-keyword" style="color: rgb(133, 153, 0);">TO</span>
MASTER_HOST=<span class="hljs-string" style="color: rgb(42, 161, 152);">'<master_host>'</span>,
MASTER_USER=<span class="hljs-string" style="color: rgb(42, 161, 152);">'<slave_username>'</span>,
MASTER_PASSWORD=<span class="hljs-string" style="color: rgb(42, 161, 152);">'<slave_password>'</span>,
MASTER_LOG_FILE=<span class="hljs-string" style="color: rgb(42, 161, 152);">'<see xtrabackup_binlog_info>'</span>,
MASTER_LOG_POS=<see xtrabackup_binlog_info>;</span>
</code>

开启主从同步:

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;"><span class="hljs-operator"><span class="hljs-keyword" style="color: rgb(133, 153, 0);">START</span> <span class="hljs-keyword" style="color: rgb(133, 153, 0);">SLAVE</span>;</span>
</code>

查看从库状态:

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;"><span class="hljs-operator"><span class="hljs-keyword" style="color: rgb(133, 153, 0);">SHOW</span> <span class="hljs-keyword" style="color: rgb(133, 153, 0);">SLAVE</span> <span class="hljs-keyword" style="color: rgb(133, 153, 0);">STATUS</span>;</span>
</code>

Ps:用 innobackupex 备份数据时,–apply-log 处理过的备份数据里有两个文件说明该备份数据对应的 binlog 的文件名和位置。但有时这俩文件说明的位置可能会不同。
1 对于纯 InnoDB 操作,备份出来的数据中上述两个文件的内容是一致的
2 对于 InnoDB 和非事务存储引擎混合操作,xtrabackup_binlog_info 中所示的 position 应该会比 xtrabackup_pos_innodb 所示的数值大。此时应以 xtrabackup_binlog_info 为准;而后者和 apply-log 时 InnoDB recovery log 中显示的内容是一致的,只针对 InnoDB 这部分数据。

Ps2:启动 MySQL 时,遇到权限问题的解决方法:

报错信息:

<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; white-space: inherit; background-color: transparent;"><span class="hljs-number" style="color: rgb(42, 161, 152);">150430</span> <span class="hljs-number" style="color: rgb(42, 161, 152);">14</span>:<span class="hljs-number" style="color: rgb(42, 161, 152);">41</span>:<span class="hljs-number" style="color: rgb(42, 161, 152);">16</span>  <span class="hljs-attribute" style="color: rgb(181, 137, 0);">InnoDB</span>: Operating system error number <span class="hljs-number" style="color: rgb(42, 161, 152);">13</span> <span class="hljs-keyword" style="color: rgb(133, 153, 0);">in</span> a file operation.
<span class="hljs-attribute" style="color: rgb(181, 137, 0);">InnoDB</span>: The error means mysqld does <span class="hljs-keyword" style="color: rgb(133, 153, 0);">not</span> have the access rights to
<span class="hljs-attribute" style="color: rgb(181, 137, 0);">InnoDB</span>: the directory.
</code>

解决方法:
chown -R mysql:mysql /home/data/mysql
chcon -R -t mysqld_db_t /home/mysql

这篇关于XtraBackup 实现不停机不锁表搭建mysql主从的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/708304

相关文章

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

java.sql.SQLTransientConnectionException连接超时异常原因及解决方案

《java.sql.SQLTransientConnectionException连接超时异常原因及解决方案》:本文主要介绍java.sql.SQLTransientConnectionExcep... 目录一、引言二、异常信息分析三、可能的原因3.1 连接池配置不合理3.2 数据库负载过高3.3 连接泄漏

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、