Home  >  Article  >  Database  >  Take a look at the new features of MySQL 8 Clone Plugin

Take a look at the new features of MySQL 8 Clone Plugin

coldplay.xixi
coldplay.xixiforward
2020-10-29 17:30:241980browse

mysql video tutorialThe column introduces the new feature of MySQL 8 Clone Plugin

Take a look at the new features of MySQL 8 Clone Plugin

##Clone Plugin It is a major feature introduced in MySQL 8.0.17. Why should we implement this feature? Personally, I feel that it mainly serves Group Replication. In Group Replication, a new node is added, and differential data is completed through distributed recovery (Distributed Recovery).

Before MySQL 8.0.17, only one recovery method-Binlog was supported. But if the Binlog required by the new node has been purged, at this time, you can only use backup tools (XtraBackup, mydumper, mysqldump) to synchronize the full data, and then synchronize the incremental data through distributed recovery.

Although this method can also achieve the purpose of adding nodes, it still requires the help of external tools, which requires a certain amount of work and usage threshold. You should know that its competitor, PXC, integrates XtraBackup by default for State Snapshot Transfer (similar to full synchronization), while MongoDB goes one step further and natively implements Initial Sync to synchronize full data. From the perspective of ease of use, MySQL is indeed inferior to its competitors in terms of adding nodes to the cluster. In terms of customer experience, there is still a lot of room for improvement.

Fortunately, MySQL officials also recognized this gap and finally implemented the Clone Plugin in MySQL 8.0.17. Of course, for officials, it is not difficult to implement this feature. After all, there is a ready-made physical backup tool (MySQL Enterprise Backup) for reference.

This article will expand on the following aspects:

    Installation of Clone Plugin
  1. Usage of Clone Plugin
  2. How to view clones Progress of the operation
  3. How to build a slave library based on clone data
  4. Implementation details of Clone Plugin
  5. Restrictions of Clone Plugin
  6. Comparison between Clone Plugin and XtraBackup
  7. Parameter analysis of Clone Plugin
1. Installation of Clone Plugin

Clone Plugin supports the following two installation methods:

(1) Configuration file specification

[mysqld]
plugin-load-add=mysql_clone.so
clone=FORCE_PLUS_PERMANENT复制代码
The clone here, strictly speaking, is not the parameter name, but the plug-in name, which can be added or not. FORCE_PLUS_PERMANENT controls the behavior of the plug-in.

has four values:

    ON** (**Enable the plug-in)
  • OFF (Disable the plug-in)
  • FORCE (Force Enable. If the plug-in initialization fails, MySQL will not start)
  • FORCE_PLUS_PERMANENT (On the basis of FORCE, uninstalling the plug-in through the UNINSTALL PLUGIN command is not allowed).

(2) Dynamic loading

[mysqld]
plugin-load-add=mysql_clone.so
clone=FORCE_PLUS_PERMANENT复制代码
Check whether the plug-in is installed successfully

mysql> show plugins;
...
| clone                           | ACTIVE   | CLONE              | mysql_clone.so | GPL     |
...复制代码
The clone status is displayed as "ACTIVE", which means the plug-in is loaded successfully. .

2. Use of Clone Plugin

Clone Plugin supports two cloning methods: local cloning and remote cloning.

1. Local cloning

Local cloning is initiated locally on the instance, and its syntax is as follows:

CLONE LOCAL DATA DIRECTORY [=] 'clone_dir';复制代码
Among them, clone_dir is the clone Table of contents.

Let’s take a look at a specific Demo.

Create clone user

mysql> create user 'clone_user'@'%' identified by 'clone_pass';
mysql> grant backup_admin on *.* to 'clone_user'@'%';复制代码
Create clone directory

# mkdir /data/mysql
# chown -R mysql.mysql /data/mysql复制代码
Create local clone

# mysql -uclone_user -pclone_pass
mysql> clone local data directory='/data/mysql/3307';复制代码
Among them, "/data/mysql/3307" is the clone directory, It needs to meet the following requirements:

    The clone directory must be an absolute path.
  1. "/data/mysql" must exist and MySQL has writable permissions on it.
  2. 3307 cannot exist.
View the contents of the clone directory

