作者:shunwah??
在運(yùn)維管理領(lǐng)域,我擁有多年深厚的專業(yè)積累,兼具堅(jiān)實(shí)的理論基礎(chǔ)與廣泛的實(shí)踐經(jīng)驗(yàn)。精通運(yùn)維自動(dòng)化流程,對(duì)于OceanBase、MySQL等多種數(shù)據(jù)庫的部署與運(yùn)維,具備從初始部署到后期維護(hù)的全鏈條管理能力。擁有OceanBase的OBCA和OBCP認(rèn)證、OpenGauss社區(qū)認(rèn)證結(jié)業(yè)證書,以及崖山DBCA、亞信AntDBCA、翰高 HDCA、GBase 8a | 8c | 8s、Galaxybase的GBCA、Neo4j的Graph Data Science Certification、NebulaGraph的NGCI & NGCP、東方通TongTech TCPE等多項(xiàng)權(quán)威認(rèn)證。
在OceanBase & 墨天輪的技術(shù)征文大賽中,多次榮獲一、二、三等獎(jiǎng)。同時(shí),在OpenGauss第五屆、第六屆、第七屆技術(shù)征文大賽,TiDB社區(qū)專欄征文大賽,金倉數(shù)據(jù)庫有獎(jiǎng)?wù)魑幕顒?dòng),以及YashanDB「產(chǎn)品體驗(yàn)官」征文等活動(dòng)中,我也屢獲殊榮。此外,我還活躍于墨天輪、CSDN、ITPUB等技術(shù)平臺(tái),經(jīng)常發(fā)布原創(chuàng)技術(shù)文章,并多次被首頁推薦。

前言
在分布式數(shù)據(jù)庫領(lǐng)域,對(duì)于熟悉OceanBase的用戶來說,“多租戶”是其標(biāo)志性特性之一。簡(jiǎn)單來說,OceanBase的租戶類似于傳統(tǒng)數(shù)據(jù)庫的實(shí)例——比如一個(gè)MySQL兼容模式的租戶,從客戶端視角看,與使用普通MySQL數(shù)據(jù)庫幾乎無異。而在4.3版本中,OceanBase在多租戶架構(gòu)基礎(chǔ)上新增了“租戶克隆”功能,讓租戶的復(fù)制和管理變得前所未有的高效。
近日在逛OB社區(qū)時(shí),偶然看到【積分 + 福利】實(shí)戰(zhàn)營(yíng)(第二季)第二期——OceanBase 租戶克隆活動(dòng)(2025.7.21已更新,福利加碼),立刻被吸引了。這活動(dòng)設(shè)計(jì)得太有意思了,OB小編(茲拉坦)老師的創(chuàng)意讓人眼前一亮,真心希望能多舉辦這類實(shí)踐活動(dòng)。正如活動(dòng)中所說,“紙上得來終覺淺,實(shí)踐才能出真知”。我已經(jīng)親身體驗(yàn)了《通過克隆租戶生成一個(gè)相同的新租戶》在線實(shí)驗(yàn),必須說OceanBase的租戶克隆功能太人性化了:操作簡(jiǎn)單快捷,特別適合快速部署測(cè)試環(huán)境或?qū)崿F(xiàn)數(shù)據(jù)隔離,尤其是一分鐘左右就能完成克隆的特點(diǎn),極大提升了工作效率。
OceanBase 租戶克隆文檔操作一步步的指引非常詳細(xì),小白也能很快上手。同時(shí),我強(qiáng)烈推薦大家都來參與這次活動(dòng),親自動(dòng)手體驗(yàn)一下!完成課后小測(cè)并上傳實(shí)驗(yàn)截圖,不僅能積累積分,還有機(jī)會(huì)贏取豐厚獎(jiǎng)品哦!

