PostgreSQL码农集散地

3分钟上手体验OceanBase

3分钟上手体验OceanBase

和PolarDB一样, Oceanbase也是国产数据库, 并且也开源, 同时提供了docker镜像可以快速部署体验. 下面简单体验OceanBase.

在使用docker容器拉起Oceanbase之前, 可以先了解一下OB镜像是如何构建的? 这样我们在后面要调整环境变量、持久化数据或者排错时会比较容易, 镜像构建开源地址如下:

  • https://github.com/oceanbase/docker-images

PS: PolarDB是如何构建docker镜像的, 参考:

这个oceanbase-ce镜像是方便快速拉起测试oceanbase的一款镜像, 其他镜像参看该项目中其他目录的内容

  • https://github.com/oceanbase/docker-images/blob/main/oceanbase-ce/Dockerfile

ce镜像的entrypoint脚本如下, 示意拉起ob时会自动执行的脚本, 包括接收docker run提供的变量输入、初始化数据库、设置模式、设置环境变量等操作

  • https://github.com/oceanbase/docker-images/blob/main/oceanbase-ce/boot/start.sh

设置环境变量的脚本, 在start.sh中调用

  • https://github.com/oceanbase/docker-images/blob/main/oceanbase-ce/boot/env.sh

3分钟上手体验Oceanbase

mac m2 16g机器

1、拉取镜像

docker pull ghcr.io/oceanbase/oceanbase-ce  
$ docker images  
REPOSITORY                                                      TAG                    IMAGE ID       CREATED        SIZE  
ghcr.io/oceanbase/oceanbase-ce                                  latest                 690ba0455daf   2 months ago   704MB  
registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel   ubuntu22.04            171fc0d0953a   6 months ago   2.06GB  

2、启动容器

cd ~  
mkdir -p ~/ob  
mkdir -p ~/obd/cluster  

# Deploy a mini standalone instance using image from ghcr.io.  
docker run -d -p 2881:2881 -v $PWD/ob:/root/ob -v $PWD/obd/cluster:/root/.obd/cluster --name oceanbase ghcr.io/oceanbase/oceanbase-ce  

3、进入容器, 可以看到二进制和数据目录内容如下

$ docker exec -ti oceanbase bash  

[root@a4050c50acec .obd]# ls -larth /root/ob  
total 260K  
drwxr-xr-x  2 root root   64 Mar 11 02:32 .conf  
drwxr-xr-x  5 root root  160 Mar 11 02:32 store  
drwxr-xr-x  6 root root  192 Mar 11 02:32 bin  
drwxr-xr-x 38 root root 1.2K Mar 11 02:32 admin  
drwxr-xr-x  8 root root  256 Mar 11 02:32 lib  
drwxr-xr-x  9 root root  288 Mar 11 02:32 log
drwxr-xr-x  3 root root   96 Mar 11 02:32 audit  
drwxr-xr-x  6 root root  192 Mar 11 02:33 log_obshell  
drwxr-xr-x 10 root root  320 Mar 11 02:33 run  
dr-xr-x---  1 root root 4.0K Mar 11 02:33 ..  
-rw-r--r--  1 root root 244K Mar 11 02:34 .meta  
drwxr-xr-x 15 root root  480 Mar 11 02:34 .  
drwxr-xr-x 13 root root  416 Mar 11 02:34 etc  
drwxr-xr-x  4 root root  128 Mar 11 02:34 etc2  
drwxr-xr-x  4 root root  128 Mar 11 02:34 etc3  
[root@a4050c50acec .obd]# ls -larth /root/.obd/cluster  
total 8.0K  
drwxr-xr-x 1 root root 4.0K Mar 11 02:32 ..  
drwxr-xr-x 3 root root   96 Mar 11 02:32 .  
drwxr-xr-x 5 root root  160 Mar 11 02:32 obcluster  
[root@a4050c50acec ~]# top -c  

