OceanBase初體驗之從MySQL遷移數據到OceanBase集羣

前置條件

  • MySQL 環境
  • OceanBase 環境
  • 測試用的表結構和一些數據

先在源端 MySQL 用如下腳本創建測試表,以及寫入10000條數據用於遷移測試。

use test;
CREATE TABLE students (
  `id` int NOT NULL PRIMARY KEY,
  `name` varchar(255) ,
  `code` varchar(20),
  `class` varchar(255)
);

DELIMITER $$
DROP PROCEDURE if EXISTS insertStudents;   
CREATE procedure insertStudents() 
BEGIN
	DECLARE i INT; 
	SET i = 0;  
	WHILE i<10000 DO 
		INSERT INTO students(id, name, code, class) VALUES(i+1, CONCAT('User', i), '2020010' + i + 10, CONCAT(FLOOR(RAND() * 10),'c'));  
		SET i = i+1;    
	END WHILE;  
END $$ 
DELIMITER ; 

CALL insertStudents(); 

select count(*) from students;

準備工作完成後,我們選擇合適的遷移工作來進行操作。對於 OceanBase 的MySQL 租戶,通常有如下兩種全量遷移方式:

  • mysqldump,MySQL自帶的導出工具,安裝好MySQL後就能直接使用,適合小數據量場景下的快速遷移,表結構和數據被導出成sql文件
  • DataX,是阿里開源的異構數據遷移工具,支持豐富的上下游數據源使用廣泛,對 OceanBase 的兼容性比較好,適合大批量數據遷移有較好的性能

下面做分別演示。

遷移方式一:mysqldump

mysqldump不需要單獨安裝,只要裝了 MySQL 的環境基本都會有mysqldump,可以用如下命令來檢查:

[root@localhost ~]# which mysqldump
/usr/bin/mysqldump

另外,obclient其實也集成了mysqldump工具,直接拿來用也可以:

[root@localhost ~]# su - ob
Last login: Sun Mar 17 10:49:37 CST 2024 on pts/1
[ob@localhost ~]$ which mysqldump
~/.oceanbase-all-in-one/obclient/u01/obclient/bin/mysqldump

整體遷移流程爲:從MySQL導出sql文件 -> 去OceanBase執行sql文件,導出的腳本都是標準的SQL語法。

第一步先把數據導出:

[ob@localhost ~]$ mkdir mysqldump_data
[ob@localhost ~]$ mysqldump -h x.x.x.222 -u root -P 3306 -p -B "test" --tables "students" > /home/ob/mysqldump_data/students.sql
#導出的sql腳本包含了create table和insert,可以自行檢查

第二步把數據導入,通常有兩種方式:mysql命令行和source語法。

#方式一:用命令行導入
[ob@localhost ~]$ mysql -h x.x.x.222 -u root -P 2883 -D test < /home/ob/mysqldump_data/students.sql

#方式二:用source導入
[ob@localhost ~]$ obclient -h x.x.x.222 -u root -P 2883 -D test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the OceanBase.  Commands end with ; or \g.
Your OceanBase connection id is 524290
Server version: OceanBase_CE 4.2.2.0 (r100010012024022719-c984fe7cb7a4cef85a40323a0d073f0c9b7b8235) (Built Feb 27 2024 19:20:54)
Copyright (c) 2000, 2018, OceanBase and/or its affiliates. All rights reserved.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

obclient [test]> show tables;
Empty set (0.002 sec)

obclient [test]> source /home/ob/mysqldump_data/students.sql;
Query OK, 10000 rows affected (0.069 sec)
Records: 10000  Duplicates: 0  Warnings: 0

