OceanBase初體驗之Docker快速部署試用環境

balahoho發表於2024-03-13

前置條件

準備好一臺安裝了 Docker 的 Linux 伺服器,確保能夠連線到 Docker Hub 倉庫。

執行以下命令拉取最新的 OceanBase 映象:

docker pull oceanbase/oceanbase-ce

[root@localhost ~]# docker images
REPOSITORY                          TAG                            IMAGE ID       CREATED         SIZE
oceanbase/oceanbase-ce              latest                         92e357628ff3   8 days ago      652MB

啟動 OceanBase 例項

建立一個使用者儲存容器資料的目錄,在啟動容器時對映到容器中。

執行 docker run命令啟動 OceanBase 例項:

[root@localhost ~]# su - ob
[ob@localhost ~]$ mkdir docker-mount
[ob@localhost ~]$ sudo docker run -p 2881:2881 -v $PWD/docker-mount:/root/ob --name oceanbase-ce -e MODE=mini -d oceanbase/oceanbase-ce
2d5eb079940876e27947372f8f529fdb30ffd6935c4936d895e0f3ba83590935

[ob@localhost ~]$ sudo docker ps -a | grep oceanbase-ce
2d5eb0799408   oceanbase/oceanbase-ce                            "/bin/sh -c _boot"       2 minutes ago   Up 2 minutes                0.0.0.0:2881->2881/tcp                             oceanbase-ce

容器的環境變數引數說明可參考官方文件:https://hub.docker.com/r/oceanbase/oceanbase-ce

容器啟動後到服務準備就緒需要1-2分鐘的時間,我們可以透過容器日誌檢查啟動進度,當最後一行列印boot success即表示服務啟動成功,整個部署過程還是非常簡單的。

[ob@localhost ~]$ sudo docker logs oceanbase-ce | tail -1
Connect to observer ok
[ob@localhost ~]$ sudo docker logs oceanbase-ce | tail -1
Connect to observer ok
... ...
[ob@localhost ~]$ sudo docker logs oceanbase-ce | tail -1
boot success!

OceanBase 初體驗

OceanBase 的 Docker 映象內建了一個客戶端連線工具叫ob-mysql,可以直接使用這個工具連線到資料庫中,比如使用root使用者連線到test租戶:

[ob@localhost ~]$ sudo docker exec -it oceanbase-ce ob-mysql root
login as root@test
Command is: obclient -h127.1 -uroot@test -A -Doceanbase -P2881
Welcome to the OceanBase.  Commands end with ; or \g.
Your OceanBase connection id is 3221501946
Server version: OceanBase_CE 4.2.2.0 (r100010012024022719-c984fe7cb7a4cef85a40323a0d073f0c9b7b8235) (Built Feb 27 2024 19:20:54)

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 [oceanbase]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| oceanbase          |
| test               |
+--------------------+
4 rows in set (0.056 sec)

從列印的資訊中可以看到這個工具實際是封裝了obclient,這是 OceanBase 官方提供的一個客戶端連線工具。

當然也可以使用 mysql 客戶端來連線(這裡要注意使用者名稱的格式是:使用者名稱@租戶名):

[ob@localhost ~]$ mysql -h127.0.0.1 -P2881 -uroot@test
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3221553752
Server version: 5.7.25 OceanBase_CE 4.2.2.0 (r100010012024022719-c984fe7cb7a4cef85a40323a0d073f0c9b7b8235) (Built Feb 27 2024 19:20:54)

Copyright (c) 2000, 2023, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| oceanbase          |
| test               |
+--------------------+
4 rows in set (0.00 sec)

除了連線工具,Docker 映象也包含 OceanBase 自己的叢集運維工具 obd,我們可以使用如下方式來操作叢集:

[ob@localhost ~]$ sudo docker exec -it oceanbase-ce obd
You need to give some commands.

Try `obd --help` for more information.
Usage: obd <command> [options]

Available commands:

cluster        Deploy and manage a cluster.

demo           Quickly start

devmode        Developer mode switch

display-trace  display trace_id log.

env            Environment variables for OBD

mirror         Manage a component repository for OBD.

obdiag         Oceanbase Diagnostic Tool

repo           Manage local repository for OBD.

telemetry      Telemetry for OB-Deploy.By default, OBD telemetry is enabled. To disable OBD telemetry, run the `obd env set TELEMETRY_MODE 0` command. To enable OBD telemetry data printing, run `obd env set TELEMETRY_LOG_MODE 1`.

test           Run test for a running deployment.

tool           Tools

update         Update OBD.

web            Start obd deploy application as web.

Options:
  --version      show program's version number and exit
  -h, --help     Show help and exit.
  -v, --verbose  Activate verbose output.

