浅谈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上新建租户是就可以为该租户指定服务名

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

相关推荐
运维行者_19 分钟前
ISP 企业级带宽计费怎么做?网络流量计费的 6 大核心能力
运维·服务器·网络·数据库·支持向量机·接口隔离原则
奇树谦24 分钟前
PCB生产制造全流程详解:从开料、钻孔、沉铜到检测、包装
网络·数据库·制造
驾驭人生42 分钟前
Hangfire.Redis.StackExchange 已归档停更!生产任务卡死、重复执行、网络抖动问题解决方案
数据库·redis·缓存
xierui12312343 分钟前
Agent记忆不是聊天记录:用三层存储构建可追溯的 AI 工作流
数据库·人工智能·aigc·软件工程
2301_800074211 小时前
mysql DQL
数据库·sql·mysql
西风未眠1 小时前
ClickHouse数据库引擎引起的数据丢失
数据库·clickhouse·故障
海浪仙人掌1 小时前
应收账款分析如何操作?应收账款分析有哪些注意事项?
数据库·人工智能
Elastic 中国社区官方博客1 小时前
在 Elasticsearch 中回填时间序列数据:通过批量 API 加载数月的历史指标数据
大数据·运维·数据库·人工智能·elasticsearch·搜索引擎·全文检索
Horn Still Sounds1 小时前
Linux网络并发服务器模型与SQLite数据库学习笔记
linux·服务器·数据库
昵称画2 小时前
POC验证怎么设计用例?不走过程的实操要点
大数据·数据库·人工智能·低代码·excel