# ll /data/mysql/3307
total 172996
drwxr-x--- 2 mysql mysql       89 May 24 22:37 #clone
-rw-r----- 1 mysql mysql     3646 May 24 22:37 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 May 24 22:37 ibdata1
-rw-r----- 1 mysql mysql 50331648 May 24 22:37 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 May 24 22:37 ib_logfile1
drwxr-x--- 2 mysql mysql        6 May 24 22:37 mysql
-rw-r----- 1 mysql mysql 25165824 May 24 22:37 mysql.ibd
drwxr-x--- 2 mysql mysql       20 May 24 22:37 slowtech
drwxr-x--- 2 mysql mysql       28 May 24 22:37 sys
-rw-r----- 1 mysql mysql 10485760 May 24 22:37 undo_001
-rw-r----- 1 mysql mysql 11534336 May 24 22:37 undo_002复制代码
Compared with Xtrabackup, there is no need to prepare and it can be started directly.

# /usr/local/mysql/bin/mysqld --no-defaults --datadir=/data/mysql/3307 --user mysql --port 3307 &复制代码
2. Remote cloning

#Remote cloning involves two instances. Among them, the instance to be cloned is Donor, and the instance that accepts cloned data It is Recipient. The cloning command needs to be initiated on the Recipient, and the syntax is as follows:

CLONE INSTANCE FROM 'user'@'host':port
IDENTIFIED BY 'password'
[DATA DIRECTORY [=] 'clone_dir']
[REQUIRE [NO] SSL];复制代码
Among them, host and port are the IP and port of the instance to be cloned (Donor), user and password are the clone user and password on Donor, and backup_admin is required Permissions, such as clone_user created above.

DATA DIRECTORY specifies the backup directory. If not specified, it will be cloned to the Recipient's data directory by default.

REQUIRE [NO] SSL, whether to enable SSL communication.

Below, let’s take a look at the specific Demo.

First, create a clone user on the Donor instance and load the Clone Plugin.

mysql> create user 'donor_user'@'%' identified by 'donor_pass';
mysql> grant backup_admin on *.* to 'donor_user'@'%';
mysql> install plugin clone soname 'mysql_clone.so';复制代码
backup_admin is the required permission for cloning operation.

Next, create a clone user on the Recipient instance and load the Clone Plugin.

mysql> create user 'recipient_user'@'%' identified by 'recipient_pass';
mysql> grant clone_admin on *.* to 'recipient_user'@'%';
mysql> install plugin clone soname 'mysql_clone.so';复制代码

这里的clone_admin,隐式含有backup_admin(阻塞DDL)和shutdown(重启实例)权限。

设置Donor白名单。Recipient只能克隆白名单中的实例。

mysql> set global clone_valid_donor_list = '192.168.244.10:3306';复制代码

设置该参数需要SYSTEM_VARIABLES_ADMIN权限。

在Recipient上发起克隆命令

# mysql -urecipient_user -precipient_pass
mysql> clone instance from 'donor_user'@'192.168.244.10':3306 identified by 'donor_pass';
Query OK, 0 rows affected (36.97 sec)复制代码

远程克隆会依次进行以下操作:

**(1)****获取备份锁。**备份锁和DDL互斥。注意,不仅仅是Recipient,Donor上的备份锁同样会获取。

**(2)****DROP用户表空间。**注意,DROP的只是用户数据,不是数据目录,也不包括mysql,ibdata等系统表空间。

**(3)从Donor实例拷贝数据。**对于用户表空间,会直接拷贝,如果是系统表空间 ,则会重命名为xxx.#clone,不会直接替代原文件。

 ll /data/mysql/3306/data/
...
-rw-r----- 1 mysql mysql     3646 May 25 07:20 ib_buffer_pool
-rw-r----- 1 mysql mysql     3646 May 27 07:31 ib_buffer_pool.#clone
-rw-r----- 1 mysql mysql 12582912 May 27 07:31 ibdata1
-rw-r----- 1 mysql mysql 12582912 May 27 07:31 ibdata1.#clone
-rw-r----- 1 mysql mysql 50331648 May 27 07:32 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 May 27 07:31 ib_logfile0.#clone
...
-rw-r----- 1 mysql mysql 25165824 May 27 07:31 mysql.ibd
-rw-r----- 1 mysql mysql 25165824 May 27 07:31 mysql.ibd.#clone
...复制代码

**(4)重启实例。**在启动的过程中,会用xxx.#clone替换掉原来的系统表空间文件。

三、如何查看克隆操作的进度

查看克隆操作的进度主要依托于performance_schema.clone_status和performance_schema.clone_progress这两张表。

首先看看performance_schema.clone_status表。

mysql> select * from performance_schema.clone_status\G
*************************** 1\. row ***************************
             ID: 1
            PID: 0
          STATE: Completed
     BEGIN_TIME: 2020-05-27 07:31:24.220
       END_TIME: 2020-05-27 07:33:08.185
         SOURCE: 192.168.244.10:3306
    DESTINATION: LOCAL INSTANCE
       ERROR_NO: 0
  ERROR_MESSAGE:
    BINLOG_FILE: mysql-bin.000009
