创建租户分为三步:
- (可选)创建资源单元规格。如果有合适的规格可以复用,就不用创建了。
- 创建资源池。可以每个
zone
一个资源池,使用独立的资源单元规格,也可以所有zone
使用同一个资源单元规格,都在一个资源池下。 - 创建租户,关联到这个资源池。
创建资源单元规格(RESOURCE UNIT
)
创建资源单元规格,并不会立即分配资源。资源单元规格元数据在视图 __all_unit_config
里。 创建之前可以先查看一下,如果有合适的规格,也是可以复用的。
- 语法
资源单元规格创建语法如下:
CREATE RESOURCE UNIT unit_name MAX_CPU [=] cpu_num, MAX_MEMORY [=] mem_size, MAX_IOPS [=] iops_num, MAX_DISK_SIZE [=] disk_size, MAX_SESSION_NUM [=] session_num, [MIN_CPU [=] cpu_num,][MIN_MEMORY [=] mem_size,] [MIN_IOPS [=] iops_num] ;
参数解释:
- 示例
下面例子创建 2 个资源池,分别使用不同的资源单元规格。且其中一个资源池横跨两个 Zone
。 这样用主要是为了演示资源池创建的灵活性。生产环境,为了管理方便,可以一个资源池横跨三个 Zone
,并且使用同一种资源单元规格。
create resource pool pool_1 unit='S1' , unit_num=1, zone_list=('zone1' ,'zone2') ; create resource pool pool_2 unit='S2' , unit_num=1, zone_list=('zone3'); select t1.name resource_pool_name, t2.`name` unit_config_name, t2.max_cpu, t2.min_cpu, t2.max_memory/1024/1024/1024 max_mem_gb, t2.min_memory/1024/1024/1024 min_mem_gb, t3.unit_id, t3.zone, concat(t3.svr_ip,':',t3.`svr_port`) observer,t4.tenant_id, t4.tenant_name from __all_resource_pool t1 join __all_unit_config t2 on (t1.unit_config_id=t2.unit_config_id) join __all_unit t3 on (t1.`resource_pool_id` = t3.`resource_pool_id`) left join __all_tenant t4 on (t1.tenant_id=t4.tenant_id) order by t1.`resource_pool_id`, t2.`unit_config_id`, t3.unit_id ; +--------------------+------------------+---------+---------+----------------+----------------+---------+-------+--------------------+-----------+-------------+ | resource_pool_name | unit_config_name | max_cpu | min_cpu | max_mem_gb | min_mem_gb | unit_id | zone | observer | tenant_id | tenant_name | +--------------------+------------------+---------+---------+----------------+----------------+---------+-------+--------------------+-----------+-------------+ | sys_pool | sys_unit_config | 5 | 5 | 1.500000000000 | 1.500000000000 | 1 | zone1 | 172.20.249.52:2882 | 1 | sys | | sys_pool | sys_unit_config | 5 | 5 | 1.500000000000 | 1.500000000000 | 2 | zone2 | 172.20.249.49:2882 | 1 | sys | | sys_pool | sys_unit_config | 5 | 5 | 1.500000000000 | 1.500000000000 | 3 | zone3 | 172.20.249.51:2882 | 1 | sys | | pool_1 | S1 | 3 | 3 | 3.000000000000 | 3.000000000000 | 1006 | zone1 | 172.20.249.52:2882 | NULL | NULL | | pool_1 | S1 | 3 | 3 | 3.000000000000 | 3.000000000000 | 1007 | zone2 | 172.20.249.49:2882 | NULL | NULL | | pool_2 | S2 | 4 | 4 | 3.000000000000 | 3.000000000000 | 1008 | zone3 | 172.20.249.51:2882 | NULL | NULL | +--------------------+------------------+---------+---------+----------------+----------------+---------+-------+--------------------+-----------+-------------+ 6 rows in set (0.037 sec) select a.zone,concat(a.svr_ip,':',a.svr_port) observer, cpu_total, cpu_assigned, (cpu_total-cpu_assigned) cpu_free, mem_total/1024/1024/1024 mem_total_gb, mem_assigned/1024/1024/1024 mem_assign_gb, (mem_total-mem_assigned)/1024/1024/1024 mem_free_gb from __all_virtual_server_stat a join __all_server b on (a.svr_ip=b.svr_ip and a.svr_port=b.svr_port) order by a.zone, a.svr_ip ; +-------+--------------------+-----------+--------------+----------+----------------+----------------+----------------+ | zone | observer | cpu_total | cpu_assigned | cpu_free | mem_total_gb | mem_assign_gb | mem_free_gb | +-------+--------------------+-----------+--------------+----------+----------------+----------------+----------------+ | zone1 | 172.20.249.52:2882 | 14 | 8 | 6 | 5.000000000000 | 4.500000000000 | 0.500000000000 | | zone2 | 172.20.249.49:2882 | 14 | 8 | 6 | 5.000000000000 | 4.500000000000 | 0.500000000000 | | zone3 | 172.20.249.51:2882 | 14 | 9 | 5 | 5.000000000000 | 4.500000000000 | 0.500000000000 | +-------+--------------------+-----------+--------------+----------+----------------+----------------+----------------+ 3 rows in set (0.026 sec)
资源池创建出来后,集群的可用资源就减少了。但是这个资源池还没有关联到具体租户,所以是无法被业务使用到。
创建租户(TENANT
)
租户就是实例,创建租户也是瞬间完成,需要关联到某个资源池。
- 语法
创建租户的语法如下:
CREATE TENANT [IF NOT EXISTS] tenant_name [tenant_characteristic_list] [opt_set_sys_var]; tenant_characteristic_list: tenant_characteristic [, tenant_characteristic...] tenant_characteristic: COMMENT 'string' | {CHARACTER SET | CHARSET} [=] charsetname | COLLATE [=] collationname | REPLICA_NUM [=] num | ZONE_LIST [=] (zone [, zone…]) | PRIMARY_ZONE [=] zone | DEFAULT TABLEGROUP [=] {NULL | tablegroup} | RESOURCE_POOL_LIST [=](poolname [, poolname…]) | LOGONLY_REPLICA_NUM [=] num | LOCALITY [=] 'locality description' opt_set_sys_var: {SET | SET VARIABLES | VARIABLES} system_var_name = expr [,system_var_name = expr] ...
参数解释:
示例:
create tenant obmysql resource_pool_list=('pool_1','pool_2'), primary_zone='RANDOM',comment 'mysql tenant/instance', charset='utf8' set ob_tcp_invited_nodes='%' ; MySQL [oceanbase]> select * from gv$tenant; +-----------+-------------+-------------------+-------------------+----------------+-----------------------+-----------+---------------------------------------------+ | tenant_id | tenant_name | zone_list | primary_zone | collation_type | info | read_only | locality | +-----------+-------------+-------------------+-------------------+----------------+-----------------------+-----------+---------------------------------------------+ | 1 | sys | zone1;zone2;zone3 | zone1;zone2,zone3 | 0 | system tenant | 0 | FULL{1}@zone1, FULL{1}@zone2, FULL{1}@zone3 | | 1001 | obmysql | zone1;zone2;zone3 | RANDOM | 0 | mysql tenant/instance | 0 | FULL{1}@zone1, FULL{1}@zone2, FULL{1}@zone3 | +-----------+-------------+-------------------+-------------------+----------------+-----------------------+-----------+---------------------------------------------+ 2 rows in set (0.005 sec)
租户创建的时候可以通过 set
命令指定租户变量(参数)值。
附录:
- 3.1 查看 OceanBase 集群资源的使用情况
- 3.2 如何创建和连接 MySQL 租户
- 3.3 如何连接租户
- 3.4 如何对租户参数(或变量)进行设置
- 3.5 如何使用 MySQL 租户做常见数据库开发
- 3.6 如何使用 OceanBase 分区表进行水平拆分
- 3.7 (高级)如何使用 OceanBase 表分组
- 3.8(高级)如何使用 OceanBase 复制表
- 3.9 常见问题
结束语
加入教程直播群方式一:钉钉群号3255 4020
加入教程直播群方式二:扫码下方二维码加入