借用OB老師的圖
一、 租戶克?。篛ceanBase 4.3的“效率利器”
1.1 技術(shù)本質(zhì)解析:從集群到租戶的隔離性
使用OceanBase時(shí),我們會(huì)在多臺(tái)機(jī)器上啟動(dòng)進(jìn)程組成集群,而集群中可以創(chuàng)建多個(gè)租戶,且租戶間完全隔離——這是OceanBase多租戶架構(gòu)的核心優(yōu)勢(shì)。
租戶克隆功能則是在這一基礎(chǔ)上的升級(jí):在系統(tǒng)租戶下執(zhí)行一條簡(jiǎn)單語句,就能基于源租戶快速克隆出一個(gè)新租戶。新租戶初始數(shù)據(jù)是源租戶在語句執(zhí)行時(shí)的快照,相當(dāng)于“瞬間復(fù)制”了源租戶的狀態(tài)。

1.2 為什么租戶克隆能“秒級(jí)”完成?
租戶克隆的高效源于其獨(dú)特的實(shí)現(xiàn)邏輯:
- 只拷貝元數(shù)據(jù):克隆過程中,僅復(fù)制源租戶的元數(shù)據(jù)(如表結(jié)構(gòu)、權(quán)限配置等),而非實(shí)際數(shù)據(jù);
- 共享物理宏塊:新租戶初始訪問的物理宏塊(OceanBase的基礎(chǔ)存儲(chǔ)單元)與源租戶完全相同,無需額外拷貝數(shù)據(jù);
- 嚴(yán)格隔離性:盡管初始共享存儲(chǔ),新租戶與源租戶仍是獨(dú)立個(gè)體——數(shù)據(jù)隔離(雙方數(shù)據(jù)改動(dòng)互不影響)、資源隔離(CPU、內(nèi)存、IOPS獨(dú)立分配,不搶占)。
舉個(gè)例子:一個(gè)8核64G、包含50萬張表和8TB數(shù)據(jù)的租戶,克隆成同等規(guī)格的新租戶僅需2分鐘,這在傳統(tǒng)數(shù)據(jù)庫中幾乎無法想象。
1.3 租戶克隆的實(shí)際應(yīng)用場(chǎng)景
在實(shí)際業(yè)務(wù)中,租戶克隆能解決多個(gè)痛點(diǎn):
- 大促報(bào)表查詢:生產(chǎn)租戶CPU使用率達(dá)85%時(shí),克隆一個(gè)新租戶專門跑報(bào)表,避免影響核心業(yè)務(wù);
- 復(fù)雜查詢分流:將內(nèi)部運(yùn)營(yíng)的復(fù)雜查詢轉(zhuǎn)移到克隆租戶(可設(shè)為只讀備租戶,實(shí)時(shí)同步生產(chǎn)數(shù)據(jù)),減輕生產(chǎn)壓力;
- 版本升級(jí)回滾:發(fā)布前克隆生產(chǎn)租戶作為“備份”,若升級(jí)失敗,用克隆租戶快速恢復(fù),減少停機(jī)時(shí)間;
- 多場(chǎng)景復(fù)用:?jiǎn)蝹€(gè)源租戶可克隆出多個(gè)不同規(guī)格的租戶,分別用于測(cè)試、開發(fā)、數(shù)據(jù)分析等場(chǎng)景。
二、租戶克隆操作指南:小白也能輕松上手
租戶克隆的使用并不復(fù)雜,以下是基于MySQL模式的詳細(xì)步驟(實(shí)驗(yàn)環(huán)境已預(yù)建,無需手動(dòng)創(chuàng)建租戶和數(shù)據(jù)庫):
2.1 步驟1:登錄用戶租戶并準(zhǔn)備測(cè)試數(shù)據(jù)
2.1.1 登錄用戶租戶
[root@iZbp1i39ztgukq3j591g2lZ ~]# obclient -h127.0.0.1 -P2881 -uroot@mysql_tenant -A
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3221490256
Server version: OceanBase_CE 4.3.5.1 (r101010042025042417-0c7ffd37c2904f4d8191fb2d056738a93cce6d1d) (Built Apr 24 2025 17:44:55)
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 [(none)]>

