天天看點

【資料庫遷移系列】從MySQL到openGauss的資料庫對象遷移實踐

9月30日新釋出的openGauss 3.1.0版本 ,工具的全量遷移和增量遷移的性能不但有了全面提升,而且支援資料庫對象視圖、觸發器、自定義函數、存儲過程的遷移。

  • 工具鍊:MySQL全量遷移支援并行遷移,提升全量遷移性能

    通過支援表級并行遷移,提升MySQL全量遷移性能,基于sysbench測試模型,在Kunpeng-920 2p伺服器上,10張表(單表容量三百萬以上)使用10并發遷移,可達到大于300M/s的遷移性能。

  • 工具鍊:MySQL增量遷移支援十五級并行消費,提升增量遷移性能

    基于開源三方件mysql-binlog-connector-java解析mysql的binlog, 并根據mysql主備進行複制的原理,對可并行的事務在openGauss端采用多線程進行并行回放,以實作MySQL到openGauss的線上遷移。

    利用sysbench對MySQL壓測,在10張表30個線程并發情況下,IUD混合場景下,在Kunpeng-920 2p伺服器上測試整體增量遷移性能可達3w tps.

  • 工具鍊:支援基于默克爾樹的資料校

    實作基于默克爾樹的資料實時校驗工具,支援MySQL資料遷移到openGauss時,源端與目的端資料全量和增量校驗。

本篇就來分享一下使用chameleon工具進行從MySQL到openGauss的資料庫對象遷移實踐。

軟體安裝

1. 由于我之前已經安裝過3.0版本的工具了,需要先解除安裝一下。

