Home Database Mysql Tutorial 通过 XtraBackup 实现不停机不锁表搭建MySQL主从同步

通过 XtraBackup 实现不停机不锁表搭建MySQL主从同步

Jun 07, 2016 pm 03:10 PM
xtrabackup

Xtrabackup是由 Percona 开发的一个开源软件,可实现对 InnoDB 的数据备份,支持在线热备份(备份时不影响数据读写)。备份时,X

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

XtraBackup 有两个工具:xtrabackup 和 innobackupex:

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

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

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

    1、Master

    vim /etc/my.cnf

    2、Slave:

    vim /etc/my.cnf

    server-id=2 datadir=/var/lib/mysql 安装 XtraBackup 1、添加源

    yum install

    检查是否添加成功:

    yum list | grep percona

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

    percona-release.x86_64 0.0-1 installed ... Percona-.rhel5 percona Percona-.rhel5 percona Percona-.rhel5 percona Percona-.rhel5 percona Percona-.rhel5 percona ... xtrabackup.x86_64 1.2-22.rhel5 percona 2、安装 xtrabackup

    yum install percona-xtrabackup

    创建备份

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

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

    innobackupex: Backup created in directory '/path/to/BACKUP-DIR/2015-03-03_00-00-09' innobackupex: MySQL :00:53 innobackupex: completed OK!

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

    准备备份

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

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

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

    xtrabackup: starting :01:36 InnoDB: Starting shutdown... 09:01:40 innobackupex: completed OK!

    在实现“准备”的过程中,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。

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

    innobackupex: Starting to copy InnoDB log files innobackupex: in '/backup/2012-04-07_08-17-03' innobackupex: back to original InnoDB log directory '/mydata/data' innobackupex: Finished copying back files. ... 120407 09:36:10 innobackupex: completed OK! 启动从库 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。

    MASTER_HOST='', MASTER_USER='', MASTER_PASSWORD='', MASTER_LOG_FILE='', MASTER_LOG_POS=;

    开启主从同步:

    START SLAVE;

    查看从库状态:

    ;

    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 时,遇到权限问题的解决方法:

    报错信息:

    : Operating system error number 13 in a file operation. InnoDB: The error means mysqld does not have the access rights to InnoDB: the directory.

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

    参考资料:

    1、
    2、

    MySQL管理之使用XtraBackup进行热备

    MySQL开源备份工具Xtrabackup备份部署

    MySQL Xtrabackup备份和恢复

    用XtraBackup实现MySQL的主从复制快速部署【主不锁表】

    安装和使用 Percona 推出的 Xtrabackup 备份 MySQL

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

    本文永久更新链接地址

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

    This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

    How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

    This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

    How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

    The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

    Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

    This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

    What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

    This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

    Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

    This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

    How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

    Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

    What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

    Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

    See all articles