BINLOG_POSITION: 665197555
  GTID_EXECUTED: 59cd4f8f-8fa1-11ea-a0fe-000c29f66609:1-560
1 row in set (0.06 sec)复制代码

顾名思义,该表记录了克隆操作的当前状态。

其中,

  • **PID:**Processlist ID。对应show processlist中的Id,如果要终止当前的克隆操作,执行kill processlist_id命令即可。

  • **STATE:**克隆操作的状态,Not Started(克隆尚未开始),In Progress(克隆中),Completed(克隆成功),Failed(克隆失败)。如果是Failed状态,ERROR_NO,ERROR_MESSAGE会给出具体的错误编码和错误信息。

  • **BEGIN_TIME,END_TIME:**克隆操作开始,结束时间。

  • **SOURCE:**Donor实例的地址。

  • **DESTINATION:**克隆目录。“LOCAL INSTANCE”代表当前实例的数据目录。

  • **GTID_EXECUTED,BINLOG_FILE(BINLOG_POSITION):**克隆操作结束时,主库已经执行的GTID集合,及一致性位置点。可利用这些信息来搭建从库。

接下来看看performance_schema.clone_progress表。

mysql> select * from performance_schema.clone_progress;
+------+-----------+-----------+----------------------------+----------------------------+---------+-----------+-----------+-----------+------------+---------------+
| ID   | STAGE     | STATE     | BEGIN_TIME                 | END_TIME                   | THREADS | ESTIMATE  | DATA      | NETWORK   | DATA_SPEED | NETWORK_SPEED |
+------+-----------+-----------+----------------------------+----------------------------+---------+-----------+-----------+-----------+------------+---------------+
|    1 | DROP DATA | Completed | 2020-05-27 07:31:28.581661 | 2020-05-27 07:31:35.855706 |       1 |         0 |         0 |         0 |          0 |             0 |
|    1 | FILE COPY | Completed | 2020-05-27 07:31:35.855952 | 2020-05-27 07:31:58.270881 |       2 | 482463294 | 482463294 | 482497011 |          0 |             0 |
|    1 | PAGE COPY | Completed | 2020-05-27 07:31:58.271250 | 2020-05-27 07:31:58.719085 |       2 |  10977280 |  10977280 |  11014997 |          0 |             0 |
|    1 | REDO COPY | Completed | 2020-05-27 07:31:58.720128 | 2020-05-27 07:31:58.930804 |       2 |    465408 |    465408 |    465903 |          0 |             0 |
|    1 | FILE SYNC | Completed | 2020-05-27 07:31:58.931094 | 2020-05-27 07:32:01.063325 |       2 |         0 |         0 |         0 |          0 |             0 |
|    1 | RESTART   | Completed | 2020-05-27 07:32:01.063325 | 2020-05-27 07:32:59.844119 |       0 |         0 |         0 |         0 |          0 |             0 |
|    1 | RECOVERY  | Completed | 2020-05-27 07:32:59.844119 | 2020-05-27 07:33:08.185367 |       0 |         0 |         0 |         0 |          0 |             0 |
+------+-----------+-----------+----------------------------+----------------------------+---------+-----------+-----------+-----------+------------+---------------+
7 rows in set (0.00 sec)复制代码

该表记录了克隆操作的进度信息。

  • **STAGE:**一个克隆操作可依次细分为DROP DATA,FILE COPY,PAGE COPY,REDO COPY,FILE SYNC,RESTART,RECOVERY等7个阶段。当前阶段结束了才会开始下一个阶段。

  • **STATE:**当前阶段的状态。有三种状态:Not Started,In Progress,Completed。

  • **BEGIN_TIME,END_TIME:**当前阶段的开始时间和结束时间。

  • **THREADS:**当前阶段使用的并发线程数。

  • **ESTIMATE:**预估的数据量。

  • **DATA:**已经拷贝的数据量。

  • **NETWORK:**通过网络传输的数据量。如果是本地克隆,该列的值为0。

  • **DATA_SPEED,NETWORK_SPEED:**当前数据拷贝的速率和网络传输的速率。

    注意,是当前值。

四、如何基于克隆数据搭建从库

在前面,我们介绍过performance_schema.clone_status表,该表会记录Donor实例的一致性位置点信息。我们可以利用这些信息来搭建从库。

mysql> select * from performance_schema.clone_status\G
*************************** 1\. row ***************************
...
    BINLOG_FILE: mysql-bin.000009
