刚入职大厂,老板让我5分钟部署一套OB集群

makefile 复制代码
作者:IT邦德
中国DBA联盟(ACDU)成员,10余年DBA工作经验
擅长主流数据Oracle、MySQL、PG、openGauss运维
备份恢复,安装迁移,性能优化、故障应急处理等

可提供技术业务:
1.DB故障处理/疑难杂症远程支援
2.Mysql/PG/Oracle/openGauss
数据库部署及数仓搭建
•••
微信:jem_db
QQ交流群:587159446
公众号:IT邦德
•••

@TOC

前言

本文通过OceanBase Docker容器,快速的体验 OceanBase 的 自动化部署过程,以及了解 OceanBase集群安装成功后的目录特点和使用方法

1.OceanBase 发展历程

2.Docker安装

2.1 安装依赖包

arduino 复制代码
yum install -y yum-utils device-mapper-persistent-data lvm2

sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo

2.2 在线安装

csharp 复制代码
[root@centos7 ~] yum -y install docker-ce docker-ce-cli containerd.io

2.3 启动 Docker

csharp 复制代码
[root@centos7 ~]# systemctl enable docker
[root@centos7 ~]# systemctl start docker
[root@centos7 ~]# systemctl status docker

[root@centos7 ~]# docker ps   --查看容器
[root@centos7 ~]# docker version  --查看版本
[root@centos7 ~]# docker info  --查看版本

3.环境要求

ruby 复制代码
1.有笔记本或服务器,内存至少12G 。
2.操作系统不限,能安装 Docker 环境即可。
Docker 官方镜像:
https://hub.docker.com/r/obpilot/oceanbase-ce

4.部署OB

4.1 下载镜像

csharp 复制代码
搜索 OceanBase 数据库相关镜像
[root@centos79 ~]# docker search oceanbase

拉取 OceanBase 数据库最新镜像
[root@centos79 ~]# docker pull oceanbase/oceanbase-ce


[root@centos79 ~]# docker images | grep oceanbase
oceanbase/oceanbase-ce   latest   92e357628ff3 7 days ago      652MB

4.2 创建容器

perl 复制代码
1.新容器创建
根据当前容器部署最大规格实例
docker run -p 2881:2881 --name oceanbase-ce -e MINI_MODE=0 -d oceanbase/oceanbase-ce
部署 mini 的独立实例
docker run -p 2881:2881 --name obstandalone -e MINI_MODE=1 -d oceanbase/oceanbase-ce

--以下命令执行后,需要等待3到5分钟
[root@centos79 ~]# docker run -p 2881:2881 --name oceanbase-ce -e MINI_MODE=0 -d oceanbase/oceanbase-ce
f21bf6621e1e6d404eb0dc8e945f6f9ab5a5b37e99b10a9058328ec1a44195ee

执行以下命令,如果返回 boot success!,则表示启动成功
[root@centos79 ~]# docker logs oceanbase-ce | tail -1
boot success!

Start observer ok
observer program health check ok
Connect to observer ok
Initialize oceanbase-ce ok
Start obagent ok
obagent program health check ok
Connect to Obagent 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

+---------------------------------------------------------------+
|                            obagent                            |
+------------+--------------------+--------------------+--------+
| ip         | mgragent_http_port | monagent_http_port | status |
+------------+--------------------+--------------------+--------+
| 172.17.0.2 | 8089               | 8088               | active |
+------------+--------------------+--------------------+--------+
obcluster running
Trace ID: 042ba7b6-e06f-11ee-90dd-0242ac110002
If you want to view detailed obd logs, please run: obd display-trace 042ba7b6-e06f-11ee-90dd-0242ac110002
/////////////////////// phase end: 140.273 s ///////////////////////
///////////////////////         STEP 5: Ob-deploy Create Tenant         ///////////////////////
Get local repositories and plugins ok
Open ssh connection ok
Connect to observer ok
Create tenant test ok
Trace ID: 57ef343a-e06f-11ee-98aa-0242ac110002
If you want to view detailed obd logs, please run: obd display-trace 57ef343a-e06f-11ee-98aa-0242ac110002
/////////////////////// phase end: 46.506 s ///////////////////////
deploy success!
boot success!

