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处理跨域的实现方式(附Demo)

《Springboot处理跨域的实现方式(附Demo)》:本文主要介绍Springboot处理跨域的实现方式(附Demo),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录Springboot处理跨域的方式1. 基本知识2. @CrossOrigin3. 全局跨域设置4.

Spring Boot 3.4.3 基于 Spring WebFlux 实现 SSE 功能(代码示例)

《SpringBoot3.4.3基于SpringWebFlux实现SSE功能(代码示例)》SpringBoot3.4.3结合SpringWebFlux实现SSE功能,为实时数据推送提供... 目录1. SSE 简介1.1 什么是 SSE?1.2 SSE 的优点1.3 适用场景2. Spring WebFlu

基于SpringBoot实现文件秒传功能

《基于SpringBoot实现文件秒传功能》在开发Web应用时,文件上传是一个常见需求,然而,当用户需要上传大文件或相同文件多次时,会造成带宽浪费和服务器存储冗余,此时可以使用文件秒传技术通过识别重复... 目录前言文件秒传原理代码实现1. 创建项目基础结构2. 创建上传存储代码3. 创建Result类4.

SpringBoot日志配置SLF4J和Logback的方法实现

《SpringBoot日志配置SLF4J和Logback的方法实现》日志记录是不可或缺的一部分,本文主要介绍了SpringBoot日志配置SLF4J和Logback的方法实现,文中通过示例代码介绍的非... 目录一、前言二、案例一:初识日志三、案例二:使用Lombok输出日志四、案例三:配置Logback一

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

Python+PyQt5实现多屏幕协同播放功能

《Python+PyQt5实现多屏幕协同播放功能》在现代会议展示、数字广告、展览展示等场景中,多屏幕协同播放已成为刚需,下面我们就来看看如何利用Python和PyQt5开发一套功能强大的跨屏播控系统吧... 目录一、项目概述:突破传统播放限制二、核心技术解析2.1 多屏管理机制2.2 播放引擎设计2.3 专

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

idea中创建新类时自动添加注释的实现

《idea中创建新类时自动添加注释的实现》在每次使用idea创建一个新类时,过了一段时间发现看不懂这个类是用来干嘛的,为了解决这个问题,我们可以设置在创建一个新类时自动添加注释,帮助我们理解这个类的用... 目录前言:详细操作:步骤一:点击上方的 文件(File),点击&nbmyHIgsp;设置(Setti

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)的解决方法

《mysql出现ERROR2003(HY000):Can‘tconnecttoMySQLserveron‘localhost‘(10061)的解决方法》本文主要介绍了mysql出现... 目录前言:第一步:第二步:第三步:总结:前言:当你想通过命令窗口想打开mysql时候发现提http://www.cpp