BINLOG_POSITION: 665197555
  GTID_EXECUTED: 59cd4f8f-8fa1-11ea-a0fe-000c29f66609:1-560
1 row in set (0.06 sec)复制代码

这里,区分两种场景,GTID复制和基于位置点的复制。

1、GTID复制

mysql> CHANGE MASTER TO MASTER_HOST = 'master_host_name', MASTER_PORT = master_port_num,
       ...
       MASTER_AUTO_POSITION = 1;
mysql> START SLAVE;复制代码

需要注意的是,无需额外执行set global gtid_purged操作。通过克隆数据启动的实例,gtid_purged已经初始化完毕。

mysql> show global variables like 'gtid_purged';
+---------------+--------------------------------------------+
| Variable_name | Value                                      |
+---------------+--------------------------------------------+
| gtid_purged   | 59cd4f8f-8fa1-11ea-a0fe-000c29f66609:1-560 |
+---------------+--------------------------------------------+
1 row in set (0.00 sec)复制代码

2、基于位置点的复制

这里,同样要区分两种场景。

场景1,Recipient要作为Donor的从库。

mysql> SELECT BINLOG_FILE, BINLOG_POSITION FROM performance_schema.clone_status; 
mysql> CHANGE MASTER TO MASTER_HOST = 'master_host_name', MASTER_PORT = master_port_num,
       ...
       MASTER_LOG_FILE = 'master_log_name',
       MASTER_LOG_POS = master_log_pos;
mysql> START SLAVE;复制代码

其中,

master_host_name,master_port_num:Donor实例的IP和端口。

master_log_name,master_log_pos:performance_schema.clone_status 中的BINLOG_FILE, BINLOG_POSITION。

场景2,Donor本身就是一个从库,Recipient要作为Donor主库的从库。

mysql> SELECT MASTER_LOG_NAME, MASTER_LOG_POS FROM mysql.slave_relay_log_info;
mysql> CHANGE MASTER TO MASTER_HOST = 'master_host_name', MASTER_PORT = master_port_num,
       ...
       MASTER_LOG_FILE = 'master_log_name',
       MASTER_LOG_POS = master_log_pos;
mysql> START SLAVE;复制代码

其中,

master_host_name,master_port_num:Donor主库的IP和端口。

master_log_name,master_log_pos:mysql.slave_relay_log_info中的Master_log_name,Master_log_pos(分别对应 SHOW SLAVE STATUS 中的 Relay_Master_Log_File,Exec_Master_Log_Pos)。

在搭建从库时,建议设置--skip-slave-start。该参数默认为OFF,实例启动后,会自动执行START SLAVE操作。

如果Donor是个从库,Recipient会基于mysql.slave_master_info,mysql.slave_relay_log_info中的信息自动建立复制,很多时候,这未必是我们的预期行为。

五、Clone Plugin的实现细节

克隆操作可细分为以下5个阶段。

[INIT] ---> [FILE COPY] ---> [PAGE COPY] ---> [REDO COPY] -> [Done]复制代码

**1、INIT:**初始化一个克隆对象。

**2、FILE COPY:**拷贝所有数据文件。在拷贝之前,会记录一个LSN,作为“CLONE START LSN”,这个LSN其实是当前CHECKPOINT的LSN,同时启动“Page Tracking”特性。

“Page Tracking”会跟踪“CLONE START LSN”之后被修改的页,具体来说,会记录该页的Tablespace ID和page ID。数据文件拷贝结束后,会将当前CHECKPOINT的LSN记为“CLONE FILE END LSN”。

**3、PAGE COPY:**拷贝“CLONE START LSN”和“CLONE FILE END LSN”之间的页,在拷贝之前,会对这些页进行排序-基于Tablespace ID和page ID,尽量避免拷贝过程中出现随机读写。同时,开启“Redo Archiving”特性。

“Redo Archiving”会在后台开启一个归档线程将Redo文件中的内容按Chunk拷贝到归档文件中。通常来说,归档线程的拷贝速度会快于Redo日志的生成速度。即使慢于,在写入新的Redo日志时,也会等待归档线程完成拷贝,不会出现还未拷贝的Redo日志被覆盖的情况。当所有修改的页拷贝完毕后,会获取实例的一致性位置点信息,此时的LSN记为“CLONE LSN”。

4、REDO COPY:拷贝归档文件中“CLONE FILE END LSN”与“CLONE LSN”之间的Redo日志。

**5、Done:**调用snapshot_end()销毁克隆对象。

六、Clone Plugin的限制