top - 02:34:16 up 1 min,  0 users,  load average: 16.34, 4.92, 1.71  
Tasks:  11 total,   1 running,  10 sleeping,   0 stopped,   0 zombie  
%Cpu(s): 41.3 us,  2.6 sy,  0.0 ni, 55.4 id,  0.0 wa,  0.0 hi,  0.7 si,  0.0 st  
MiB Mem :   7937.5 total,   3425.6 free,   3336.3 used,   1175.6 buff/cache  
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   4442.1 avail Mem   

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                                                                                               
  680 root      20   0 3285160   3.0g 184640 S 177.0  38.2   0:38.69 /root/ob/bin/observer -r 172.17.0.2:2882:2881 -p 2881 -P 2882 -z zone1 -n obcluster -c 1 -d /root/ob/store -l INFO -I 172.17.0.2 -o+  
    1 root      20   0    3796   2412   2156 S   0.0   0.0   0:00.00 /bin/bash /root/boot/start.sh                                                                                                         
    8 root      20   0   14892   3440   2580 S   0.0   0.0   0:00.00 /usr/sbin/sshd                                                                                                                        
 1204 root      20   0 1108724  31056  19556 S   0.0   0.4   0:00.02 /root/ob/bin/obshell daemon --ip 172.17.0.2 --port 2886                                                                               
 1224 root      20   0 1323880  85644  21964 S   0.0   1.1   0:00.17 /root/ob/bin/obshell server --ip 172.17.0.2 --port 2886                                                                               
 1310 root      20   0    3928   2824   2312 S   0.0   0.0   0:00.00 bash                                                                                                                                  
 1342 root      20   0    2632   1524   1396 S   0.0   0.0   0:00.16 obd cluster tenant create obcluster -n test -o express_oltp                                                                           
 1344 root      20   0  218192  59432  17392 S   0.0   0.7   0:00.54 obd cluster tenant create obcluster -n test -o express_oltp                                                                           
 1386 root      20   0   19840   8632   7480 S   0.0   0.1   0:00.00 sshd: root [priv]                                                                                                                     
 1390 root      20   0   19840   4912   3772 S   0.0   0.1   0:00.00 sshd: root                                                                                                                            
 1894 root      20   0    9216   3284   2772 R   0.0   0.0   0:00.00 top -c   

4、使用cli连接ob

# Connect to the root user of the sys tenant.  

$ docker exec -it oceanbase obclient -h127.0.0.1 -P2881 -uroot  

Welcome to the OceanBase.  Commands end with ; or \g.  
Your OceanBase connection id is 3221225473  
Server version: OceanBase_CE 4.2.1.10 (r110000072024112010-28c1343085627e79a4f13c29121646bb889cf901) (Built Nov 20 2024 10:13:05)  

Copyright (c) 2000, 2018, OceanBase and/or its affiliates. All rights reserved.  

Type 'help;' or '\h'forhelp. Type '\c' to clear the current input statement.  

obclient(root@(none))[(none)]>
obclient(root@(none))[(none)]> select version();
+-------------------------------+
| version()                     |
+-------------------------------+
| 5.7.25-OceanBase_CE-v4.2.1.10 |
+-------------------------------+
1 row inset (0.003 sec)
obclient(root@(none))[(none)]> help

General information about OceanBase can be found at  
https://www.oceanbase.com  

