基於Xtrabackup及可傳輸表空間實現多源數據恢復

本文目錄一、使用背景

1.可傳輸表空間基本流程
2.使用前提條件/限制
二、技術要點
三、實施步驟
1.實驗環境
2.源端操作
1) 造測試數據並模擬小壓力
2) 備份單庫數據
3) 備份表結構
4) 批量生成可傳輸表空間命令
3.目標端操作
1) 備份數據恢復準備
2) 恢復表結構至目標端
3) 捨棄目標端對應表空間文件
4) 拷貝表數據及配置文件到目標端sbtest庫數據目錄
5) 執行導入表空間文件操作
4.建立複製(多源複製)
四、參考鏈接

Keywords:
MySQL xtrabackup Transportable Tablespaces 
MySQLdump multi-source replication

一、使用背景 

MySQL在5.7以後引入了多源複製(Multi-Source)功能,可用於將多個單獨的數據庫匯聚到一個數據庫實例下,方便用戶進行數據分析、彙總或推送至其他數據庫平臺。早期針對匯聚場景初始化各源端數據到匯聚庫,爲了提升效率通常會使用使用開源並行邏輯導入導出工具myloader/mydumper進行數據導入/導出,當源端多實例多庫數據量較大(100G以上)情況下,使用myloader/mydumper花費的時間則可能無法滿足時間要求,需要考慮是否有更高效的方式進行數據初始化同步操作,以及當複製通道異常時更便捷快速修復

ps:多源複製使用物理備份(xtrabackup)做數據初始化時,常規方式只有第一個通道可做覆蓋還原,後續通道需邏輯還原或用其他方式還原,如該方案所描述的方式。

MySQL在5.6以後支持了可傳輸表空間,以及Xtrabackup針對該功能在備份時提供了一個對應參數export,該參數支持對InnoDB存儲引擎表的備份數據export轉換,且與常規備份操作一樣可生成備份時間點的binlog信息(GTID信息),結合這兩特性可以更高效和快速的方式實現多庫數據匯聚的初始導入操作

▽場景架構圖如下▽
 



1.可傳輸表空間基本流程
先簡單梳理一下可傳輸表空間基本流程:
 

  • 在源端將InnoDB表進行ibd數據文件導出處理(export tablespace))
  • 在目標端創建與源端相同表結構的表
  • 將目標端的表數據文件捨棄(discard tablespace)
  • 用源端的相對應ibd文件覆蓋到目標端
  • 在目標端執行表空間文件的導入/置換(import tablespace)


單純使用可傳輸表空間功能無法記錄對應表的事務點,也就是如果需要進行匯聚複製同步,無法知道從binlog哪個文件的哪個position點進行數據同步,這也就是需要引入xtrabackup及export功能的原因。備份元數據信息本身會記錄複製同步點信息。

2.使用前提條件/限制

  • MySQL須爲5.6以上版本
  • 表存儲引擎須爲InnoDB存儲引擎(支持可傳輸表空間)
  • 導入完成後建議執行ANALYZE TABLE更新統計信息


二、技術要點

  • sysbench 源端造測試數據並模擬壓力
  • mysqldump 備份源端表結構
  • concat()拼接批量可傳輸表空間SQL
  • xtrabackup 備份源端單庫(部分庫)數據
  • Transportable Tablespace可傳輸表空間功能
  • Multi-source Replication 多源複製
  • mysql-error.log 錯誤信息日誌校驗
  • ANALYZE TABLE更新統計信息


三、實施步驟 

1.實驗環境 


2.源端操作 

  • 1) 造測試數據並模擬小壓力
使用sysbench創建4張各100W記錄的測試表,並使用2個併發持續模擬業務壓力:
 
## 造數據
shell> /opt/sysbench-0.9/sysbench/sysbench --test=/opt/sysbench-0.9/sysbench/tests/db/oltp.lua --oltp-table-size=1000000 --oltp-tables-count=4 --mysql-user=sysbench --mysql-password=sysbench --mysql-host=10.186.60.16 --mysql-port=3333 prepare

## 模擬小壓力
shell> /opt/sysbench-0.9/sysbench/sysbench --test=/opt/sysbench-0.9/sysbench/tests/db/oltp.lua --oltp-table-size=1000000 --oltp-tables-count=4 --mysql-user=sysbench --mysql-password=sysbench --mysql-host=10.186.60.16 --mysql-port=3333 --num-threads=2 --max-requests=0 --max-time=0 --report-interval=1 run
  • 2) 備份單庫數據


