参考文档:
https://dev.mysql.com/doc/refman/8.4/en/clone-plugin-local.html
https://dev.mysql.com/doc/refman/8.4/en/clone-plugin-monitoring.html
https://dev.mysql.com/doc/refman/8.4/en/clone-plugin-replication.html
-- 安装克隆插件
INSTALL PLUGIN clone SONAME 'mysql_clone.so';
SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME = 'clone';
root@db:7002 08:53:34 [(none)]> INSTALL PLUGIN clone SONAME 'mysql_clone.so';
Query OK, 0 rows affected (0.01 sec)
root@db:7002 08:53:45 [(none)]> SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME = 'clone';
+-------------+---------------+
| PLUGIN_NAME | PLUGIN_STATUS |
+-------------+---------------+
| clone | ACTIVE |
+-------------+---------------+
1 row in set (0.00 sec)
root@db:7002 08:53:51 [(none)]>
--使用捐赠者用户'bkpuser'@'localhost'
root@db:7002 09:03:27 [(none)]> SHOW GRANTS FOR 'bkpuser'@'localhost';
+------------------------------------------------------------------------------------------------+
| Grants for bkpuser@localhost |
+------------------------------------------------------------------------------------------------+
| GRANT SELECT, RELOAD, PROCESS, LOCK TABLES, REPLICATION CLIENT ON *.* TO `bkpuser`@`localhost` |
| GRANT BACKUP_ADMIN ON *.* TO `bkpuser`@`localhost` |
| GRANT SELECT ON `performance_schema`.`keyring_component_status` TO `bkpuser`@`localhost` |
| GRANT SELECT ON `performance_schema`.`log_status` TO `bkpuser`@`localhost` |
+------------------------------------------------------------------------------------------------+
4 rows in set (0.00 sec)
root@db:7002 09:04:51 [(none)]>
-- 将clone的mysql,clone到/mysql/mysql_clone
官网上有一段有意思的说法,mysql> CLONE LOCAL DATA DIRECTORY = '/path/to/clone_dir'; ,clone_dir不能存在
where /path/to/clone_dir is the full path of the local directory that data is cloned to. An absolute path is required, and the specified directory ("clone_dir") must not exist, but the specified path must be an existent path. The MySQL server must have the necessary write access to create the directory.
CLONE LOCAL DATA DIRECTORY = '/mysql/mysql_clone';
root@db:7002 09:04:51 [(none)]> CLONE LOCAL DATA DIRECTORY = '/mysql/mysql_clone';
ERROR 1007 (HY000): Can't create database '/mysql/mysql_clone'; database exists
root@db:7002 09:09:01 [(none)]>
root@db:7002 09:13:35 [(none)]> CLONE LOCAL DATA DIRECTORY = '/mysql/mysql_clone';
Query OK, 0 rows affected (7.71 sec)
root@db:7002 09:13:44 [(none)]>
--监控克隆过程(接收者或捐赠者上都可以看)
root@db:7002 09:08:42 [(none)]> SELECT STATE FROM performance_schema.clone_status;
+-------------+
| STATE |
+-------------+
| In Progress |
+-------------+
1 row in set (0.01 sec)
root@db:7002 09:13:39 [(none)]> SELECT STATE FROM performance_schema.clone_status;
+-------------+
| STATE |
+-------------+
| In Progress |
+-------------+
1 row in set (0.00 sec)
root@db:7002 09:13:41 [(none)]> SELECT STATE FROM performance_schema.clone_status;
+-----------+
| STATE |
+-----------+
| Completed |
+-----------+
1 row in set (0.00 sec)
root@db:7002 09:13:47 [(none)]>
--如果有错误,则可以查看错误的信息
root@db:7002 09:16:06 [(none)]> SELECT STATE, ERROR_NO, ERROR_MESSAGE FROM performance_schema.clone_status;
+-----------+----------+---------------+
| STATE | ERROR_NO | ERROR_MESSAGE |
+-----------+----------+---------------+
| Completed | 0 | |
+-----------+----------+---------------+
1 row in set (0.00 sec)
root@db:7002 09:16:45 [(none)]>
-- 查看克隆的过程 (接收者或捐赠者上都可以看,不过捐赠者上,看不到RESTART、RECOVERY)
root@db:7002 09:13:47 [(none)]> SELECT STAGE, STATE, END_TIME FROM performance_schema.clone_progress;
+-----------+-------------+----------------------------+
| STAGE | STATE | END_TIME |
+-----------+-------------+----------------------------+
| DROP DATA | Completed | 2026-06-19 09:13:36.926532 |
| FILE COPY | Completed | 2026-06-19 09:13:44.133227 |
| PAGE COPY | Completed | 2026-06-19 09:13:44.171170 |
| REDO COPY | Completed | 2026-06-19 09:13:44.176549 |
| FILE SYNC | Completed | 2026-06-19 09:13:44.516317 |
| RESTART | Not Started | NULL |
| RECOVERY | Not Started | NULL |
+-----------+-------------+----------------------------+
7 rows in set (0.01 sec)
root@db:7002 09:16:06 [(none)]>
-- 这个时候,查看clone的库,文件夹里面没有information_schema 、 mysql 、 performance_schem等库
[root@19ctest mysql_clone]# ls
'#clone' ib_buffer_pool '#innodb_redo' mysql.ibd sys testdb8 undo_002
db_84 ibdata1 mysql query_rewrite test undo_001
[root@19ctest mysql_clone]#
-- 使用mycnf启动克隆好的数据库
[root@19ctest oracle]# sh startup_clone.sh
[root@19ctest oracle]# Logging to '/mysql/mysql_clone/19ctest.err'.
2026-06-19T01:34:42.726736Z mysqld_safe Starting mysqld daemon with databases from /mysql/mysql_clone
[root@19ctest oracle]#
-- 启动后,在克隆好的库上看clone_progress,各个状态已经完成
[mysql@19ctest ~]$ mysql -uroot -h127.0.0.1 -p -P7003
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.35 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
root@db:7003 09:35:26 [(none)]> SELECT STAGE, STATE, END_TIME FROM performance_schema.clone_progress;
+-----------+-----------+----------------------------+
| STAGE | STATE | END_TIME |
+-----------+-----------+----------------------------+
| DROP DATA | Completed | 2026-06-19 09:13:36.926532 |
| FILE COPY | Completed | 2026-06-19 09:13:44.133227 |
| PAGE COPY | Completed | 2026-06-19 09:13:44.171170 |
| REDO COPY | Completed | 2026-06-19 09:13:44.176549 |
| FILE SYNC | Completed | 2026-06-19 09:13:44.516317 |
| RESTART | Completed | 2026-06-19 09:34:43.639854 |
| RECOVERY | Completed | 2026-06-19 09:34:44.664991 |
+-----------+-----------+----------------------------+
7 rows in set (0.00 sec)
root@db:7003 09:35:39 [(none)]>
-- 做从库,略,从官网看,直接SOURCE_AUTO_POSITION = 1, 不像xtrabackup的官网,要做一个gtid_purge=XXX,然后再SOURCE_AUTO_POSITION = 1。
END