1. 程式人生 > 實用技巧 >MySQL 8.0(8.0.17+) Clone-plugin整理

MySQL 8.0(8.0.17+) Clone-plugin整理

1 . Clone Plugin介紹

本地克隆(local):
啟動克隆操作的MySQL伺服器例項中的資料,克隆到同伺服器或同節點上的一個目錄裡

遠端克隆(remote):
預設情況下,遠端克隆操作會刪除接受者(recipient)資料目錄中的資料,並將其替換為捐贈者(donor)的克隆資料。您也可以將資料克隆到接受者的其他目錄,以避免刪除現有資料。(可選)

2. 原理(remote)

'- PAGE COPY
  這裡有兩個動作
  開啟redo archiving功能,從當前點開始儲存新增的redo log,這樣從當前點開始所有的增量修改都不會丟失。同時上一步在page track的page被髮送到目標端。確保當前點之前所做的變更一定傳送到目標端。
  關於redo archiving,實際上這是官方早就存在的功能,主要用於官方的企業級備份工具,但這裡clone利用了該特性來維持增量修改產生的redo。 
  在開始前會做一次checkpoint, 開啟一個後臺執行緒log_archiver_thread()來做日誌歸檔。當有新的寫入時(notify_about_advanced_write_lsn)也會通知他去archive。當arch_log_sys處於活躍狀態時,他會控制日誌寫入以避免未 
  歸檔的日誌被覆蓋(log_writer_wait_on_archiver), 注意如果log_writer等待時間過長的話, archive任務會被中斷掉.

- Redo Copy
  停止Redo Archiving 所有歸檔的日誌被髮送到目標端,這些日誌包含了從page copy階段開始到現在的所有日誌,另外可能還需要記下當前的複製點,例如最後一個事務提交時的binlog位點或者gtid資訊,在系統頁中可以找到。

- Done
  目標端重啟例項,通過crash recovery將redo log應用上去。

3. 限制

官方文件列出的一些限制:
The clone plugin is subject to these limitations:
*   DDL, is not permitted during a cloning operation. This limitation should be considered when selecting data sources. A workaround is to use dedicated donor instances, which can accommodate DDL operations being blocked while data is cloned. Concurrent DML is permitted.
*   An instance cannot be cloned from a different MySQL server version. The donor and recipient must have the same MySQL server version. For example, you cannot clone between MySQL 5.7 and MySQL 8.0. The clone plugin is only supported in MySQL 8.0.17 and higher.
*   Only a single MySQL instance can be cloned at a time. Cloning multiple MySQL instances in a single cloning operation is not supported.
*   The X Protocol port specified byis not supported for remote cloning operations 
*   The clone plugin does not support cloning of MySQL server configurations.
*   The clone plugin does not support cloning of binary logs.
*   The clone plugin only clones data stored in `InnoDB`. Other storage engine data is not cloned.
*   Connecting to the donor MySQL server instance through MySQL Router is not supported.
*   Local cloning operations do not support cloning of general tablespaces that were created with an absolute path. A cloned tablespace file with the same path as the source tablespace file would cause a conflict.

總結: 
版本限制   : 同版本
DDL       : 不允許
InnoDB    :  支援

4. 應用

4.1 本地
4.1.1 載入外掛
INSTALL PLUGIN clone SONAME 'mysql_clone.so';
或
[mysqld]
plugin-load-add=mysql_clone.so
clone=FORCE_PLUS_PERMANENT

SELECT PLUGIN_NAME, PLUGIN_STATUS
FROM INFORMATION_SCHEMA.PLUGINS
WHERE PLUGIN_NAME LIKE 'clone';

4.1.2 建立克隆專用使用者
CREATE USER clone_user@'%' IDENTIFIED by 'password'; 
GRANT BACKUP_ADMIN ON *.* TO 'clone_user'; 

Ps: BACKUP_ADMIN 是MySQL8.0 才有的備份鎖的許可權

4.1.3 MYSQL 8.0 本地克隆

[root@db01 3306]# mkdir -p /data/test/
[root@db01 3306]# chown -R mysql.mysql /data/
mysql -uclone_user -ppassword
CLONE LOCAL DATA DIRECTORY = '/data/test/clonedir';

# 觀測狀態
db01 [(none)]> SELECT STAGE, STATE, END_TIME FROM performance_schema.clone_progress;
+-----------+-------------+----------------------------+
| STAGE     | STATE       | END_TIME                   |
+-----------+-------------+----------------------------+
| DROP DATA | Completed   | 2020-04-20 21:13:19.264003 |
| FILE COPY | Completed   | 2020-04-20 21:13:20.025444 |
| PAGE COPY | Completed   | 2020-04-20 21:13:20.028552 |
| REDO COPY | Completed   | 2020-04-20 21:13:20.030042 |
| FILE SYNC | Completed   | 2020-04-20 21:13:20.439444 |
| RESTART   | Not Started | NULL                       |
| RECOVERY  | Not Started | NULL                       |
+-----------+-------------+----------------------------+
7 rows in set (0.00 sec)

#日誌觀測: 
set global log_error_verbosity=3;
tail -f db01.err
CLONE LOCAL DATA DIRECTORY = '/data/test/3308';

4.1.4 啟動新例項
[root@db01 clonedir]# mysqld_safe  --datadir=/data/test/clonedir --port=3333 --socket=/tmp/mysql3333.sock --user=mysql --mysqlx=OFF &

4.2 MYSQL 8.0 遠端 clone

  • 各個節點載入外掛
INSTALL PLUGIN clone SONAME 'mysql_clone.so';
或
[mysqld]
plugin-load-add=mysql_clone.so
clone=FORCE_PLUS_PERMANENT
  • 檢視各載入clone狀態
SELECT PLUGIN_NAME, PLUGIN_STATUS
FROM INFORMATION_SCHEMA.PLUGINS
WHERE PLUGIN_NAME LIKE 'clone';
  • 建立遠端clone使用者
1.捐贈者(source)授權
create user test_s@'%' identified by '123';
grant backup_admin on *.* to test_s@'%';
 
2. 接受者(target)授權
create user test_t@'%' identified by '123';
grant clone_admin on *.* to test_t@'%';
  • 遠端clone( 接收者 )
開始克隆 -->
管理使用者:
mysql> SET GLOBAL clone_valid_donor_list='10.0.0.51:3306';
 
新視窗使用test_t使用者重新連線資料庫:
mysql -utest_t -p123 -h10.0.0.52  -P3306
mysql> CLONE INSTANCE FROM test_s@'10.0.0.51':3306 IDENTIFIED BY '123';