译文 Streaming MySQL Backups with Percona XtraBackup – Another Alternative

Streaming MySQL Backups with Percona XtraBackup – Another Alternative

使用Percona XtraBackup流式传输MySQL备份-另一种选择

今天我将向你介绍另一种使用Percona XtraBackup在服务器间创建并传递备份的方式. 这个方法和网上的其他方法有什么不同吗? 其实并没有很大不同, 但是在性能和可用性方面很有趣.

我们将xbstream实用程序与pigz和socat的功能结合在一起, 以在拥有多个处理器的情况下利用多处理功能, 同时在此组件成为瓶颈的情况下, 减少对网络带宽的使用. 因此, 让我们解释每个组件:

socat: 代表SOcket CAT. 它是一个用于在两个地址之间进行数据传输的实用程序.

使socat如此通用的原因是地址可以代表网络套接字, 任何文件描述符, Unix域数据报或流套接字, TCP和UDP(在IPv4和IPv6上), 在IPv4 / IPv6上的SOCKS 4 / 4a, SCTP , PTY, 数据报和流套接字, 命名管道和未命名管道, 原始IP套接字, OpenSSL, 或者在Linux上甚至任何任意网络设备上

Pigz: 它代表gzip的并行实现, 它是gzip的全功能替代品, 在压缩数据时利用了多个处理器和多个内核.

xbstream : (具有并行性)并行处理多个文件.

所需软件包: pigz, socat, 当然还有 Percona XtraBackup

以下示例操作中的环境信息

Source: 进行备份的来源数据库(MySQL 5.7 installed on CentOS 7.8)

Target: 备份将发送到的目的地(MySQL 5.7 installed on CentOS 7.8)

步骤

  1. SourceTarget安装依赖包
1
2
Source # yum install -y pigz socat
Target # yum install -y pigz socat

如果你尚未在源端和目标端安装Percona XtraBackup, 请参照以下步骤https://www.percona.com/doc/percona-xtrabackup/2.4/index.html#installation

确保您拥有一个具有适当特权的用户, 可以在数据库上进行备份:

1
2
3
4
5
+---------------------------------------------------------------------------+
| Grants for bkpuser@localhost |
+---------------------------------------------------------------------------+
| GRANT RELOAD, PROCESS, REPLICATION CLIENT ON *.* TO 'bkpuser'@'localhost' |
+---------------------------------------------------------------------------+
  1. Target执行:

停止当前的数据库服务(如果有):

1
Target # systemctl stop mysqld

删除datadir内容(假设它已使用默认设置安装), 并确保您已登录到目标服务器!

1
Target # rm -rf /var/lib/mysql/*

最后, 我们将执行命令以从源(Source)接收备份:

1
Target # socat -u TCP-LISTEN:4444,reuseaddr stdio | pigz -dc -p 4 - | xbstream —p 4 -x -C /var/lib/mysql
  1. Source执行命令以将备份发送到目标(Target).
1
Source # xtrabackup --defaults-file=/etc/my.cnf --backup --user=bkpuser --password=Bkpuser123! --stream=xbstream --parallel 4 --no-timestamp --target-dir=/tmp | pigz -k -1 -p4 - | socat -u stdio TCP:Target:4444

您将看到类似以下的输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
xtrabackup: recognized server arguments: --datadir=/var/lib/mysql --server-id=1 --log_bin=/var/lib/mysql/mysql-bin --innodb_log_file_size=200M --innodb_log_files_in_group=2 --open_files_limit=65535 --parallel=4
xtrabackup: recognized client arguments: --backup=1 --user=bkpuser --password=* --stream=xbstream --target-dir=/tmp
200822 11:10:16 version_check Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup' as 'bkpuser' (using password: YES).
200822 11:10:16 version_check Connected to MySQL server
200822 11:10:16 version_check Executing a version check against the server...
200822 11:10:16 version_check Done.
200822 11:10:16 Connecting to MySQL server host: localhost, user: bkpuser, password: set, port: not set, socket: not set
Using server version 5.7.30-log
xtrabackup version 2.4.20 based on MySQL server 5.7.26 Linux (x86_64) (revision id: c8b4056)
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /var/lib/mysql
xtrabackup: open files limit requested 65535, set to 65535
xtrabackup: using the following InnoDB configuration:
xtrabackup: innodb_data_home_dir = .
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 209715200
InnoDB: Number of pools: 1
200822 11:10:16 >> log scanned up to (6724690490)
xtrabackup: Generating a list of tablespaces
InnoDB: Allocated tablespace ID 2 for mysql/plugin, old maximum was 0
xtrabackup: Starting 4 threads for parallel data files transfer
200822 11:10:16 [01] Streaming ./ibdata1
...etc
  1. 完成第3步后, 您将在Target节点上看到如下输出:
1
2
3
4
5
6
7
8
...
MySQL binlog position: filename 'mysql-bin.000091', position '102205647'
200822 11:10:21 [00] Streaming <STDOUT>
200822 11:10:21 [00] ...done
200822 11:10:21 [00] Streaming <STDOUT>
200822 11:10:21 [00] ...done
xtrabackup: Transaction log of lsn (4308505553) to (4308505562) was copied.
200822 11:10:21 completed OK!

第2步也将完成, 因此您必须在Target节点中执行以下命令:

1
Target # xtrabackup --prepare --use-memory=1G --target-dir=/var/lib/mysql/

从文档中:

使用xtrabackup –backup选项进行备份后, 首先需要准备它以进行还原. 在准备好数据文件之前, 它们在时间点上是不一致的, 因为它们是在程序运行时在不同的时间复制的, 并且在此过程中它们可能已被更改. 如果您尝试使用这些数据文件启动InnoDB, 它将检测到损坏并自身崩溃, 以防止您在损坏的数据上运行. 该xtrabackup -prepare步骤使得文件在单个时间瞬间完全一致, 这样你就可以在上面运行的InnoDB.

1
2
Target # chown -R mysql:mysql /var/lib/mysql/*
Target # systemctl start mysqld

您已经克隆了一个新数据库!

当然, 您可以将新数据库设置为副本, 并在目标节点中执行以下附加步骤:

查看文件xtrabackup_binlog_info的内容, 它将类似于:

1
2
3
Target # cat /var/lib/mysql/xtrabackup_binlog_info

mysql-bin.000091 102205647

(我们假设在数据库中创建了以下用户/授权, 如果没有, 请创建它)

1
2
3
4
5
6
mysql> show grants for replicator;
+----------------------------------------------------+
| Grants for replicator@% |
+----------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'replicator'@'%' |
+----------------------------------------------------+

连接到数据库, 然后运行:

1
Target # mysql -u root -p
1
2
mysql> change master to master_host='Source',master_port=3306,master_user='replicator',master_password='R3pl1c4t10n!',master_log_file='mysql-bin.000091',master_log_pos=102205647;
Query OK, 0 rows affected (0.00 sec)
1
2
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
1
mysql> pager egrep -i "Master_Host|Master_User|Master_Port|file|behind"
1
2
3
4
5
6
7
8
9
10
11
12
13
mysql> show slave status\G
Master_Host: master
Master_User: replicator
Master_Port: 3306
Master_Log_File: mysql-bin.000091
Relay_Log_File: relay.000001
Relay_Master_Log_File: mysql-bin.000091
Until_Log_File:
Master_SSL_CA_File:
Seconds_Behind_Master: 0
Master_Info_File: mysql.slave_master_info

1 row in set (0.00 sec)

就这样. 直播愉快!

Powered by Hexo and Hexo-theme-hiker

Copyright © 2013 - 2022 Fan() All Rights Reserved.

访客数 : | 访问量 :