首页 > 数据库 > 国内数据库 > 技术分享 | OceanBase 租户延迟删除
作者:杨涛涛
资深数据库专家,专研 MySQL 十余年。擅长 MySQL、PostgreSQL、MongoDB 等开源数据库相关的备份恢复、SQL 调优、监控运维、高可用架构设计等。目前任职于爱可生,为各大运营商及银行金融企业提供 MySQL 相关技术支持、MySQL 相关课程培训等工作。
本文来源:原创投稿
对于第二种方式,之前同事们有内部讨论过 OceanBase 的设计初衷:有可能是以防租户被误删、或者是给费用到期并且不续租的租户一段缓冲的时间,让他能在时间到期前备份自己的数据。
DROP TENANT [IF EXISTS] tenant_name [PURGE|FORCE];
先来创建两个新租户 tenant1、tenant2 。
mysql:5.6.25:oceanbase>create resource unit mini1 max_cpu 1,max_memory '1G',max_disk_size '1G',max_session_num 1200,max_iops 1000;
Query OK, 0 rows affected (0.015 sec)
<mysql:5.6.25:oceanbase>create resource pool p3 unit 'mini1',unit_num=1;
Query OK, 0 rows affected (0.038 sec)
<mysql:5.6.25:oceanbase>create resource pool p4 unit 'mini1',unit_num=1;
Query OK, 0 rows affected (0.062 sec)
<mysql:5.6.25:oceanbase>create tenant tenant1 resource_pool_list=('p3');
Query OK, 0 rows affected (2.660 sec)
<mysql:5.6.25:oceanbase>create tenant tenant2 resource_pool_list=('p4');
回收站开启,删除租户 tenant1 : 查询系统表 __all_tenant ,租户 tenant1 已经被删除,但是租户数据存放在 sys 租户回收站里,可以随时被恢复。
mysql:5.6.25:oceanbase>set recyclebin=on;
Query OK, 0 rows affected (0.000 sec)
<mysql:5.6.25:oceanbase>drop tenant tenant1;
Query OK, 0 rows affected (0.040 sec)
<mysql:5.6.25:oceanbase>show recyclebin;
+--------------------------------+---------------+--------+----------------------------+
| OBJECT_NAME | ORIGINAL_NAME | TYPE | CREATETIME |
+--------------------------------+---------------+--------+----------------------------+
| __recycle_$_1_1678073859469312 | tenant1 | TENANT | 2023-03-06 12:07:21.626602 |
+--------------------------------+---------------+--------+----------------------------+
1 row in set (0.012 sec)
<mysql:5.6.25:oceanbase>select tenant_name from __all_tenant where tenant_name = 'tenant1';
Empty set (0.001 sec)
从回收站恢复租户 tenant1 :再次查询系统表 __all_tenant ,租户 tenant1 恢复正常。
mysql:5.6.25:oceanbase>flashback tenant tenant1 to before drop;
Query OK, 0 rows affected (0.044 sec)
<mysql:5.6.25:oceanbase>show recyclebin;
Empty set (0.003 sec)
<mysql:5.6.25:oceanbase>select tenant_name from __all_tenant where tenant_name = 'tenant1';
+-------------+
| tenant_name |
+-------------+
| tenant1 |
+-------------+
1 row in set (0.002 sec)
回收站关闭,删除租户 tenant1:删除后,依然可以通过系统表 __all_tenant 查询到租户信息。
mysql:5.6.25:oceanbase>set recyclebin=off;
Query OK, 0 rows affected (0.001 sec)
<mysql:5.6.25:oceanbase>drop tenant tenant1;
Query OK, 0 rows affected (0.041 sec)
<mysql:5.6.25:oceanbase>select tenant_name from __all_tenant where tenant_name = 'tenant1';
+-------------+
| tenant_name |
+-------------+
| tenant1 |
+-------------+
1 row in set (0.001 sec)
直到配置项 schema_history_expire_time 设定的时间到期前,租户 tenant1 都可以对其内部对象正常操作:比如 DQL 语句 、DML 语句等。
[root@ytt-pc obytt111]# obclient -uroot@tenant1#ob-ytt -P 2883 -cA -h 127.1 -D ytt -e "select * from t1;"
+----+
| id |
+----+
| 1 |
| 2 |
+----+
[root@ytt-pc obytt111]# obclient -uroot@tenant1#ob-ytt -P 2883 -cA -h 127.1 -Dytt -e "insert into t1 values (10)"
但是无法执行 DDL 语句:比如新建表 t2 则会报错!
[root@ytt-pc obytt111]# obclient -uroot@tenant1#ob-ytt -P 2883 -cA -h 127.1 -Dytt -e "create table t2 like t1;"
ERROR 4179 (HY000) at line 1: ddl operation during dropping tenant not allowed
DROP TENANT tenant2 PURGE:延迟删除租户,同样是等配置项 schema_history_expire_time 设定的时间到期后,彻底将租户删除,和上面例子类似。
<mysql:5.6.25:oceanbase>drop tenant tenant2 purge;
Query OK, 0 rows affected (0.055 sec)
<mysql:5.6.25:oceanbase>select tenant_name from __all_tenant where tenant_name ='tenant2';
+-------------+
| tenant_name |
+-------------+
| tenant2 |
+-------------+
1 row in set (0.002 sec)
DROP TENANT tenant2 FORCE :后续不再需要租户 tenant2 ,可以立即删除。
mysql:5.6.25:oceanbase>drop tenant tenant2 force;
Query OK, 0 rows affected (0.050 sec)
<mysql:5.6.25:oceanbase>select tenant_name from __all_tenant where tenant_name = 'tenant2';
Empty set (0.002 sec)本文关键字:#OceanBase# #租户删除#
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70024924/viewspace-2938828/ ,如需转载,请注明出处,否则将追究法律责任。