[root@pekphisprb70593 chameleon]# pip3 uninstall chameleonUninstalling chameleon-3.0.0:
Would remove:
/usr/local/python3/bin/chameleon
/usr/local/python3/bin/chameleon.py
/usr/local/python3/lib/python3.6/site-packages/chameleon-3.0.0.dist-info/*
/usr/local/python3/lib/python3.6/site-packages/pg_chameleon/*Proceed (y/n)? ySuccessfully uninstalled chameleon-3.0.0
[root@pekphisprb70593 chameleon]# rm -rf chameleon-3.0.0-py3-none-any.whl      

2.從官網​​https://opengauss.org/zh/supporttools.html ​​擷取擷取工具包:chameleon-3.1.0-py3-none-any.whl

【資料庫遷移系列】從MySQL到openGauss的資料庫對象遷移實踐

3.将新的3.1.0工具上傳到openGauss資料庫所在節點的chameleon檔案夾下。

[root@pekphisprb70593 chameleon]# python3 -m venv venv
[root@pekphisprb70593 chameleon]# source venv/bin/activate(venv) 
[root@pekphisprb70593 chameleon]# pip3 install ./chameleon-3.1.0-py3-none-any.whl      

最後提示“Successfully installed chameleon-3.1.0”說明安裝成功。

4. 設定配置檔案。這裡繼續使用之前已經配置好的 default.yml.

切換到omm 使用者進行操作。

(venv) [root@pekphisprb70593 chameleon]# su - ommLast login: Tue Oct 25 16:24:30 CST 2022 on pts/0
[omm@pekphisprb70593 ~]$ cd /opt/software/chameleon/
[omm@pekphisprb70593 chameleon]$ python3 -m venv venv
[omm@pekphisprb70593 chameleon]$ source venv/bin/activate
(venv) [omm@pekphisprb70593 chameleon]$  chameleon set_configuration_filesupdating configuration example with /home/omm/.pg_chameleon/configuration//config-example.yml      

資料庫對象遷移測試

初始化遷移過程

(venv) [omm@pekphisprb70593 chameleon]$ chameleon create_replica_schema --config default
(venv) [omm@pekphisprb70593 chameleon]$ chameleon add_source --config default --source mysql      

除了基礎資料同步,chameleon還支援将視圖、觸發器、自定義函數、存儲過程從MySQL遷移到openGauss。以下四個指令無先後之分。若不想日志輸出到控制台,可去掉--debug參數。

複制視圖:

chameleon start_view_replica --config default --source mysql --debug

複制觸發器:

chameleon start_trigger_replica --config default --source mysql --debug

複制自定義函數:

chameleon start_func_replica --config default --source mysql --debug

複制存儲過程:

chameleon start_proc_replica --config default --source mysql --debug

此外,工具還提供了可以在對象遷移資訊表sch_chameleon.t_replica_object中檢視遷移對象的記錄能力。下表展示了t_replica_object表的字段說明。

字段 類型 描述
i_id_object bigint id
i_id_source bigint 與sch_schema.t_sources的id相對應
en_object_type 枚舉類型 遷移對象所屬類型(VIEW/TRIGGER/FUNC/PROC)
ts_created timestamp with time zone 遷移時間
b_status boolean 遷移狀态。true表示遷移成功,false表示遷移失敗
t_src_object_sql text 原始sql語句
t_dst_object_sql text 翻譯後的語句。若無法翻譯或者翻譯出現error的情況為空;openGauss不支援的字段被注釋

視圖遷移

1. mysql 構造視圖資料。

mysql> create view test1_view as
-> select * from test1 where id=1;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from test1_view;      
【資料庫遷移系列】從MySQL到openGauss的資料庫對象遷移實踐

2. 工具執行視圖遷移指令

chameleon start_view_replica --config default --source mysql --debug

【資料庫遷移系列】從MySQL到openGauss的資料庫對象遷移實踐

3.在openGauss側查詢視圖,遷移成功。注意查詢的時候需要攜帶schema:mysql_db1_sch,視圖名稱和mysql 中定義的一緻,都是test1_view。

【資料庫遷移系列】從MySQL到openGauss的資料庫對象遷移實踐

觸發器遷移

1.mysql 構造如下觸發器:每删除一條test1中的資料,就向test2表中插入一條記錄。

DELIMITER//
CREATE TRIGGER del_log
AFTER DELETEON test1
FOR EACH ROW
BEGININSERT 
INTO test2(id,name) VALUES (old.id,concat("delete record:",old.name));
END; 
//      

2.工具執行指令,成功

chameleon start_trigger_replica --config default --source mysql --debug

【資料庫遷移系列】從MySQL到openGauss的資料庫對象遷移實踐

3.openGauss側測試觸發器

從測試結果來看,觸發器是直接生效的,test2中已經成功插入了資料。

【資料庫遷移系列】從MySQL到openGauss的資料庫對象遷移實踐
【資料庫遷移系列】從MySQL到openGauss的資料庫對象遷移實踐

自定義函數遷移

1. 在MySQL側構造了兩個簡單的函數。

DELIMITER // 
create function mysql_func2(x smallint unsigned, y smallint unsigned) returns smallint deterministic 
BEGIN 
DECLARE a, b SMALLINT UNSIGNED DEFAULT 10; 
SET  a = x, b = y; RETURN a+b; 
END; // 
 create function mysql_func1(s char(20)) returns char(50) deterministic return concat('mysql_func1, ',s,'!')
//      

2. 啟動遷移操作,如下圖所示,總共兩個,成功兩個。

chameleon start_func_replica --config default --source mysql --debug

【資料庫遷移系列】從MySQL到openGauss的資料庫對象遷移實踐

3. openGauss側直接測試函數調用,也是OK的。注意攜帶schema。

【資料庫遷移系列】從MySQL到openGauss的資料庫對象遷移實踐

存儲過程遷移

1. 在MySQL側構造了一個簡單的存儲過程。

DELIMITER //
CREATE PROCEDURE mysql_sp(IN x SMALLINT ,IN y SMALLINT ,OUT sum int)
BEGINSET  
sum = x + y;
END //
DELIMITER ;      

2. 工具側執行遷移,提示總共一個,成功一個。

chameleon start_proc_replica --config default --source mysql --debug

【資料庫遷移系列】從MySQL到openGauss的資料庫對象遷移實踐

3.openGauss直接測試調用,也是OK的。

【資料庫遷移系列】從MySQL到openGauss的資料庫對象遷移實踐

Q&A

1、遷移資料庫對象過程中報類似錯誤“‘replica_engine’ object has no attribute ”

(venv) [omm@pekphisprb70593 configuration]$ chameleon start_func_replica --config default --source mysql --debugTraceback (most recent call last):
File "/opt/software/chameleon/venv/bin/chameleon", line 5, in <module>exec(compile(open(__file__).read(), __file__, 'exec'))
File "/opt/software/chameleon/venv/bin/chameleon.py", line 58, in <module>getattr(replica, args.command)()
AttributeError: 'replica_engine' object has no attribute 'start_func_replica'      

這個錯誤的話是工具3.0.0之前版本還不支援 ,請确認工具版本是3.1.0。

2、遷移觸發器限制

create table test1_log(id int not null auto_increment,operation varchar(200) ,oper_time date, primary key (id));
DELIMITER //
CREATE TRIGGER del_write_log
AFTER DELETEON test1FOR EACH ROWBEGINset @t_name = old.name;
INSERT INTO test1_log(operation,oper_time) VALUES (concat("delete record:",@t_name),NOW());
END; //      
2022-10-26 14:37:56 MainProcess ERROR mysql_lib.py (1845): 2022-10-26 02:37:56.294 [main] ERROR org.opengauss.sqltranslator.dialect.mysql.MySqlToOpenGaussOutputVisitor - openGauss does not support set @T_NAME,trigger name is del_write_log

2022-10-26 14:37:56 MainProcess ERROR mysql_lib.py (1845): 2022-10-26 02:37:56.295 [main] ERROR org.opengauss.sqltranslator.dialect.mysql.MySqlToOpenGaussOutputVisitor - openGauss does not support variable started with @,trigger name is del_write_log