2.进入容器
[root@centos79 ~]# docker exec -it oceanbase-ce bash
[root@f21bf6621e1e ~]# cat /etc/redhat-release 
Anolis OS release 8.8
[root@f21bf6621e1e ~]# ls
boot  dest  ob  obagent  pkg  store
[root@f21bf6621e1e ~]# obclient -uroot@sys -h127.1 -P2881

3.连接OceanBase数据库实例

--使用 root 用户登录集群的 sys 租户
docker exec -it oceanbase-ce ob-mysql sys

--使用 root 用户登录集群的 root 租户
docker exec -it oceanbase-ce ob-mysql root

--使用 root 用户登录集群的 test 租户
docker exec -it oceanbase-ce ob-mysql test


[root@centos79 ~]# docker exec -it oceanbase-ce ob-mysql sys
login as root@sys
Command is: obclient -h127.1 -uroot@sys -A -Doceanbase -P2881 
Welcome to the OceanBase.  Commands end with ; or \g.
Your OceanBase connection id is 3221506698
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]> 

--修改用户
ALTER USER root IDENTIFIED BY '123456';

4.3 集群管理

sql 复制代码
--  查看集群列表
[root@centos79 ~]# docker exec -it oceanbase-ce bash
[root@f21bf6621e1e ~]# obd cluster list
+------------------------------------------------------------+
|                        Cluster List                        |
+-----------+------------------------------+-----------------+
| Name      | Configuration Path           | Status (Cached) |
+-----------+------------------------------+-----------------+
| obcluster | /root/.obd/cluster/obcluster | running         |
+-----------+------------------------------+-----------------+
Trace ID: 2703e982-e070-11ee-8e4b-0242ac110002
If you want to view detailed obd logs, please run: obd display-trace 2703e982-e070-11ee-8e4b-0242ac110002

配置文件:/root/.obd/cluster/obcluster/config.yaml
[root@f21bf6621e1e ~]# ls /root/.obd/cluster/obcluster/config.yaml
/root/.obd/cluster/obcluster/config.yaml

5.使用OceanBase

5.1 登陆OB

sql 复制代码
1.使用 root 用户登录到集群的 sys 租户

[root@f21bf6621e1e ~]# obclient -uroot@sys -h127.1 -P2881 -p123456 -A
Welcome to the OceanBase.  Commands end with ; or \g.
Your OceanBase connection id is 3221545291
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 [(none)]> 