1、克隆期间,不允许执行DDL命令。同样,DDL会阻塞克隆命令的执行

2、Clone Plugin不会拷贝Donor的配置参数。

3、Clone Plugin不会拷贝Donor的二进制日志文件。

4、Clone Plugin只会拷贝InnoDB表的数据,对于其它存储引擎的表,只会拷贝表结构。

5、Donor实例中如果有表通过DATA DIRECTORY指定了绝对路径,在进行本地克隆时,会提示文件已存在。在进行远程克隆时,绝对路径必须存在且有可写权限。

6、不允许通过MySQL Router连接Donor实例。

7、执行CLONE INSTANCE操作时,指定的Donor端口不能为X Protocol端口。

除此之外,在进行远程克隆时,还会进行如下检查:

  • MySQL版本(包括小版本)必须一致,且支持Clone Plugin。
ERROR 3864 (HY000): Clone Donor MySQL version: 8.0.20 is different from Recipient MySQL version 8.0.19.复制代码
  • 主机的操作系统和位数(32位,64位)必须一致。两者可根据version_compile_os,version_compile_machine参数获取。
  • Recipient必须有足够的磁盘空间存储克隆数据。
  • 字符集(character_set_server),校验集(collation_server),character_set_filesystem必须一致。
  • innodb_page_size必须一致。会检查innodb_data_file_path中ibdata的数量和大小。
  • 目前Clone Plugin(8.0.20)的实现,无论是Donor,还是Recipient,同一时间,只能执行一个克隆操作。后续会支持多个克隆操作并发执行。
ERROR 3634 (HY000): Too many concurrent clone operations. Maximum allowed - 1.复制代码
  • Recipient需要重启,所以其必须通过mysqld_safe或systemd等进行管理。如果是通过mysqld进行启动,实例关闭后,需要手动启动。
ERROR 3707 (HY000): Restart server failed (mysqld is not managed by supervisor process).复制代码
  • ACTIVE状态的Plugin必须一致。

七、Clone Plugin与XtraBackup的对比

1、在实现上,两者都有FILE COPY和REDO COPY阶段,但Clone Plugin比XtraBackup多了一个PAGE COPY,由此带来的好处是,Clone Plugin的恢复速度比XtraBackup更快。

2、XtraBackup没有Redo Archiving特性,有可能出现未拷贝的Redo日志被覆盖的情况。

3、GTID下建立复制,无需额外执行set global gtid_purged操作。

八、Clone Plugin的参数解析

  • clone_autotune_concurrency 是否自动调节克隆过程中并发线程数的数量,默认为ON,此时,最大线程数受clone_max_concurrency参数控制。若设置为OFF,则并发线程数的数量将是固定的,同clone_max_concurrency参数一致。该参数的默认值为16。
  • clone_buffer_size 本地克隆时,中转缓冲区的大小,默认4M。缓冲区越大,备份速度越快,相应的,对磁盘IO的压力越大。
  • clone_ddl_timeout 克隆操作需要获取备份锁(Backup Lock)。如果在执行CLONE命令时,有DDL在执行,则CLONE命令会被阻塞,等待获取备份锁(Waiting for backup lock)。等待的最大时长由clone_ddl_timeout参数决定,默认300(单位秒)。如果在这个时间内还没获取到锁,CLONE命令会失败,且提示“ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction”。

需要注意的是,如果在执行DDL时,有CLONE命令在执行,DDL同样会因获取不到备份锁被阻塞,只不过,DDL操作的等待时长由lock_wait_timeout参数决定,该参数的默认值为31536000s,即365天。

  • clone_enable_compression Remote cloning, whether to enable compression when transmitting data. Turning on compression can save network bandwidth, but will increase CPU consumption accordingly.
  • clone_max_data_bandwidth The maximum data copy rate allowed during remote cloning (unit: MiB/s). The default is 0, no limit. Note that the limit here is only the copy rate of a single thread. If there are multiple threads copying in parallel, the actual maximum copy rate = clone_max_data_bandwidth*number of threads.
  • clone_max_network_bandwidth The maximum network transmission rate allowed during remote cloning (unit: MiB/s). The default is 0, no limit. If there is a bottleneck in the network bandwidth, you can use this parameter to limit the speed.
  • clone_valid_donor_list Set the Donor whitelist and only clone the instances specified in the whitelist.
  • clone_ssl_ca, clone_ssl_cert, clone_ssl_key SSL related.

Related free learning recommendations: mysql video tutorial

The above is the detailed content of Take a look at the new features of MySQL 8 Clone Plugin. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete