本文主要介绍通过 OBProxy 连接 OceanBase 数据库时的三段式用户名格式,以及如何通过调整 OBProxy 的参数配置来实现简化用户名登陆。
详细说明
三段式用户名格式
由于单个 OBProxy 支持为多个 OceanBase 集群提供路由服务,而 OceanBase 集群天然支持多租户,因此通过 OBProxy 访问 OceanBase 服务时,需要指定集群名、租户名和用户名,三段式格式有四种:
username@tenantname#clustername,如:root@trade#xxbank。
clustername:tenantname:username,如:xxbank:trade:root。
clustername-tenantname-username,如:xxbank-trade-root。
clustername.tenantname.username,如:xxbank.trade.root。
注意
仅 OceanBase 数据库 Oracle 模式下狭义的 username 不区分大小写,其它所有的字段(包括租户名、集群名)都是大小写敏感的,OceanBase 数据库 MySQL 模式下狭义的 username 也是区分大小写的。
测试验证
格式一:username@tenantname#clustername
$ mysql -hxx.xxx.80.111 -uroot@mysql#oio_cluster -P2883 -pxxx -A -c
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1577227
Server version: 5.6.25 OceanBase 4.2.5.3 (r103000142025033110-f5b88cd987f23383677f2eb53cf76ed38a48979b) (Built Mar 31 2025 11:04:29)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
格式二:clustername:tenantname:username
$ mysql -hxx.xxx.80.111 -uoio_cluster:mysql:root -P2883 -pxxx -A -c
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1577245
Server version: 5.6.25 OceanBase 4.2.5.3 (r103000142025033110-f5b88cd987f23383677f2eb53cf76ed38a48979b) (Built Mar 31 2025 11:04:29)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
格式三:clustername-tenantname-username
$ mysql -hxx.xxx.80.111 -uoio_cluster-mysql-root -P2883 -pxxx -A -c
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1577254
Server version: 5.6.25 OceanBase 4.2.5.3 (r103000142025033110-f5b88cd987f23383677f2eb53cf76ed38a48979b) (Built Mar 31 2025 11:04:29)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
格式四:clustername.tenantname.username
$ mysql -hxx.xxx.80.111 -uoio_cluster.mysql.root -P2883 -pxxx -A -c
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1577264
Server version: 5.6.25 OceanBase 4.2.5.3 (r103000142025033110-f5b88cd987f23383677f2eb53cf76ed38a48979b) (Built Mar 31 2025 11:04:29)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
如果这边的 username 本身已经包含了格式字符如 “-”,那么就需要使用其它的格式来登:
$ mysql -hxx.xxx.80.111 -uoio_cluster-mysql-test-user -P2883 -pxxx -A -c
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0
$ mysql -hxx.xxx.80.111 -uoio_cluster:mysql:test-user -P2883 -pxxx -A -c
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1658761
Server version: 5.6.25 OceanBase 4.2.5.3 (r103000142025033110-f5b88cd987f23383677f2eb53cf76ed38a48979b) (Built Mar 31 2025 11:04:29)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> select current_user();
+----------------+
| current_user() |
+----------------+
| test-user@% |
+----------------+
1 row in set (0.00 sec)
$ mysql -hxx.xxx.80.111 -uoio_cluster.mysql.test-user -P2883 -pxxx -A -c
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1658804
Server version: 5.6.25 OceanBase 4.2.5.3 (r103000142025033110-f5b88cd987f23383677f2eb53cf76ed38a48979b) (Built Mar 31 2025 11:04:29)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> select current_user();
+----------------+
| current_user() |
+----------------+
| test-user@% |
+----------------+
1 row in set (0.00 sec)
简化用户名模式
在非云环境部署的 OceanBase 集群中,通过 OBProxy 连接 OceanBase 数据库默认要求用户名必须包含租户和集群信息(比如:用户名@租户#集群)。为适配传统 MySQL 连接习惯(仅使用 -u用户名),可以通过调整下面几个 OBProxy 的参数来实现简化登录:
enable_full_username。
proxy_tenant_name。
rootservice_cluster_name。
下面对这三个参数逐一进行介绍:
enable_full_username
如果
enable_full_username=False,那么,通过root、root@sys、root@sys#集群名都是可以成功连接到 OBProxy+2883 的。如果
enable_full_username=True,那么,只能通过root@sys#集群名才能成功连接到 OBProxy+2883。
MySQL [oceanbase]> show proxyconfig like 'enable_full_username'; -- 如果设置被 False,可以不带集群名来访问 2883。默认值:True。
+----------------------+-------+-----------------------------------------------------------------------------+-------------+---------------+-------+--------------+
| name | value | info | need_reboot | visible_level | range | config_level |
+----------------------+-------+-----------------------------------------------------------------------------+-------------+---------------+-------+--------------+
| enable_full_username | True | used for non-cloud user, if set true, username must have tenant and cluster | false | SYS | | LEVEL_GLOBAL |
+----------------------+-------+-----------------------------------------------------------------------------+-------------+---------------+-------+--------------+
1 row in set (0.00 sec)
-- 启用简化用户名模式
MySQL [oceanbase]> alter proxyconfig set enable_full_username=False;
-- 恢复默认值
MySQL [oceanbase]> alter proxyconfig set enable_full_username=True;
注意:这种不带集群名+2883 端口访问的方式仅适用于该 OBProxy 中只添加了一个可连接的 OceanBase 集群,如果该 OBProxy 同时服务于多个 OceanBase 集群,不带集群名+2883 端口连接就会出现问题:
$ mysql -hxx.xxx.80.111 -uroot@sys -P2883 -pxxx -A -c
ERROR 4669 (HY000): cluster not exist
proxy_tenant_name
proxy_tenant_name 用于设置云用户的默认租户名称:
MySQL [(none)]> show proxyconfig like 'proxy_tenant_name'; -- 绑定 OBProxy 连接的默认租户,避免客户端显式指定租户。默认值:空字符串
+-------------------+-------+------------------------------------+-------------+---------------+-------+--------------+
| name | value | info | need_reboot | visible_level | range | config_level |
+-------------------+-------+------------------------------------+-------------+---------------+-------+--------------+
| proxy_tenant_name | | default tenant name for cloud user | false | SYS | | LEVEL_VIP |
+-------------------+-------+------------------------------------+-------------+---------------+-------+--------------+
1 row in set (0.01 sec)
-- 绑定默认租户(示例为 obmysql 租户)
ALTER PROXYCONFIG SET proxy_tenant_name = 'obmysql';
说明
该配置项一般需要配置 rootservice_cluster_name 一起使用。
rootservice_cluster_name
根据登录方式的不同,rootservice_cluster_name 配置项有如下几种作用:
通过 Service Name 登录时,该配置项表示读写分离下的默认备集群。
非 Service Name 登录时,该配置项用于设置
rootservice_list的默认集群名,或云用户的默认集群名。
说明
该配置项自 OBProxy V4.3.3 版本起更新,区分是否通过 Service Name 登录 Service Name 登录时,该配置项表示读写分离下的默认备集群。
MySQL [(none)]> show proxyconfig like 'proxy_cluster'; -- 默认值:undefined
+---------------+-----------+-------------------------------------------------------------------+-------------+---------------+-------+--------------+
| name | value | info | need_reboot | visible_level | range | config_level |
+---------------+-----------+-------------------------------------------------------------------+-------------+---------------+-------+--------------+
| proxy_cluster | undefined | proxy cluster name, mark which proxy cluster the proxy belongs to | false | SYS | | LEVEL_GLOBAL |
+---------------+-----------+-------------------------------------------------------------------+-------------+---------------+-------+--------------+
1 row in set (0.01 sec)
-- 设置该 OBProxy 默认连接的 OceanBase 数据库集群名
MySQL [(none)]> alter proxyconfig set rootservice_cluster_name='oio_cluster';
Query OK, 0 rows affected (0.01 sec)
测试验证如下:
$ mysql -hxx.xxx.80.111 -utest-user -P2883 -pxxx -A -c
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1655359
Server version: 5.6.25 OceanBase 4.2.5.3 (r103000142025033110-f5b88cd987f23383677f2eb53cf76ed38a48979b) (Built Mar 31 2025 11:04:29)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show tenant;
+---------------------+
| Current_tenant_name |
+---------------------+
| mysql |
+---------------------+
1 row in set (0.00 sec)
MySQL [(none)]> select current_user();
+----------------+
| current_user() |
+----------------+
| test-user@% |
+----------------+
1 row in set (0.00 sec)
适用版本
OceanBase 数据库 V2.x、V3.x、V4.x 版本。
OBProxy V3.x、V4.x 版本。