2.1.1 創(chuàng)建測(cè)試數(shù)據(jù)庫和表
obclient [(none)]> create database testdb;
Query OK, 1 row affected (0.041 sec)
obclient [(none)]> use testdb
Database changed
obclient [testdb]> create table test(id int, code1 varchar(10),code2 varchar(10));
Query OK, 0 rows affected (0.140 sec)

2.1.1 插入測(cè)試數(shù)據(jù)
obclient [testdb]> insert into test values(1,'apple','a');
Query OK, 1 row affected (0.047 sec)
obclient [testdb]> insert into test values(2,'banana','b');
Query OK, 1 row affected (0.001 sec)
obclient [testdb]> insert into test values(3,'carambola','c');
Query OK, 1 row affected (0.001 sec)
obclient [testdb]>

2.2 步驟2:配置歸檔環(huán)境
2.2.1 創(chuàng)建備份目錄
obclient [testdb]> exit
Bye
[root@iZbp1i39ztgukq3j591g2lZ ~]# ls
oceanbase-all-in-one
oceanbase-all-in-one-4.2.1.0-100120231013145059.el7.x86_64.tar.gz
oceanbase-ce-4.3.5.1-101010042025042417.el7.x86_64.rpm
oceanbase-ce-libs-4.3.5.1-101010042025042417.el7.x86_64.rpm
[root@iZbp1i39ztgukq3j591g2lZ ~]# mkdir backup
[root@iZbp1i39ztgukq3j591g2lZ ~]# cd backup/
[root@iZbp1i39ztgukq3j591g2lZ backup]# pwd
/root/backup
[root@iZbp1i39ztgukq3j591g2lZ backup]#

2.2.2 通過本地Unix Socket連接租戶,配置備份路徑
[root@iZbp1i39ztgukq3j591g2lZ backup]# obclient -S /home/admin/oceanbase/observer/run/sql.sock -uroot@mysql_tenant
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3221501807
Server version: OceanBase_CE 4.3.5.1 (r101010042025042417-0c7ffd37c2904f4d8191fb2d056738a93cce6d1d) (Built Apr 24 2025 17:44:55)
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 [(none)]> SET GLOBAL secure_file_priv = "/root/backup";
Query OK, 0 rows affected (0.046 sec)
obclient [(none)]>

2.2.3 重新登錄用戶租戶,配置歸檔目的端并開啟歸檔
obclient [(none)]> exit;
Bye
[root@iZbp1i39ztgukq3j591g2lZ backup]# obclient -h127.0.0.1 -P2881 -uroot@mysql_tenant -A
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3221505908
Server version: OceanBase_CE 4.3.5.1 (r101010042025042417-0c7ffd37c2904f4d8191fb2d056738a93cce6d1d) (Built Apr 24 2025 17:44:55)
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 [(none)]> ALTER SYSTEM SET LOG_ARCHIVE_DEST='LOCATION=file:///root/backup';
Query OK, 0 rows affected (0.026 sec)
obclient [(none)]> ALTER SYSTEM ARCHIVELOG;
Query OK, 0 rows affected (0.008 sec)
obclient [(none)]>

2.2.4 查看歸檔進(jìn)度(STATUS為DOING即正常)
obclient [(none)]> SELECT * FROM oceanbase.DBA_OB_ARCHIVELOG\G
*************************** 1. row ***************************
DEST_ID: 1001
ROUND_ID: 1
INCARNATION: 1
DEST_NO: 0
STATUS: DOING
START_SCN: 1753347835198885000
START_SCN_DISPLAY: 2025-07-24 17:03:55.198885
CHECKPOINT_SCN: 1753347835198885001
CHECKPOINT_SCN_DISPLAY: 2025-07-24 17:03:55.198885
COMPATIBLE: 1
BASE_PIECE_ID: 1
USED_PIECE_ID: 1
PIECE_SWITCH_INTERVAL: 86400000000
UNIT_SIZE: 1
COMPRESSION: none
INPUT_BYTES: 49419326
INPUT_BYTES_DISPLAY: 47.13MB
OUTPUT_BYTES: 49419326
OUTPUT_BYTES_DISPLAY: 47.13MB
COMPRESSION_RATIO: 1.00
DELETED_INPUT_BYTES: 0
DELETED_INPUT_BYTES_DISPLAY: 0.00MB
DELETED_OUTPUT_BYTES: 0
DELETED_OUTPUT_BYTES_DISPLAY: 0.00MB
COMMENT:
PATH: file:///root/backup
1 row in set (0.013 sec)
obclient [(none)]>

