本文介绍 OceanBase 数据库 MySQL 租户 obclient connect 用法。
一般使用 use databasename 切换数据库,此用法仅改变当前会话中的数据库而不重新建立连接,使用 connect 切换数据库则会重新建立连接。
此外,OceanBase 数据库 MySQL 租户使用 OBClient 命令行连接时,还支持使用 connect 连接到其他租户。
详细说明
OceanBase 数据库 MySQL 租户 OBClient connect 用法一般有以下三种。
注意
第三种使用 connect 连接到其他租户的用法仅可在 OBClient 命令行下使用,在 MySQL 命令行中则不可以。
用法一 指定数据库
用法示例如下,切换到 test2 数据库并重新建立连接。
connect test2
从如下测试中,可以看到 connection_id 发生了变化,表示重新建立了连接。
Server version: OceanBase 4.2.1.10 (r110000072024111216-17254329f8916c159fa250e86b0f4ef467da92c7) (Built Nov 12 2024 16:24:12)
Copyright (c) 2000, 2018, OceanBase and/or its affiliates. All rights reserved.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
obclient [test]> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| 3221650807 |
+-----------------+
1 row in set (0.000 sec)
obclient [test]> connect test2
Connection id: 3221634840
Current database: test2
obclient [test2]> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| 3221634840 |
+-----------------+
1 row in set (0.000 sec)
用法二 指定数据库和 IP
用法示例如下,切换到 10.10.20.21 上的 test 数据库。
connect test 10.10.20.21
测试如下。
Server version: OceanBase 4.2.1.10 (r110000072024111216-17254329f8916c159fa250e86b0f4ef467da92c7) (Built Nov 12 2024 16:24:12)
Copyright (c) 2000, 2018, OceanBase and/or its affiliates. All rights reserved.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
obclient [test]> connect test 10.10.20.21
Connection id: 3221959271
Current database: test
用法三 指定租户
注意事项
- 此用法仅可在 OBClient 命令行下使用,在 MySQL 命令行中则不可以。
- 指定租户连接时,其他信息如端口、密码等均需与当前连接信息一致。
用法示例如下。
如当前通过 2883 端口通过 OBProxy 连接,切换租户时需要指定集群名。
connect root@obmysql#obcluster2通过 2883 端口通过 OBProxy 连接,切换租户示例如下。
Server version: OceanBase 4.2.1.10 (r110000072024111216-17254329f8916c159fa250e86b0f4ef467da92c7) (Built Nov 12 2024 16:24:12) Copyright (c) 2000, 2018, OceanBase and/or its affiliates. All rights reserved. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. obclient [test]> connect root@obmysql#obcluster Connection id: 277946 Current database: test
如当前通过 2881 端口直连 OBServer,切换租户时不加集群名。
connect root@obmysql通过 2881 端口直连 OBServer,切换租户示例如下。
Server version: OceanBase 4.2.1.10 (r110000072024111216-17254329f8916c159fa250e86b0f4ef467da92c7) (Built Nov 12 2024 16:24:12) Copyright (c) 2000, 2018, OceanBase and/or its affiliates. All rights reserved. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. obclient [test]> connect root@obmysql Connection id: 3221529694 Current database: test
注意事项一
需要目标租户存在同名的数据库,否则报错如下。
Server version: OceanBase 4.2.1.10 (r110000072024111216-17254329f8916c159fa250e86b0f4ef467da92c7) (Built Nov 12 2024 16:24:12)
Copyright (c) 2000, 2018, OceanBase and/or its affiliates. All rights reserved.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
obclient [test]> use test2
Database changed
obclient [test2]> connect root@obmysql#obcluster
ERROR 1049 (42000): Unknown database 'test2'
obclient [test2]>
obclient [test2]> select connection_id();
No connection. Trying to reconnect...
ERROR 1049 (42000): Unknown database 'test2'
ERROR: Can't connect to the server
注意事项二
此用法仅可在 OBClient 命令行下使用,在 MySQL 命令行中则不可以。
使用 MySQL 命令行切换租户时报错如下。
Server version: 5.6.25 OceanBase 4.2.1.10 (r110000072024111216-17254329f8916c159fa250e86b0f4ef467da92c7) (Built Nov 12 2024 16:24:12)
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 [test]> connect root@obmysql#obcluster
ERROR 1049 (42000): Unknown database 'root@obmysql#obcluster'
适用的租户
以上内容仅适用于 OceanBase 数据库中的 MySQL 租户,不适用于 SYS 租户和 Oracle 租户。
适用版本
OceanBase 数据库 V3.2.x、V4.2.x 版本。