使用xtrabackup備份工具對源端sbtest庫進行單庫備份,保存至/data/mysql/backup/目錄下:

shell> innobackupex --databases=sbtest /data/mysql/backup/
  • 3) 備份表結構


爲可傳輸表空間做準備,將源端表結構備份並後續在目標端導入:

shell> cd /data/mysql/backup
shell> mysqldump --no-data --set-gtid-purged=off sbtest>sbtest_schema.sql

 

  • 4) 批量生成可傳輸表空間命令

 

  • discard


使用concat函數拼接出批量DISCARD TABLESPACE的SQL:

shell> cd /data/mysql/backup
shell> mysql -ssre "select concat('ALTER TABLE ',TABLE_SCHEMA,'.',TABLE_NAME,' DISCARD TABLESPACE;') from information_schema.tables where TABLE_SCHEMA='sbtest';" >discard_tbs.sql

## 輸出文件如下所示
shell> cat discard_tbs.sql
ALTER TABLE sbtest.sbtest1 DISCARD TABLESPACE;
ALTER TABLE sbtest.sbtest2 DISCARD TABLESPACE;
ALTER TABLE sbtest.sbtest3 DISCARD TABLESPACE;
ALTER TABLE sbtest.sbtest4 DISCARD TABLESPACE;
  • import


使用concat函數拼接出批量IMPORT TABLESPACE的SQL:

shell> cd /data/mysql/backup
shell> mysql -ssre "select concat('ALTER TABLE ',TABLE_SCHEMA,'.',TABLE_NAME,' IMPORT TABLESPACE;') from information_schema.tables where TABLE_SCHEMA='sbtest';">import_tbs.sql

## 輸出文件如下所示
shell> cat import_tbs.sql
ALTER TABLE sbtest.sbtest1 IMPORT TABLESPACE;
ALTER TABLE sbtest.sbtest2 IMPORT TABLESPACE;
ALTER TABLE sbtest.sbtest3 IMPORT TABLESPACE;
ALTER TABLE sbtest.sbtest4 IMPORT TABLESPACE;


拷貝源端/data/mysql/backup目錄下生成的所有相關文件到目標端/data/mysql/backup下
 

3.目標端操作

  • 1) 備份數據恢復準備


對備份數據進行apply-log日誌應用及將數據進行export轉換生成配置文件:

shell> innobackupex --apply-log --export /data/mysql/backup/2019-02-22_15-26-20/

## export執行完後sbtest庫下備份文件如下所示
## exp結尾的文件爲Percona針對Percona XtraDB做export的配置文件
## cfg結尾的文件爲Percona針對MySQL可傳輸表空間export的配置文件

shell>  ll /data/mysql/backup/2019-02-22_15-26-20/sbtest/
總用量 999556
-rw-r----- 1 root root        67 2月  22 15:40 db.opt
-rw-r--r-- 1 root root       569 2月  22 15:53 sbtest1.cfg
-rw-r----- 1 root root     16384 2月  22 15:53 sbtest1.exp
-rw-r----- 1 root root      8632 2月  22 15:40 sbtest1.frm
-rw-r----- 1 root root 255852544 2月  22 15:53 sbtest1.ibd
-rw-r--r-- 1 root root       569 2月  22 15:53 sbtest2.cfg
-rw-r----- 1 root root     16384 2月  22 15:53 sbtest2.exp
-rw-r----- 1 root root      8632 2月  22 15:40 sbtest2.frm
-rw-r----- 1 root root 255852544 2月  22 15:53 sbtest2.ibd
-rw-r--r-- 1 root root       569 2月  22 15:53 sbtest3.cfg
-rw-r----- 1 root root     16384 2月  22 15:53 sbtest3.exp
-rw-r----- 1 root root      8632 2月  22 15:40 sbtest3.frm
-rw-r----- 1 root root 255852544 2月  22 15:53 sbtest3.ibd
-rw-r--r-- 1 root root       569 2月  22 15:53 sbtest4.cfg
-rw-r----- 1 root root     16384 2月  22 15:53 sbtest4.exp
-rw-r----- 1 root root      8632 2月  22 15:40 sbtest4.frm
-rw-r----- 1 root root 255852544 2月  22 15:53 sbtest4.ibd

 

  • 2) 恢復表結構至目標端


