---
title: OceanBase 数据库 MySQL 租户 OBClient connect 用法-OceanBase数据库使用指南
description: 了解OceanBase数据库在实际应用中关于OceanBase 数据库 MySQL 租户 OBClient connect 用法相关的常见问题和使用技巧，帮助您快速解决OceanBase 数据库 MySQL 租户 OBClient connect 用法的难题。
---
切换语言

- 中文站 - 简体中文
- International - English
- 日本站 - 日本語

划线反馈

# OceanBase 数据库 MySQL 租户 OBClient connect 用法

更新时间：2026-06-03 03:01

适用版本： V3.2.x、V4.2.x 内容类型：TechNote  

本文介绍 OceanBase 数据库 MySQL 租户 obclient connect 用法。

一般使用 `use databasename` 切换数据库，此用法仅改变当前会话中的数据库而不重新建立连接，使用 `connect` 切换数据库则会重新建立连接。

此外，OceanBase 数据库 MySQL 租户使用 OBClient 命令行连接时，还支持使用 `connect` 连接到其他租户。

## 详细说明

OceanBase 数据库 MySQL 租户 OBClient connect 用法一般有以下三种。

#### 注意

第三种使用 `connect` 连接到其他租户的用法仅可在 OBClient 命令行下使用，在 MySQL 命令行中则不可以。

### 用法一 指定数据库

用法示例如下，切换到 `test2` 数据库并重新建立连接。

```shell
connect test2

```

从如下测试中，可以看到 `connection_id` 发生了变化，表示重新建立了连接。

```shell
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 数据库。

```shell
connect test 10.10.20.21

```

测试如下。

obclient [test]> connect test 10.10.20.21
Connection id:    3221959271
Current database: test

```

### 用法三 指定租户

#### 注意事项

1. 此用法仅可在 OBClient 命令行下使用，在 MySQL 命令行中则不可以。
 2. 指定租户连接时，其他信息如端口、密码等均需与当前连接信息一致。

**用法示例如下**。

- 如当前通过 2883 端口通过 OBProxy 连接，切换租户时需要指定集群名。

  ```shell
  connect root@obmysql#obcluster2

  ```

     - 通过 2883 端口通过 OBProxy 连接，切换租户示例如下。

      obclient [test]> connect root@obmysql#obcluster
      Connection id:    277946
      Current database: test

      ```
 - 如当前通过 2881 端口直连 OBServer，切换租户时不加集群名。

  ```shell
  connect root@obmysql

  ```

     - 通过 2881 端口直连 OBServer，切换租户示例如下。

      obclient [test]> connect root@obmysql
      Connection id:    3221529694
      Current database: test

      ```

**注意事项一**

**需要目标租户存在同名的数据库**，否则报错如下。

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 命令行切换租户时报错如下。

```shell
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.

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 版本。

上一篇

[OceanBase 数据库 V4.x 版本中建连接报错 4016](https://www.oceanbase.com/knowledge-base/oceanbase-database-1000000000780934)

下一篇

[通过 DBLink 去远端执行 SQL 语句时触发了嵌套 PL/SQL，导致执行 120s 后就会报 -4012 超时](https://www.oceanbase.com/knowledge-base/oceanbase-database-1000000004521618) ![有帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*y6ocSqN8cqsAAAAAAAAAAAAAARQnAQ)![无帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*BG9IQJyLHF8AAAAAAAAAAAAAARQnAQ)![反馈](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*eTWdQKCRKHwAAAAAAAAAAAAAARQnAQ)[AI](https://www.oceanbase.com/obi) 咨询热线