List of all client commands:  
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.  
clear     (\c) Clear the current input statement.  
connect   (\r) Reconnect to the server. Optional arguments are db and host.  
conn      (\) Reconnect to the server. Optional arguments are db and host.  
delimiter (\d) Set statement delimiter.  
edit      (\e) Edit command with $EDITOR.  
ego       (\G) Send command to OceanBase server, display result vertically.  
exit      (\q) Exit mysql. Same as quit.  
go        (\g) Send command to OceanBase server.  
help      (\h) Display this help.  
nopager   (\n) Disable pager, print to stdout.  
notee     (\t) Don'
t write into outfile.  
pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.  
print     (\p) Print current command.  
prompt    (\R) Change your mysql prompt.  
quit      (\q) Quit mysql.  
rehash    (\#) Rebuild completion hash.  
source    (\.) Execute an SQL script file. Takes a file name as an argument.  
status    (\s) Get status information from the server.  
system    (\!) Execute a system shell command.  
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.  
use       (\u) Use another database. Takes database name as argument.  
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.  
warnings  (\W) Show warnings after every statement.  
nowarning (\w) Don't show warnings after every statement.  

For server side help, type '
help contents'  

试几条和mysql兼容的命令

obclient(root@(none))[(none)]> show processlist;  
+------------+------+-----------------+------+---------+------+--------+------------------+  
| Id         | User | Host            | db   | Command | Time | State  | Info             |  
+------------+------+-----------------+------+---------+------+--------+------------------+  
| 3221487632 | root | 127.0.0.1:43516 | NULL | Query   |    0 | ACTIVE | show processlist |  
| 3221487620 | root | 127.0.0.1:35954 | ocs  | Sleep   |    0 | SLEEP  | NULL             |  
+------------+------+-----------------+------+---------+------+--------+------------------+  
2 rows inset (0.005 sec)  

obclient(root@(none))[(none)]> show databases;  
+--------------------+  
| Database           |  
+--------------------+  
| information_schema |  
| LBACSYS            |  
| mysql              |  
| oceanbase          |  
| ocs                |  
| ORAAUDITOR         |  
| SYS                |  
| test               |  
+--------------------+  
8 rows inset (0.007 sec)  

obclient(root@(none))[(none)]> show parameters;  
+-------+----------+------------+----------+-------------------------------------------------+-----------+----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------+---------+---------+-------------------+  
| zone  | svr_type | svr_ip     | svr_port | name                                            | data_type | value                | info                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         | section        | scope   | source  | edit_level        |  
+-------+----------+------------+----------+-------------------------------------------------+-----------+----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------+---------+---------+-------------------+  
| zone1 | observer | 172.17.0.2 |     2882 | ob_storage_s3_url_encode_type                   | NULL      | default              | Determines the URL encoding method for S3 requests."default": Uses the S3 standard URL encoding method."compliantRfc3986Encoding": Uses URL encoding that adheres to the RFC 3986 standard.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | OBSERVER       | CLUSTER | DEFAULT | DYNAMIC_EFFECTIVE |  
| zone1 | observer | 172.17.0.2 |     2882 | sql_protocol_min_tls_version                    | NULL      | none                 | SQL SSL control options, used to specify the minimum SSL/TLS version number. values: none, TLSv1, TLSv1.1, TLSv1.2, TLSv1.3   
...  

写入一些测试数据看看

use test
create table tbl (id int, info text, ts timestamp);  

DELIMITER //  

CREATE PROCEDURE InsertRandomData(IN num_rows INT)  
BEGIN  
    DECLARE i INT DEFAULT 0;  
    WHILE i < num_rows DO  
        INSERT INTO tbl (id, info, ts)  
        VALUES (i, MD5(RAND()), NOW());  
        SET i = i + 1;  
    END WHILE;  
END //  

DELIMITER ;  

-- 调用存储过程插入n条数据  
obclient(root@(none))[test]> CALL InsertRandomData(100);  
Query OK, 1 row affected (0.092 sec)  

obclient(root@(none))[test]> CALL InsertRandomData(10000);  
Query OK, 1 row affected (1.609 sec)  

obclient(root@(none))[test]> CALL InsertRandomData(100000);  
Query OK, 1 row affected (15.879 sec)  

// mysql好像不如PG批量写入方便, 如果是PG就这样:    
// insert into tbl select generate_series(1,100000), md5(random()::text), now();  

// 改成事务快多了, 避免每一条都刷redo  
obclient(root@(none))[test]> begin;  
Query OK, 0 rows affected (0.001 sec)  

obclient(root@(none))[test]> CALL InsertRandomData(100000);  
Query OK, 1 row affected (1.686 sec)  

obclient(root@(none))[test]> commit;  
Query OK, 0 rows affected (0.013 sec)  

非常方便.