2.2.5 執(zhí)行轉(zhuǎn)儲(chǔ)
obclient [(none)]> ALTER SYSTEM MINOR FREEZE;
Query OK, 0 rows affected (0.026 sec)
obclient [(none)]>

2.3 步驟3:執(zhí)行租戶克?。ǖ卿泂ys租戶)
2.3.1 登錄sys租戶
obclient [(none)]> exit;
Bye
[root@iZbp1i39ztgukq3j591g2lZ backup]#
[root@iZbp1i39ztgukq3j591g2lZ backup]# obclient -h127.0.0.1 -P2881 -uroot@sys -A -Doceanbase
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3221516138
Server version: OceanBase_CE 4.3.5.1 (r101010042025042417-0c7ffd37c2904f4d8191fb2d056738a93cce6d1d) (Built Apr 24 2025 17:44:55)
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 [oceanbase]>

2.3.2 創(chuàng)建克隆租戶的資源單元配置
obclient [oceanbase]> CREATE RESOURCE UNIT clone_unit_config
-> MEMORY_SIZE = '2G',
-> MAX_CPU = 1, MIN_CPU = 1,
-> LOG_DISK_SIZE = '6G',
-> MAX_IOPS = 10000, MIN_IOPS = 10000, IOPS_WEIGHT=1;
Query OK, 0 rows affected (0.012 sec)
obclient [oceanbase]>

2.3.3 執(zhí)行克隆語句
obclient [oceanbase]> CREATE TENANT clone_mysql001 FROM mysql_tenant
-> WITH
-> RESOURCE_POOL = clone_tnt_pool,
-> UNIT= clone_unit_config;
Query OK, 0 rows affected (52.090 sec)
obclient [oceanbase]>

2.3.4 查看克隆任務(wù)狀態(tài)(STATUS為CLONE_SYS_SUCCESS即完成)
obclient [oceanbase]> SELECT * FROM oceanbase.DBA_OB_CLONE_HISTORY\G
*************************** 1. row ***************************
CLONE_JOB_ID: 1753348061389497000
TRACE_ID: YB427F000001-00063AA910457756-0-0
SOURCE_TENANT_ID: 1002
SOURCE_TENANT_NAME: mysql_tenant
CLONE_TENANT_ID: 1004
CLONE_TENANT_NAME: clone_mysql001
TENANT_SNAPSHOT_ID: 1753348061475743001
TENANT_SNAPSHOT_NAME: _inner_snapshot$1753348061475743000
RESOURCE_POOL_ID: 1004
RESOURCE_POOL_NAME: clone_tnt_pool
UNIT_CONFIG_NAME: clone_unit_config
RESTORE_SCN: 1753348062532431000
STATUS: CLONE_SYS_SUCCESS
CLONE_JOB_TYPE: FORK
CLONE_START_TIME: 2025-07-24 17:07:41.394514
CLONE_FINISHED_TIME: 2025-07-24 17:08:33.084167
RET_CODE: 0
ERROR_MESSAGE: NULL
1 row in set (0.002 sec)
obclient [oceanbase]>

