OceanBase初体验之Docker快速部署试用环境
前置条件
准备好一台安装了 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 更多有趣的玩法。
文章作者:hoho 首发论坛:博客园 文章出处:http://www.cnblogs.com/hohoa/ 欢迎大家一起讨论分享,喜欢请点右下角的推荐鼓励一下,我会有更多的动力来写出好文章!欢迎持续关注我的博客! 欢迎转载,转载的时候请注明作者和原文链接。