2.进入 oceanbase 数据库。
obclient [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| LBACSYS            |
| mysql              |
| oceanbase          |
| ocs                |
| ORAAUDITOR         |
| SYS                |
| test               |
+--------------------+
8 rows in set (0.006 sec)

obclient [(none)]> USE oceanbase;
Database changed

5.2 创建租户

yaml 复制代码
OceanBase 数据库支持两种类型的租户,MySQL 兼容模式和 Oracle 兼容模式。创建租户时,您需要指定租户的类型。租户创建后,租户类型无法修改,因此创建租户前请规划好您的租户类型。

1.使用 root 用户登录到集群的 sys 租户。
[root@f21bf6621e1e ~]# obclient -uroot@sys -h127.1 -P2881 -A -p123456

2.进入 oceanbase 数据库。
obclient [(none)]> USE oceanbase;

3.通过 DBA_OB_TENANTS 视图,查看所有的租户信息
obclient [oceanbase]> SELECT * FROM oceanbase.DBA_OB_TENANTS\G
*************************** 1. row ***************************
                 TENANT_ID: 1
               TENANT_NAME: sys
               TENANT_TYPE: SYS
               CREATE_TIME: 2024-03-12 22:36:36.751973
               MODIFY_TIME: 2024-03-12 22:36:36.751973
              PRIMARY_ZONE: RANDOM
                  LOCALITY: FULL{1}@zone1
         PREVIOUS_LOCALITY: NULL
        COMPATIBILITY_MODE: MYSQL
                    STATUS: NORMAL
             IN_RECYCLEBIN: NO
                    LOCKED: NO
               TENANT_ROLE: PRIMARY
         SWITCHOVER_STATUS: NORMAL
          SWITCHOVER_EPOCH: 0
                  SYNC_SCN: NULL
            REPLAYABLE_SCN: NULL
              READABLE_SCN: NULL
        RECOVERY_UNTIL_SCN: NULL
                  LOG_MODE: NOARCHIVELOG
ARBITRATION_SERVICE_STATUS: DISABLED
                  UNIT_NUM: 1
                COMPATIBLE: 4.2.2.0
                 MAX_LS_ID: 1
*************************** 2. row ***************************
                 TENANT_ID: 1001
               TENANT_NAME: META$1002
               TENANT_TYPE: META
               CREATE_TIME: 2024-03-12 22:37:04.034661
               MODIFY_TIME: 2024-03-12 22:37:41.629058
              PRIMARY_ZONE: RANDOM
                  LOCALITY: FULL{1}@zone1
         PREVIOUS_LOCALITY: NULL
        COMPATIBILITY_MODE: MYSQL
                    STATUS: NORMAL
             IN_RECYCLEBIN: NO
                    LOCKED: NO
               TENANT_ROLE: PRIMARY
         SWITCHOVER_STATUS: NORMAL
          SWITCHOVER_EPOCH: 0
                  SYNC_SCN: NULL
            REPLAYABLE_SCN: NULL
              READABLE_SCN: NULL
        RECOVERY_UNTIL_SCN: NULL
                  LOG_MODE: NOARCHIVELOG
ARBITRATION_SERVICE_STATUS: DISABLED
                  UNIT_NUM: 1
                COMPATIBLE: 4.2.2.0
                 MAX_LS_ID: 1
*************************** 3. row ***************************
                 TENANT_ID: 1002
               TENANT_NAME: test
               TENANT_TYPE: USER
               CREATE_TIME: 2024-03-12 22:37:04.033335
               MODIFY_TIME: 2024-03-12 22:37:41.783779
              PRIMARY_ZONE: RANDOM
                  LOCALITY: FULL{1}@zone1
         PREVIOUS_LOCALITY: NULL
        COMPATIBILITY_MODE: MYSQL
                    STATUS: NORMAL
             IN_RECYCLEBIN: NO
                    LOCKED: NO
               TENANT_ROLE: PRIMARY
         SWITCHOVER_STATUS: NORMAL
          SWITCHOVER_EPOCH: 0
                  SYNC_SCN: 1710254307122913004
            REPLAYABLE_SCN: 1710254307122913004
              READABLE_SCN: 1710254307122913004
        RECOVERY_UNTIL_SCN: 4611686018427387903
                  LOG_MODE: NOARCHIVELOG
ARBITRATION_SERVICE_STATUS: DISABLED
                  UNIT_NUM: 1
                COMPATIBLE: 4.2.2.0
                 MAX_LS_ID: 1001
3 rows in set (0.026 sec)

4.通过 CREATE TENANT 语句,创建租户
obclient [oceanbase]> drop TENANT META$1002;

obclient [oceanbase]> 
CREATE TENANT IF NOT EXISTS mq_t1 
PRIMARY_ZONE='zone1', 
RESOURCE_POOL_LIST=('test_pool')
set OB_TCP_INVITED_NODES='%';

5.4 使用租户

sql 复制代码
租户创建成功后,可以尝试登录租户进行使用
默认管理员用户(MySQL 模式为 root,Oracle 模式为 sys)的密码为空,
您需要及时修改管理员用户的密码。

本次租户是MySQL 兼容模式
--登录 mq_t1 租户的 root 用户。
obclient -uroot@mq_t1 -h127.1 -P2881


--执行以下语句修改 root 用户的密码
obclient [(none)]> ALTER USER root IDENTIFIED BY '123456';

--退出后重新登陆
obclient -uroot@mq_t1 -h127.1 -P2881 -p123456

--创建数据库并指定字符集
CREATE DATABASE testdb DEFAULT CHARACTER SET UTF8;
obclient [(none)]> use testdb

--建表
CREATE TABLE course_tb(
course_id int(10) NOT NULL, 
course_name char(10) NOT NULL,
course_datetime char(30) NOT NULL);

INSERT INTO course_tb VALUES(1, 'Python', '2021-12-1 19:00-21:00');
INSERT INTO course_tb VALUES(2, 'SQL', '2021-12-2 19:00-21:00');
INSERT INTO course_tb VALUES(3, 'R', '2021-12-3 19:00-21:00');

obclient [testdb]> desc course_tb
    -> ;
+-----------------+----------+------+-----+---------+-------+
| Field           | Type     | Null | Key | Default | Extra |
+-----------------+----------+------+-----+---------+-------+
| course_id       | int(10)  | NO   |     | NULL    |       |
| course_name     | char(10) | NO   |     | NULL    |       |
| course_datetime | char(30) | NO   |     | NULL    |       |
+-----------------+----------+------+-----+---------+-------+

obclient [testdb]> select * from course_tb;
+-----------+-------------+-----------------------+
| course_id | course_name | course_datetime       |
+-----------+-------------+-----------------------+
|         1 | Python      | 2021-12-1 19:00-21:00 |
|         2 | SQL         | 2021-12-2 19:00-21:00 |
|         3 | R           | 2021-12-3 19:00-21:00 |
+-----------+-------------+-----------------------+

6.报错处理

sql 复制代码
1.创建资源池的报错
ERROR 4656 (HY000): resource pool unit num is bigger than zone server count
//说明:集群只有一个zone,且zone中只有一台observer ,所以无法创建 unit_num大于1的资源池;

向集群新增 OBServer xxx.xx.xxx.xx1。
obclient> ALTER SYSTEM ADD SERVER '127.0.0.1:2883' ZONE 'zone2';
obclient [oceanbase]> select * from GV$OB_SERVERS

2.ERROR 4012 (HY000): Timeout报错
sql响应超时时间设置太短了,所以当sql执行需要一定时间时就会报错4012
obclient [oceanbase]> show variables like '%timeout%';
+---------------------+------------------+
| Variable_name       | Value            |
+---------------------+------------------+
| connect_timeout     | 10               |
| interactive_timeout | 28800            |
| lock_wait_timeout   | 31536000         |
| net_read_timeout    | 30               |
| net_write_timeout   | 60               |
| ob_pl_block_timeout | 3216672000000000 |
| ob_query_timeout    | 10000000         |
| ob_trx_idle_timeout | 86400000000      |
| ob_trx_lock_timeout | -1               |
| ob_trx_timeout      | 86400000000      |
| wait_timeout        | 28800            |
+---------------------+------------------+
11 rows in set (0.015 sec)

# 租户下设置,防止超时
obclient> set global ob_query_timeout=86400000000000000;
obclient> set global ob_trx_timeout=86400000000000000;


ob_query_timeout用于设置查询超时时间,单位是微秒;
ob_trx_timeout用于设置事务超时时间,单位为微秒。

两个参数有GLOBAL和SESSION两种级别:
global参数的话是对全局session生效,但是不包括本次链接,重新登录生效;
如果不加global,就是仅对本次session生效,退出登录还是原来的值。


3.登陆OB如果遇到以下报错,重启容器即可
ERROR 2014 (HY000): Commands out of sync; you can't run this command now

7.总结

通过 OceanBase Docker 容器,快速的体验 OceanBase 的 自动化部署过程

相关推荐
冯志浩4 天前
Harmony Next - 图形绘制
harmonyos·掘金·金石计划
中杯可乐多加冰10 天前
【AI落地应用实战】HivisionIDPhotos AI证件照制作实践指南
人工智能·掘金·金石计划
冯志浩1 个月前
Harmony Next - 多线程技术 TaskPool
harmonyos·掘金·金石计划
宇宙之一粟1 个月前
设计快速并发哈希表
后端·rust·掘金·金石计划
宇宙之一粟1 个月前
【译】Go 迭代器的乐趣
后端·go·掘金·金石计划
雨绸缪1 个月前
ABAP 的 “小技巧 ”和 “陷阱 ”以及新语法
后端·代码规范·掘金·金石计划
冯志浩2 个月前
Harmony NEXT:如何给数据库添加自定义分词
harmonyos·掘金·金石计划
中杯可乐多加冰3 个月前
【AI落地应用实战】DAMODEL深度学习平台部署+本地调用ChatGLM-6B解决方案
人工智能·掘金·金石计划
中杯可乐多加冰4 个月前
Amazon Bedrock +Amazon Step Functions实现链式提示(Prompt Chaining)
人工智能·掘金·金石计划