前置條件
準備好一臺安裝了 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 更多有趣的玩法。