2.4 步驟4:驗(yàn)證克隆結(jié)果
2.4.1 查看源租戶數(shù)據(jù)
obclient [oceanbase]> exit
Bye
[root@iZbp1i39ztgukq3j591g2lZ backup]# obclient -h127.0.0.1 -P2881 -uroot@mysql_tenant -A -Dtestdb
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3221563835
Server version: OceanBase_CE 4.3.5.1 (r101010042025042417-0c7ffd37c2904f4d8191fb2d056738a93cce6d1d) (Built Apr 24 2025 17:44:55)
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 [testdb]> SELECT * FROM test;
+------+-----------+-------+
| id | code1 | code2 |
+------+-----------+-------+
| 1 | apple | a |
| 2 | banana | b |
| 3 | carambola | c |
+------+-----------+-------+
3 rows in set (0.018 sec)
obclient [testdb]> SHOW TABLES;
+------------------+
| Tables_in_testdb |
+------------------+
| test |
+------------------+
1 row in set (0.002 sec)
obclient [testdb]>

2.4.2 查看克隆租戶數(shù)據(jù)(與源租戶完全一致)
obclient -h127.0.0.1 -P2881 -uroot@clone_mysql001 -A -Dtestdb
obclient [testdb]> SELECT * FROM test;
obclient [testdb]> SHOW TABLES;
obclient [testdb]> exit
Bye
[root@iZbp1i39ztgukq3j591g2lZ backup]# obclient -h127.0.0.1 -P2881 -uroot@clone_mysql001 -A -Dtestdb
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3221586308
Server version: OceanBase_CE 4.3.5.1 (r101010042025042417-0c7ffd37c2904f4d8191fb2d056738a93cce6d1d) (Built Apr 24 2025 17:44:55)
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 [testdb]> SELECT * FROM test;
+------+-----------+-------+
| id | code1 | code2 |
+------+-----------+-------+
| 1 | apple | a |
| 2 | banana | b |
| 3 | carambola | c |
+------+-----------+-------+
3 rows in set (0.106 sec)
obclient [testdb]> SHOW TABLES;
+------------------+
| Tables_in_testdb |
+------------------+
| test |
+------------------+
1 row in set (0.027 sec)
obclient [testdb]>

三、 應(yīng)用場(chǎng)景測(cè)試
3.1 登錄源租戶使用 testdb 數(shù)據(jù)庫
[root@iZbp16hry3z746bjjlah7sZ ~]# obclient -h127.0.0.1 -P2881 -uroot@mysql_tenant -A
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3221514399
Server version: OceanBase_CE 4.3.5.1 (r101010042025042417-0c7ffd37c2904f4d8191fb2d056738a93cce6d1d) (Built Apr 24 2025 17:44:55)
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 [(none)]> create database testdb;
Query OK, 1 row affected (0.045 sec)
obclient [(none)]> USE testdb;
Database changed
obclient [testdb]>

3.2 場(chǎng)景1:大促報(bào)表加速測(cè)試
3.2.1 源租戶創(chuàng)建壓力測(cè)試表
obclient [testdb]> CREATE TABLE stress_test(
-> id INT AUTO_INCREMENT PRIMARY KEY,
-> code1 VARCHAR(10),
-> code2 VARCHAR(10)
-> );
Query OK, 0 rows affected (0.086 sec)
obclient [testdb]>