obd cluster list 可以檢視部署了哪些 OceanBase 叢集,obd cluster display可以檢視指定叢集的拓撲結構。

對於剛才使用 Docker 部署的叢集,裡面啟動了兩個元件:observer 和 obagent。

[ob@localhost ~]$ sudo docker exec -it oceanbase-ce obd cluster list
+------------------------------------------------------------+
|                        Cluster List                        |
+-----------+------------------------------+-----------------+
| Name      | Configuration Path           | Status (Cached) |
+-----------+------------------------------+-----------------+
| obcluster | /root/.obd/cluster/obcluster | running         |
+-----------+------------------------------+-----------------+
Trace ID: 77ea24f4-e0e0-11ee-87c4-0242ac110003
If you want to view detailed obd logs, please run: obd display-trace 77ea24f4-e0e0-11ee-87c4-0242ac110003

[ob@localhost ~]$ sudo docker exec -it oceanbase-ce obd cluster display obcluster
Get local repositories and plugins ok
Open ssh connection ok
Cluster status check ok
Connect to observer 127.0.0.1:2881 ok
Wait for observer init ok
+---------------------------------------------+
|                   observer                  |
+-----------+---------+------+-------+--------+
| ip        | version | port | zone  | status |
+-----------+---------+------+-------+--------+
| 127.0.0.1 | 4.2.2.0 | 2881 | zone1 | ACTIVE |
+-----------+---------+------+-------+--------+
obclient -h127.0.0.1 -P2881 -uroot -Doceanbase -A

Connect to Obagent ok
+---------------------------------------------------------------+
|                            obagent                            |
+------------+--------------------+--------------------+--------+
| ip         | mgragent_http_port | monagent_http_port | status |
+------------+--------------------+--------------------+--------+
| 172.17.0.3 | 8089               | 8088               | active |
+------------+--------------------+--------------------+--------+
Trace ID: ba22c2cc-e0e0-11ee-8585-0242ac110003
If you want to view detailed obd logs, please run: obd display-trace ba22c2cc-e0e0-11ee-8585-0242ac110003

OceanBase 的資料操作

OceanBase 採用了多租戶設計,透過資源進行物理資源、邏輯結構上的隔離,因此在操作 OceanBase 前必須要指定一個具體的租戶。

建立租戶前,需首先確定租戶的資源配置、使用資源範圍等。租戶建立的通用流程如下:

  • 建立資源配置
  • 建立資源池
  • 建立租戶

建立資源池前最好檢查下節點剩餘資源(CPU、記憶體、磁碟)是否能夠支撐你的配置,要不然會建立失敗報錯,檢查方式如下:

# 建立資源配置
obclient [oceanbase]> CREATE RESOURCE UNIT uc1 MAX_CPU 1, MEMORY_SIZE '2G', LOG_DISK_SIZE '2G';
Query OK, 0 rows affected (0.029 sec)
# 建立資源池
obclient [oceanbase]> CREATE RESOURCE POOL rp1 UNIT 'uc1', UNIT_NUM 1;
Query OK, 0 rows affected (0.026 sec)
# 建立租戶
obclient [oceanbase]> CREATE TENANT tt resource_pool_list=('rp1')  set ob_tcp_invited_nodes = '%';
Query OK, 0 rows affected (25.628 sec)

值得注意的是,只有 root@sys 使用者擁有建立租戶的許可權。

在 OceanBase 的使用者體系中,使用者名稱@租戶名才是一個完整的身份標識,所有在使用使用者的地方都要用這種格式。

現在登入到新建立的租戶中:

[ob@localhost ~]$ mysql -h127.0.0.1 -P2881 -uroot@tt
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3221688400
Server version: 5.7.25 OceanBase_CE 4.2.2.0 (r100010012024022719-c984fe7cb7a4cef85a40323a0d073f0c9b7b8235) (Built Feb 27 2024 19:20:54)

Copyright (c) 2000, 2023, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| oceanbase          |
| test               |
+--------------------+
4 rows in set (0.01 sec)

mysql> create database tt_db1;
Query OK, 1 row affected (0.05 sec)

mysql> use tt_db1;
Database changed

mysql> create table t1(id int primary key,name varchar(50),dt datetime);
Query OK, 0 rows affected (0.14 sec)

mysql> select * from t1;
Empty set (0.01 sec)

mysql> insert into t1 values(1,'aaa',now());
Query OK, 1 row affected (0.01 sec)

mysql> select * from t1;
+----+------+---------------------+
| id | name | dt                  |
+----+------+---------------------+
|  1 | aaa  | 2024-03-13 14:27:02 |
+----+------+---------------------+
1 row in set (0.00 sec)

租戶裡面就可以做一些常規的資料物件管理和資料操作了,與使用mysql並沒有區別。

後續探索 OceanBase 更多有趣的玩法。

相關文章