將源端表結構在目標端數據庫創建:

shell> cd /data/mysql/backup

## 手工創建sbtest庫
shell> mysql -e "create database sbtest;"

## 導入源端對應的表結構
shell> mysql sbtest< sbtest_schema.sql

## 驗證
shell> mysql -e "show tables from sbtest;"

 

  • 3) 捨棄目標端對應表空間文件

 

shell> cd /data/mysql/backup
shell> mysql <discard_tbs.sql

## 執行後效果如下所示,ibd文件已被捨棄
shell>  ll /data/mysql/data/sbtest/
-rw-r----- 1 mysql mysql   67 2月  22 15:58 db.opt
-rw-r----- 1 mysql mysql 8632 2月  22 15:58 sbtest1.frm
-rw-r----- 1 mysql mysql 8632 2月  22 15:58 sbtest2.frm
-rw-r----- 1 mysql mysql 8632 2月  22 15:58 sbtest3.frm
-rw-r----- 1 mysql mysql 8632 2月  22 15:58 sbtest4.frm

 

  • 4) 拷貝表數據及配置文件到目標端sbtest庫數據目錄

 

## 拷貝ibd文件
shell> cp /data/mysql/backup/2019-02-22_15-26-20/sbtest/*.ibd /data/mysql/data/sbtest/

## 拷貝cfg文件
shell> cp /data/mysql/backup/2019-02-22_15-26-20/sbtest/*.cfg /data/mysql/data/sbtest/

## 修改文件權限爲mysql用戶
shell> chown -R mysql:mysql /data/mysql/data/sbtest

 

  • 5) 執行導入表空間文件操作
shell> cd /data/mysql/backup

## 出現Warning爲可傳輸表空間正常輸出,可忽略,詳情可參考下圖所示note說明
shell> mysql <import_tbs.sql
Warning (Code 1814): InnoDB: Tablespace has been discarded for table 'sbtest1'
Warning (Code 1814): InnoDB: Tablespace has been discarded for table 'sbtest2'
Warning (Code 1814): InnoDB: Tablespace has been discarded for table 'sbtest3'
Warning (Code 1814): InnoDB: Tablespace has been discarded for table 'sbtest4'

## 可通過MySQL錯誤日誌查看import期間是否存在導入異常
shell> less /data/mysql/data/mysql-error.log



4.建立複製(多源複製) 

mysql> CHANGE MASTER TO 
MASTER_HOST='10.186.60.16', 
MASTER_USER='repl', 
MASTER_PORT=3333, 
MASTER_PASSWORD='repl',
MASTER_LOG_FILE='mysql-bin.000004',
MASTER_LOG_POS=149327998 FOR CHANNEL '10-186-60-16';

mysql> CHANGE REPLICATION FILTER REPLICATE_WILD_DO_TABLE=('sbtest.%');

mysql> START SLAVE FOR CHANNEL '10-186-60-16';
mysql> SHOW SLAVE STATUS FOR CHANNEL '10-186-60-16'\G;



四、參考鏈接

  • Percona xtrabackup export功能介紹

https://www.percona.com/doc/percona-xtrabackup/2.4/xtrabackup_bin/restoring_individual_tables.html

  • MySQL 可傳輸表空間功能說明

https://dev.mysql.com/doc/refman/5.7/en/tablespace-copying.html

  • 場景示例思路來源博客

http://www.cnblogs.com/xuanzhi201111/p/6609867.html
 

使用指南系列

| 使用指南
深度分析 | MyCat與DBLE的對比性能調
開源分佈式中間件 DBLE 快速入門指南
DBLE 自定義拆分算法
DBLE Server.xml 配置解析
DBLE Schema.xml 配置解析
DBLE rule.xml 配置解析
| 案例分析
DBLE和Mycat跨分片查詢結果不一致案例分析
| 社區活動
DBLE核心研發主講:MySQL分佈式中間件公開課開課啦

 


開源分佈式中間件DBLE
社區官網:https://opensource.actionsky.com/
GitHub主頁:https://github.com/actiontech/dble
技術交流羣:669663113

開源數據傳輸中間件DTLE
社區官網:https://opensource.actionsky.com/
GitHub主頁:https://github.com/actiontech/dtle
技術交流羣:852990221