浅谈OceanBase服务名(SERVICE_NAME)

服务名定义

官方声明

在 OceanBase 数据库 V2.x 或 V3.x 版本中,物理备库的产品形态为集群级主备,互为主备关系的集群的集群名及其对应的租户名均相同,而不同的主备库集群,其集群名也不相同,故集群名可以作为主备集群的唯一标识。用户在通过 ODP 连接数据库时,可以使用指定集群名的方式将连接自动路由到主集群。

从 V4.1.0 版本开始,物理备库的产品形态变更为租户级主备,主备租户所属的集群不一定相同,主备租户之间无感知,即主租户不记录备租户的信息,备租户也不记录主租户信息。为了实现租户级主备的自动路由,OceanBase 数据库引入了服务的概念。一个服务下可以有多个同集群或跨集群的租户。用户在通过 ODP 连接数据库时,可以使用指定服务名的方式将连接自动路由到主租户。

使用限制

不支持为 sys 租户或 Meta 租户创建服务。

一个用户租户最多只能创建一个服务。

不允许在通过服务名建立的 Session 中执行 CREATE SERVICE 命令。

该功能的使用依赖 OCP 和 ODP 等产品,相关产品的版本要求如下:

ODP:V4.3.1 及之后版本

OCP:V4.3.1 及之后版本

创建服务名

前提

官方声明租户创建服务名时,需要先确认租户的 Unit 中是否有临时下线的机器。如果有临时下线的机器,则在执行 CREATE SERVICE 命令后,可能会有部分机器暂时无法通过该服务名提供服务,建议等待临时下线的机器恢复或永久下线后再执行 CREATE SERVICE 命令。或者,用户也可以先执行 CREATE SERVICE 命令,等待临时下线的机器恢复或永久下线后再执行 START SERVICE 命令启动服务。

使用sys租户登录OB,执行以下SQL语句查看是否有临时下线的机器,tenant_id为目标租户ID

复制代码
[root@server061 1002]# mysql -h10.0.0.61 -P2883 -uroot@sys#obdemo61 -paaAA11__ -c -A
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2625955
Server version: 5.6.25 OceanBase 4.2.1.10 (r110020012024120420-760d3c19482ba306e5a10e214a8ec64422bbf1f0) (Built Dec  4 2024 20:33:18)

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
mysql> SELECT SVR_IP, SVR_PORT, LAST_OFFLINE_TIME, NOW() FROM oceanbase.DBA_OB_SERVERS WHERE LAST_OFFLINE_TIME IS NOT NULL AND (SVR_IP, SVR_PORT) IN (SELECT DISTINCT SVR_IP, SVR_PORT FROM oceanbase.DBA_OB_UNITS WHERE TENANT_ID = 1006);
Empty set (0.00 sec)

mysql>
mysql>
mysql>

为租户创建服务时,要求租户的 SWITCHOVER_STATUSNORMAL即保证租户不处于主备角色切换的中间状态。

复制代码
[root@server061 1002]# mysql -h10.0.0.61 -P2883 -uroot@sys#obdemo61 -paaAA11__ -c -A
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2625963
Server version: 5.6.25 OceanBase 4.2.1.10 (r110020012024120420-760d3c19482ba306e5a10e214a8ec64422bbf1f0) (Built Dec  4 2024 20:33:18)

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SELECT TENANT_ID, TENANT_NAME, TENANT_ROLE, SWITCHOVER_STATUS FROM oceanbase.DBA_OB_TENANTS WHERE TENANT_NAME = 'obmysql';
+-----------+-------------+-------------+-------------------+
| TENANT_ID | TENANT_NAME | TENANT_ROLE | SWITCHOVER_STATUS |
+-----------+-------------+-------------+-------------------+
|      1006 | obmysql     | PRIMARY     | NORMAL            |
+-----------+-------------+-------------+-------------------+
1 row in set (0.03 sec)

mysql>

由于一个服务下最多只有一个主租户,故在创建服务前,用户需要确认当前租户的角色

复制代码
[root@server061 1002]# mysql -h10.0.0.61 -P2883 -uroot@sys#obdemo61 -paaAA11__ -c -A
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2625965
Server version: 5.6.25 OceanBase 4.2.1.10 (r110020012024120420-760d3c19482ba306e5a10e214a8ec64422bbf1f0) (Built Dec  4 2024 20:33:18)

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SELECT TENANT_ID, TENANT_NAME, TENANT_ROLE FROM oceanbase.DBA_OB_TENANTS WHERE TENANT_NAME = 'obmysql';
+-----------+-------------+-------------+
| TENANT_ID | TENANT_NAME | TENANT_ROLE |
+-----------+-------------+-------------+
|      1006 | obmysql     | PRIMARY     |
+-----------+-------------+-------------+
1 row in set (0.02 sec)

mysql>
mysql>

查询待创建的服务下的租户情况

可以发现,目前没有租户使用 s_hz 服务名,可以为租户创建s_hz服务名

复制代码
mysql>  SELECT t.tenant_id, t.tenant_name, t.tenant_role
    -> FROM oceanbase.CDB_OB_SERVICES AS s JOIN oceanbase.DBA_OB_TENANTS AS t
    -> ON s.tenant_id = t.tenant_id
    -> WHERE s.service_name = 's_hz';
Empty set (0.03 sec)

mysql>
mysql>
黑屏方式

sys租户为普通租户创建服务名

复制代码
mysql>
mysql>
mysql> ALTER SYSTEM CREATE SERVICE s_hz TENANT = obmysql;
Query OK, 0 rows affected (0.04 sec)

mysql>
mysql>

用户租户为本租户创建服务名

复制代码
mysql> ALTER SYSTEM CREATE SERVICE s_hz 
Query OK, 0 rows affected (0.04 sec)

mysql>
mysql>

使用服务名登录

ip:ODP 的 IP 地址。

port:ODP 的端口号。

user_name:待登录的租户的用户名。

service_name:待登录的租户的服务名。

复制代码
[root@server061 1002]# mysql -h10.0.0.61 -P2883 -uroot@SERVICE:s_hz -paaAA11__
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2626368
Server version: 5.6.25 OceanBase 4.2.1.10 (r110020012024120420-760d3c19482ba306e5a10e214a8ec64422bbf1f0) (Built Dec  4 2024 20:33:18)

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye
白屏方式

白屏方式比较简单,在OCP上新建租户是就可以为该租户指定服务名

也可以在租户概览界面,手动添加服务名

相关推荐
寒山李白1 小时前
MySQL安装与配置详细讲解
数据库·mysql·配置安装
文牧之2 小时前
PostgreSQL 的扩展pg_freespacemap
运维·数据库·postgresql
deriva2 小时前
某水表量每15分钟一报,然后某天示数清0了,重新报示值了 ,如何写sql 计算每日水量
数据库·sql
Leo.yuan4 小时前
数据库同步是什么意思?数据库架构有哪些?
大数据·数据库·oracle·数据分析·数据库架构
Kookoos4 小时前
ABP VNext 与 Neo4j:构建基于图数据库的高效关系查询
数据库·c#·.net·neo4j·abp vnext
云之兕4 小时前
MyBatis 的动态 SQL
数据库·sql·mybatis
gaoliheng0064 小时前
Redis看门狗机制
java·数据库·redis
?ccc?4 小时前
MySQL主从复制与读写分离
数据库·mysql
会飞的Anthony5 小时前
数据库优化实战分享:高频场景下的性能调优技巧与案例解析
数据库