3.2.2 插入測(cè)試數(shù)據(jù)
obclient [testdb]>
obclient [testdb]> INSERT INTO stress_test (id, code1, code2)
-> SELECT
-> ROW_NUMBER() OVER () AS id, -- 使用行號(hào)生成唯一ID
-> SUBSTRING(MD5(RAND()), 1, 10) AS code1,
-> CHAR(65 + FLOOR(RAND() * 26)) AS code2
-> FROM
-> (SELECT 1) t1,
-> (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
-> (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
-> (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4,
-> (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t5
-> LIMIT 100000;
Query OK, 6561 rows affected (0.087 sec)
Records: 6561 Duplicates: 0 Warnings: 0
obclient [testdb]>

3.2.3 創(chuàng)建報(bào)表專用克隆租戶
[root@iZbp16hry3z746bjjlah7sZ ~]# obclient -h127.0.0.1 -P2881 -uroot@sys -A -Doceanbase
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3221584832
Server version: OceanBase_CE 4.3.5.1 (r101010042025042417-0c7ffd37c2904f4d8191fb2d056738a93cce6d1d) (Built Apr 24 2025 17:44:55)
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 [oceanbase]> CREATE RESOURCE UNIT clone_unit_config
-> MEMORY_SIZE = '2G',
-> MAX_CPU = 1, MIN_CPU = 1,
-> LOG_DISK_SIZE = '6G',
-> MAX_IOPS = 10000, MIN_IOPS = 10000, IOPS_WEIGHT=1;
Query OK, 0 rows affected (0.012 sec)
obclient [oceanbase]>

3.2.4 登錄sys租戶創(chuàng)建克隆租戶
[root@iZbp16hry3z746bjjlah7sZ ~]# obclient -h127.0.0.1 -P2881 -uroot@sys -A -Doceanbase
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3221604881
Server version: OceanBase_CE 4.3.5.1 (r101010042025042417-0c7ffd37c2904f4d8191fb2d056738a93cce6d1d) (Built Apr 24 2025 17:44:55)
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 [oceanbase]> CREATE TENANT report_tenant FROM mysql_tenant
-> WITH
-> RESOURCE_POOL = report_pool,
-> UNIT= clone_unit_config;
Query OK, 0 rows affected (52.103 sec)
obclient [oceanbase]>

3.2.5 驗(yàn)證克隆租戶狀態(tài)
obclient [oceanbase]> SELECT tenant_name, locality, status FROM oceanbase.DBA_OB_TENANTS;
+---------------+---------------+--------+
| tenant_name | locality | status |
+---------------+---------------+--------+
| sys | FULL{1}@zone1 | NORMAL |
| META$1002 | FULL{1}@zone1 | NORMAL |
| mysql_tenant | FULL{1}@zone1 | NORMAL |
| META$1004 | FULL{1}@zone1 | NORMAL |
| report_tenant | FULL{1}@zone1 | NORMAL |
+---------------+---------------+--------+
5 rows in set (0.023 sec)
obclient [oceanbase]>

3.3 性能對(duì)比測(cè)試
3.3.1 源租戶執(zhí)行復(fù)雜查詢(模擬生產(chǎn)負(fù)載)
[root@iZbp16hry3z746bjjlah7sZ ~]# obclient -h127.0.0.1 -P2881 -uroot@mysql_tenant -A -Dtestdb
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3221622776
Server version: OceanBase_CE 4.3.5.1 (r101010042025042417-0c7ffd37c2904f4d8191fb2d056738a93cce6d1d) (Built Apr 24 2025 17:44:55)
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 [testdb]> SELECT /*+ PARALLEL(4) */
-> code1, COUNT(*) total
-> FROM stress_test
-> GROUP BY code1;
+------------+-------+
| code1 | total |
+------------+-------+
| eee9fea870 | 1 |
| 491eb033cc | 1 |
| 50966ca7b2 | 1 |
| 6ca212ee1e | 1 |
| 5679a421e3 | 1 |
| 46a03214e9 | 1 |
| 99378b935d | 1 |
| 14a9a4d2ca | 1 |
| 3c9b108037 | 1 |
| 04594856b2 | 1 |
| 896aa859d6 | 1 |
| 906fa8b405 | 1 |
| b42a77ade9 | 1 |
| 83857a73a0 | 1 |
| 02f53a2f06 | 1 |
| 0f12b3ca6e | 1 |
| 96190122e5 | 1 |
| 1b5f46b6ce | 1 |
| eb01a2eec6 | 1 |
| 6bd948c024 | 1 |
+------------+-------+
6561 rows in set (0.038 sec)
obclient [testdb]>

3.3.2 克隆租戶執(zhí)行相同查詢
[root@iZbp16hry3z746bjjlah7sZ ~]# obclient -h127.0.0.1 -P2881 -uroot@report_tenant -A -Dtestdb
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3221670123
Server version: OceanBase_CE 4.3.5.1 (r101010042025042417-0c7ffd37c2904f4d8191fb2d056738a93cce6d1d) (Built Apr 24 2025 17:44:55)
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 [testdb]> SELECT /*+ PARALLEL(4) */
-> code1, COUNT(*) total
-> FROM stress_test
-> GROUP BY code1;
+------------+-------+
| code1 | total |
+------------+-------+
| 564ca437a1 | 1 |
| d464b16386 | 1 |
| eee9fea870 | 1 |
| 0e2c104423 | 1 |
| 6bd948c024 | 1 |
| 5b4226d2a5 | 1 |
| c8b2912a9b | 1 |
| c6261adba4 | 1 |
| b9c0ce2c37 | 1 |
+------------+-------+
6561 rows in set (0.011 sec)
obclient [testdb]>

結(jié)果:
/* 源租戶查詢耗時(shí):0.038秒 /
/ 克隆租戶查詢耗時(shí):0.011秒 /
/ 性能提升:3.45倍 */
3.4 場(chǎng)景2:復(fù)雜查詢隔離測(cè)試
3.4.1 配置日志同步的備租戶
[root@iZbp16hry3z746bjjlah7sZ ~]# obclient -h127.0.0.1 -P2881 -uroot@mysql_tenant -A
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3221591101
Server version: OceanBase_CE 4.3.5.1 (r101010042025042417-0c7ffd37c2904f4d8191fb2d056738a93cce6d1d) (Built Apr 24 2025 17:44:55)
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 [(none)]> ALTER SYSTEM SET LOG_ARCHIVE_DEST='LOCATION=file:///root/backup';
Query OK, 0 rows affected (0.025 sec)
obclient [(none)]> ALTER SYSTEM ARCHIVELOG;
Query OK, 0 rows affected (0.008 sec)
obclient [(none)]> SELECT * FROM oceanbase.DBA_OB_ARCHIVELOG\G
*************************** 1. row ***************************
DEST_ID: 1001
ROUND_ID: 1
INCARNATION: 1
DEST_NO: 0
STATUS: BEGINNING
START_SCN: 1753667550178615000
START_SCN_DISPLAY: 2025-07-28 09:52:30.178615
CHECKPOINT_SCN: 1753667550178615000
CHECKPOINT_SCN_DISPLAY: 2025-07-28 09:52:30.178615
COMPATIBLE: 1
BASE_PIECE_ID: 1
USED_PIECE_ID: 1
PIECE_SWITCH_INTERVAL: 86400000000
UNIT_SIZE: 1
COMPRESSION: none
INPUT_BYTES: 0
INPUT_BYTES_DISPLAY: 0.00MB
OUTPUT_BYTES: 0
OUTPUT_BYTES_DISPLAY: 0.00MB
COMPRESSION_RATIO: 0.00
DELETED_INPUT_BYTES: 0
DELETED_INPUT_BYTES_DISPLAY: 0.00MB
DELETED_OUTPUT_BYTES: 0
DELETED_OUTPUT_BYTES_DISPLAY: 0.00MB
COMMENT:
PATH: file:///root/backup
1 row in set (0.016 sec)
obclient [(none)]>

3.4.2 創(chuàng)建資源池(直接指定 zone 名稱)
obclient [oceanbase]>
obclient [oceanbase]> CREATE RESOURCE POOL standby_pool
-> UNIT = 'clone_unit_config',
-> UNIT_NUM = 1,
-> ZONE_LIST = ('zone1');
Query OK, 0 rows affected (0.020 sec)
obclient [oceanbase]>

3.4.3 創(chuàng)建備租戶(日志同步模式)
obclient [oceanbase]> CREATE TENANT standby_tenant FROM mysql_tenant
-> WITH
-> RESOURCE_POOL = clone_tnt_pool,
-> UNIT= clone_unit_config;
Query OK, 0 rows affected (26.058 sec)
obclient [oceanbase]>

3.4.4 驗(yàn)證同步狀態(tài)
obclient [oceanbase]> SELECT tenant_name, recovery_until_scn
-> FROM oceanbase.DBA_OB_TENANTS
-> WHERE tenant_name = 'standby_tenant';
+----------------+---------------------+
| tenant_name | recovery_until_scn |
+----------------+---------------------+
| standby_tenant | 1753692734368718000 |
+----------------+---------------------+
1 row in set (0.049 sec)
obclient [oceanbase]>

3.4.5 執(zhí)行復(fù)雜查詢隔離
– 在源租戶插入新數(shù)據(jù)
[root@iZbp19mqejjdm33rwu9a4oZ ~]# obclient -h127.0.0.1 -P2881 -uroot@mysql_tenant -A -Dtestdb
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3221617395
Server version: OceanBase_CE 4.3.5.1 (r101010042025042417-0c7ffd37c2904f4d8191fb2d056738a93cce6d1d) (Built Apr 24 2025 17:44:55)
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 [testdb]> INSERT INTO stress_test (code1, code2) VALUES ('obtest', 'd');
Query OK, 1 row affected (0.005 sec)
obclient [testdb]>

3.4.6 在備租戶執(zhí)行分析型查詢
[root@iZbp10sbp6nzy93j36giqtZ ~]# obclient -h127.0.0.1 -P2881 -uroot@standby_tenant -A -Dtestdb
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3221528306
Server version: OceanBase_CE 4.3.5.1 (r101010042025042417-0c7ffd37c2904f4d8191fb2d056738a93cce6d1d) (Built Apr 24 2025 17:44:55)
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 [testdb]> SELECT code1, COUNT(*) cnt
-> FROM test
-> GROUP BY code1
-> ORDER BY LENGTH(code1) DESC;
+-----------+------+
| code1 | cnt |
+-----------+------+
| carambola | 1 |
| banana | 1 |
| apple | 1 |
+-----------+------+
3 rows in set (0.129 sec)
obclient [testdb]>

/* 生產(chǎn)租戶持續(xù)寫入 /
/ 備租戶執(zhí)行分析型查詢 */
6. 親身體驗(yàn):在線實(shí)驗(yàn)與福利活動(dòng)
OceanBase專門設(shè)計(jì)了在線體驗(yàn)課程,無需搭建本地環(huán)境,即可按步驟實(shí)操租戶克隆。實(shí)驗(yàn)鏈接:《通過克隆租戶生成一個(gè)相同的新租戶》。


完成實(shí)驗(yàn)后,別忘了參與課后小測(cè)(小測(cè)地址),上傳實(shí)驗(yàn)截圖即可積累積分,還有機(jī)會(huì)贏取豐厚獎(jiǎng)品。若堅(jiān)持完成全部十期實(shí)驗(yàn)并通過結(jié)課考試,更有機(jī)會(huì)獲得OBCA/OBCP考試券!活動(dòng)詳情可參考:【積分 + 福利】OceanBase DBA 實(shí)戰(zhàn)營(yíng)(第二季)—— 體驗(yàn)再升級(jí),好禮不停息!

借用OB老師的圖
總結(jié)
OceanBase 4.3的租戶克隆功能,以“元數(shù)據(jù)拷貝+共享存儲(chǔ)”的創(chuàng)新方式,實(shí)現(xiàn)了租戶的秒級(jí)復(fù)制,既保證了隔離性,又極大提升了效率,完美解決了報(bào)表查詢、負(fù)載分流、版本回滾等實(shí)際業(yè)務(wù)痛點(diǎn)。
無論是DBA還是開發(fā)人員,都能通過簡(jiǎn)單操作快速掌握這一功能。正如“紙上得來終覺淺,實(shí)踐才能出真知”,強(qiáng)烈推薦大家參與本次實(shí)戰(zhàn)營(yíng)活動(dòng),親自體驗(yàn)租戶克隆的便捷——不僅能提升技能,還能贏取福利,何樂而不為呢?