obclient [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| students       |
+----------------+
1 row in set (0.002 sec)

obclient [test]> select count(*) from students;
+----------+
| count(*) |
+----------+
|    10000 |
+----------+
1 row in set (0.005 sec)

到這裏數據遷移就完成了。

擴展:只要是標準SQL語法組成的.sql文件,都可以用這兩種方式批量執行。

遷移方式二:DataX

DataX 是單獨的組件所以需要先下載才能使用,但是基本是開箱即用,沒有複雜的環境配置。

先準備好 DataX 的使用環境,下載、解壓即可:

[root@localhost ~]# wget https://datax-opensource.oss-cn-hangzhou.aliyuncs.com/202308/datax.tar.gz
[root@localhost ~]# tar -xvzf datax.tar.gz

DataX 的遷移任務被定義成一個個job,安裝官網的配置文件規範我們準備一個如下的job配置:

[root@localhost ~]# cd datax/job/
[root@localhost job]# vi mysql2ob_test.json
{
    "job": {
        "setting": {
            "speed": {
                "channel": 4
            },
            "errorLimit": {
                "record": 0,
                "percentage": 0.1
            }
        },
        "content": [
            {
                "reader": {
                    "name": "mysqlreader",
                    "parameter": {
                        "username": "root",
                        "password": "填實際密碼,不能留空",
                        "column": ["*"],
                        "connection": [
                            {
                                "table": ["students"],
                                "jdbcUrl": ["jdbc:mysql://x.x.x.222:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false"]
                            }
                        ]
                    }
                },
                "writer": {
                    "name": "oceanbasev10writer",
                    "parameter": {
                        "obWriteMode": "insert",
                        "column": ["*"],
                        "preSql": ["truncate table students_datx"],
                        "connection": [
                            {
                                "jdbcUrl": "jdbc:oceanbase://x.x.x.222:2883/test?",
                                "table": ["students_datx"]
                            }
                        ],
                        "username": "root",
                        "password":"填實際密碼,不能留空",
                        "writerThreadCount":10,
                        "batchSize": 1000,
                        "memstoreThreshold": "0.9"
                    }
                }
            }
        ]
    }
}

配置內容主要包含以下幾部分:

  • setting,job的參數配置,如併發數、限流等
  • reader,源端的讀取方式,主要包含源端的數據庫連接信息
  • writer,目標端的寫入方式,主要包含目標端的數據庫連接信息和寫入行爲等

由於 DataX 不會遷移表結構,所以先在 OceanBase 中把表建好,表名可以不一樣,但是字段要一樣

obclient [test]> CREATE TABLE students_datax (
    ->   `id` int NOT NULL PRIMARY KEY,
    ->   `name` varchar(255) ,
    ->   `code` varchar(20),
    ->   `class` varchar(255)
    -> );
Query OK, 0 rows affected (0.179 sec)

準備就緒後啓動 DataX 執行即可:

[root@localhost job]# python ../bin/datax.py mysql2ob_test.json
... ...
2024-03-17 11:37:29.012 [job-0] INFO  StandAloneJobContainerCommunicator - Total 10000 records, 207784 bytes | Speed 20.29KB/s, 1000 records/s | Error 0 records, 0 bytes |  All Task WaitWriterTime 0.143s |  All Task WaitReaderTime 0.022s | Percentage 100.00%
2024-03-17 11:37:29.013 [job-0] INFO  JobContainer -
任務啓動時刻                    : 2024-03-17 11:37:18
任務結束時刻                    : 2024-03-17 11:37:29
任務總計耗時                    :                 10s
任務平均流量                    :           20.29KB/s
記錄寫入速度                    :           1000rec/s
讀出記錄總數                    :               10000
讀寫失敗總數                    :                   0

校驗下數據是否正常:

[ob@localhost ~]$ obclient -h x.x.x.222 -u root -P 2883 -D test -p
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the OceanBase.  Commands end with ; or \g.
Your OceanBase connection id is 524294
Server version: OceanBase_CE 4.2.2.0 (r100010012024022719-c984fe7cb7a4cef85a40323a0d073f0c9b7b8235) (Built Feb 27 2024 19:20:54)
Copyright (c) 2000, 2018, OceanBase and/or its affiliates. All rights reserved.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

obclient [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| students       |
| students_datax |
+----------------+
2 rows in set (0.002 sec)

obclient [test]> select count(*) from students_datax;
+----------+
| count(*) |
+----------+
|    10000 |
+----------+
1 row in set (0.004 sec)

其他方式

OMS遷移

OMS(OceanBase Migration Service)是 OceanBase 提供的遷移服務,它包含在 OCP 中,可以在web界面上做一些配置即可實現數據遷移,它支持庫表結構遷移、全量遷移和增量同步。

後續體驗OMS的時候再來介紹。

增量實時同步

前面演示的兩種方法都是全量遷移,對於有增量實時同步的場景以上工具還無法解決,需要引入新的工具。這一類工具的原理基本都是一樣的,就是訂閱 MySQL binlog 進行回放解析成標準sql在下游執行,使用比較多的有 Canal,也是阿里的開源項目。

使用方式可以參考:

https://www.oceanbase.com/docs/common-oceanbase-database-cn-1000000000507573

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章