一、环境准备
1.1 服务器要求
生产环境中,每台机器上启动一个 observer 进程,所以一台机器就对应一个节点。因此理论上生产环境最小节点数为 3OceanBase + 1OBProxy,共需要4台服务器。如果考虑对obproxy做负载均衡和高可用,则应该再增加一个obproxy节点。
学习环境中,一台机器可以启动多个 observer 进程,模拟多个节点。每个节点的监听端口(默认是 2881 和 2882)、数据总目录是独立的,互不冲突。每个节点进程启动的最小内存是 10G,因此空间需要至少 10G 。
在该实验中,由于本人的硬件资源有限,因此该实验在一台内存为32GB,操作系统为centos7.4的主机上完成。
[root@master ~]# free -m
total used free shared buff/cache available
Mem: 31962 974 29712 10 1276 30462
Swap: 0 0 0
1.2 支持的操作系统
OceanBase 社区版支持的操作系统包括:
CentOS:推荐 7.2 以后版本。
Debian:推荐 9.8、10.9 版本。
openSUSE:推荐 15.2 版本。
OpenAnolis:推荐 8.2 版本。
SUSE: 推荐 15.2 版本。
Ubuntu:推荐 16.04、18.04、20.04 等版本。
1.3 目录规划
和 observer 相关的软件安装目录有如下几个:
如果是直接安装 OceanBase 数据库的 RPM 包,则需要提前创建好用户 admin ,数据库的 RPM 包将被自动安装在目录 /home/admin/oceanbase 下。
如果您是通过 OBD 软件自动化安装,则会将 RPM 包解压到 HOME 目录的隐藏文件夹 .obd 下,如:/.obd/repository/oceanbase-ce/3.1.0/afd11d52f83eef4b456d77969fde620c4bfba85e 。这种方式可以同时部署多个版本。
1.4 内核参数修改
vim /etc/sysctl.conf
net.core.somaxconn = 2048
net.core.netdev_max_backlog = 10000
net.core.rmem_default = 16777216
net.core.wmem_default = 16777216
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.ip_local_port_range = 3500 65535
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.tcp_syncookies = 0
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_slow_start_after_idle=0
vm.swappiness = 0
vm.min_free_kbytes = 2097152
vm.max_map_count=655360
fs.aio-max-nr=1048576
运行以下命令可让配置生效:
sysctl -p
1.5 修改会话变量设置
vi /etc/security/limits.conf
* soft nofile 655360
* hard nofile 655360
* soft nproc 655360
* hard nproc 655360
* soft core unlimited
* hard core unlimited
* soft stack unlimited
* hard stack unlimited
退出当前会话,重新登录。执行以下命令,查看配置是否生效:
ulimit -a
1.6 关闭防火墙和 SELinux
不同操作系统的防火墙设置可能会有不同,下文以 CentOS 系统为例进行讲解。
# 关闭防火墙
systemctl status firewalld
systemctl disable firewalld
systemctl stop firewalld
# 关闭 SELinux
修改 SELinux 配置文件中的 SELINUX 选项。
setenforce 0
vi /etc/selinux/config
将其中的SELINUX参数值改为disabled
SELINUX=disabled
保存后退出即可。
1.7 配置时间同步服务NTP (本实验由于三个节点都在同一主机上,因此此步骤不配置)
CentOS 或 RedHat 7.x 版本推荐使用 chrony 服务做时间源。Chrony 是 NTP(即 Network Time Protocol,网络时间协议,服务器时间同步的一种协议)的另一种实现,与 ntpd 不同的是,它可以更快且更准确地同步系统时钟,最大程度的减少时间和频率误差。
# 在集群中的任意一个节点安装chrony并配置为服务节点
yum -y install chrony
vi /etc/chrony.conf
修改以下参数并保存:
server 127.127.1.0
allow 192.168.1.0/24 #指定各节点的IP网段
local stratum 10 #提供的公网NTP服务器不可用时,采用本地时间作为同步标准
# 启动chronyd:
systemctl enable chronyd.service
systemctl start chronyd.service
systemctl status chronyd.service
# 集群其它节点安装并配置chrony:
yum -y install chrony
mv /etc/chrony.conf /etc/chrony.conf.bak
vim /etc/chrony.conf,新增如下代码:
server 192.168.1.146 iburst #为chrony服务节点的IP地址
rtcsync
local stratum 10
# 启动chronyd服务:
systemctl enable chronyd.service
systemctl start chronyd.service
systemctl status chronyd.service
# 查看时间同步活动
chronyc activity
# 查看时间服务器
chronyc sources
# 查看同步状态
chronyc sources -v
# 校准时间服务器
chronyc tracking
1.8 时区设置(可选)
timedatectl set-timezone Asia/Shanghai
chronyc -a makestep
1.9 配置安装用户
# 新增普通用户 admin
useradd admin
# 修改用户密码
passwd admin
# 或运行下面命令指定密码,密码修改为自己的。
echo 'admin:admin1' | chpasswd
1.10 配置 SSH 免密登录
如果使用自动化技术部署 OceanBase 集群,所有的命令通过中控机向 OceanBase 集群节点发出。则需要配置中控机中 OBD 运行的用户到 OceanBase 集群节点中 OBSERVER 安装的用户的 SSH 免密登录。
在中控机上操作:
su - admin
ssh-keygen -t rsa
ssh-copy-id `hostname -i` #打通到本机的 SSH 免密登录
(本实验由于三个节点都在同一主机上,因此以下步骤不配置)
生产环境中的规划:
OBD: 192.168.1.146
OBSERVER:
zone1 192.168.1.146
zone2 192.168.1.147
zone3 192.168.1.148
OBPROXY:192.168.1.149
OBCLIENT:192.168.1.149
ssh-copy-id admin@192.168.1.147 #打通到其它节点的 SSH 免密登录
ssh-copy-id admin@192.168.1.148 #打通到其它节点的 SSH 免密登录
ssh-copy-id admin@192.168.1.149 #打通到obproxy的 SSH 免密登录
二、使用OBD 自动化部署软件
2.1 本次实验时的集群规划
OBD: 192.168.18.28
OBSERVER:
zone1 192.168.18.28:2890
zone2 192.168.18.28:2891
zone3 192.168.18.28:2892
OBPROXY:192.168.18.28
OBCLIENT:192.168.18.28
2.2 安装OBD软件(在线方式)
cat <<EOF > /etc/yum.repos.d/aliyun_oceanbase.repo
[aliyun_oceanbase]
name =aliyun_oceanbase
baseurl=https://mirrors.aliyun.com/oceanbase/community/stable/el/7/x86_64
gpgcheck=0
enabled=1
EOF
yum install -y ob-deploy
2.2 编辑 OBD 配置文件
下载并编辑部署三节点 observer 的配置文件:
https://github.com/oceanbase/obdeploy/blob/master/example/mini-distributed-example.yaml
部署三节点 observer 和 obproxy 进程:
https://github.com/oceanbase/obdeploy/blob/master/example/mini-distributed-with-obproxy-example.yaml
由于github访问不稳定且速度慢,可以选择国内gitee访问:
https://gitee.com/oceanbase/obdeploy/tree/master/example
su - admin
$ git clone https://gitee.com/oceanbase/obdeploy.git
$ cp obdeploy/example/mini-distributed-with-obproxy-example.yaml .
$ mv mini-distributed-with-obproxy-example.yaml obconfig.yaml
以下是我在同一台服务器上同时安装三台observer+1台obproxy的配置文件:
[admin@master ~]$ cat obconfig.yaml | egrep -v "^#|^$"
oceanbase-ce:
servers:
- name: server1
ip: 192.168.18.28
- name: server2
ip: 192.168.18.28
- name: server3
ip: 192.168.18.28
global:
devname: br1
cluster_id: 1
stack_size: 512K
cpu_count: 16
cache_wash_threshold: 1G
__min_full_resource_pool_memory: 268435456
workers_per_cpu_quota: 10
schema_history_expire_time: 1d
net_thread_count: 4
major_freeze_duty_time: Disable
minor_freeze_times: 10
enable_separate_sys_clog: 0
enable_merge_by_turn: FALSE
appname: obcluster
server1:
mysql_port: 2890
rpc_port: 2900
home_path: /home/admin/observer1
zone: zone1
server2:
mysql_port: 2891
rpc_port: 2901
home_path: /home/admin/observer2
zone: zone2
server3:
mysql_port: 2892
rpc_port: 2902
home_path: /home/admin/observer3
zone: zone3
obproxy:
depends:
- oceanbase-ce
servers:
- 192.168.18.28
global:
home_path: /home/admin/obproxy
rs_list: 192.168.18.28:2890;192.168.18.28:2891;192.168.18.28:2892
enable_cluster_checkout: false
skip_proxy_sys_private_check: true
2.3 OBD 部署集群
obd cluster deploy [集群名] -c 集群配置文件
[admin@master ~]$ obd cluster deploy obce-cqiwen -c obconfig.yaml
Update OceanBase-community-stable-el7 ok
Update OceanBase-development-kit-el7 ok
Download oceanbase-ce-3.1.2-10000392021123010.el7.x86_64.rpm (46.45 M): 100% [#####################################################] Time: 0:01:18 622.20 kB/s
Package oceanbase-ce-3.1.2 is available.
Download obproxy-3.2.0-1.el7.x86_64.rpm (7.80 M): 100% [###########################################################################] Time: 0:00:12 636.94 kB/s
Package obproxy-3.2.0 is available.
install oceanbase-ce-3.1.2 for local ok
install obproxy-3.2.0 for local ok
+-------------------------------------------------------------------------------------------+
| Packages |
+--------------+---------+-----------------------+------------------------------------------+
| Repository | Version | Release | Md5 |
+--------------+---------+-----------------------+------------------------------------------+
| oceanbase-ce | 3.1.2 | 10000392021123010.el7 | 7fafba0fac1e90cbd1b5b7ae5fa129b64dc63aed |
| obproxy | 3.2.0 | 1.el7 | 8d5c6978f988935dc3da1dbec208914668dcf3b2 |
+--------------+---------+-----------------------+------------------------------------------+
Repository integrity check ok
Parameter check ok
Open ssh connection ok
Remote oceanbase-ce-3.1.2-7fafba0fac1e90cbd1b5b7ae5fa129b64dc63aed repository install ok
Remote oceanbase-ce-3.1.2-7fafba0fac1e90cbd1b5b7ae5fa129b64dc63aed repository lib check ok
Remote obproxy-3.2.0-8d5c6978f988935dc3da1dbec208914668dcf3b2 repository install ok
Remote obproxy-3.2.0-8d5c6978f988935dc3da1dbec208914668dcf3b2 repository lib check ok
Cluster status check ok
Initializes observer work home ok
Initializes obproxy work home ok
obce-cqiwen deployed
检查集群部署状态。
[admin@master ~]$ obd cluster list
+----------------------------------------------------------------------+
| Cluster List |
+-------------+--------------------------------------+-----------------+
| Name | Configuration Path | Status (Cached) |
+-------------+--------------------------------------+-----------------+
| obce-cqiwen | /home/admin/.obd/cluster/obce-cqiwen | deployed |
+-------------+--------------------------------------+-----------------+
2.4 OBD 启动和初始化集群
上面 deploy 操作只是安装了软件和准备初始化目录,还需使用命令 obd cluster start 启动集群节点并初始化集群。
[admin@master ~]$ obd cluster start obce-cqiwen
Get local repositories and plugins ok
Open ssh connection ok
Load cluster param plugin ok
Check before start observer ok
[WARN] (192.168.18.28) clog and data use the same disk (/)
Check before start obproxy ok
Start observer ok
observer program health check ok
Connect to observer ok
Initialize cluster
Cluster bootstrap ok
Wait for observer init ok
+-------------------------------------------------+
| observer |
+---------------+---------+------+-------+--------+
| ip | version | port | zone | status |
+---------------+---------+------+-------+--------+
| 192.168.18.28 | 3.1.2 | 2890 | zone1 | active |
| 192.168.18.28 | 3.1.2 | 2891 | zone2 | active |
| 192.168.18.28 | 3.1.2 | 2892 | zone3 | active |
+---------------+---------+------+-------+--------+
Start obproxy ok
obproxy program health check ok
Connect to obproxy ok
Initialize cluster
+-------------------------------------------------+
| obproxy |
+---------------+------+-----------------+--------+
| ip | port | prometheus_port | status |
+---------------+------+-----------------+--------+
| 192.168.18.28 | 2883 | 2884 | active |
+---------------+------+-----------------+--------+
obce-cqiwen running
查看启动后的集群状态:status变为running
[admin@master ~]$ obd cluster list
+----------------------------------------------------------------------+
| Cluster List |
+-------------+--------------------------------------+-----------------+
| Name | Configuration Path | Status (Cached) |
+-------------+--------------------------------------+-----------------+
| obce-cqiwen | /home/admin/.obd/cluster/obce-cqiwen | running |
+-------------+--------------------------------------+-----------------+
三、连接测试
3.1 使用obclient进行连接
# 安装obclient
yum install obclient
或者在机器上准备好离线安装包,并执行以下命令安装:
sudo yum install -y libobclient-2.0.0-2.el7.x86_64.rpm
sudo yum install -y obclient-2.0.0-2.el7.x86_64.rpm
# 连接数据库
obclient -uroot@sys#集群名 -h<IP地址> -P<proxy端口> -p<租户密码> -c -A <租户访问的数据库>
由于我刚才的配置文件obconfig.yam中未配置sys用户的密码,因此直接免密登录:
[root@master ~]# obclient -h 192.168.18.28 -P2883 -uroot@sys
Welcome to the OceanBase. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.6.25 OceanBase 3.1.2 (r10000392021123010-d4ace121deae5b81d8f0b40afbc4c02705b7fc1d) (Built Dec 30 2021 02:47:29)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| oceanbase |
| information_schema |
| mysql |
| SYS |
| LBACSYS |
| ORAAUDITOR |
| test |
+--------------------+
7 rows in set (0.004 sec)
3.2 使用mysql客户端进行连接
连接 OceanBase 集群的内部实例(sys)
传统的 MySQL 客户端可以连接 OceanBase 社区版,前提是 MySQL 的版本是 5.5/5.6/5.7。OceanBase 数据库也提供自己的客户端工具 obclient,需要安装后才可使用。
和传统MySQL 不同的是 OBPROXY 的连接端口是 2883(obproxy的端口), 连接用户名为 root@sys#集群名,密码则是在前面 OBD 配置文件里指定的。
由于我刚才的配置文件obconfig.yam中未配置sys用户的密码,因此直接免密登录:
[root@rhel701 ~]# mysql -h 192.168.18.28 -P2883 -uroot@sys
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 131073
Server version: 5.6.25 OceanBase 3.1.2 (r10000392021123010-d4ace121deae5b81d8f0b40afbc4c02705b7fc1d) (Built Dec 30 2021 02:47:29)
Copyright (c) 2000, 2021, 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>
查询集群状态的sql:
mysql> use oceanbase
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select a.zone,concat(a.svr_ip,':',a.svr_port) observer, cpu_total, (cpu_total-cpu_assigned) cpu_free, round(mem_total/1024/1024/1024) mem_total_gb, round((mem_total-mem_assigned)/1024/1024/1024) mem_free_gb, usec_to_time(b.last_offline_time) last_offline_time, usec_to_time(b.start_service_time) start_service_time, b.status, usec_to_time(b.stop_time) stop_time, b.build_version
-> from __all_virtual_server_stat a join __all_server b on (a.svr_ip=b.svr_ip and a.svr_port=b.svr_port)
-> order by a.zone, a.svr_ip;
+-------+--------------------+-----------+----------+--------------+-------------+----------------------------+----------------------------+--------+----------------------------+----------------------------------------------------------------------------------------+
| zone | observer | cpu_total | cpu_free | mem_total_gb | mem_free_gb | last_offline_time | start_service_time | status | stop_time | build_version |
+-------+--------------------+-----------+----------+--------------+-------------+----------------------------+----------------------------+--------+----------------------------+----------------------------------------------------------------------------------------+
| zone1 | 192.168.18.28:2900 | 14 | 11.5 | 4 | 3 | 1970-01-01 08:00:00.000000 | 2022-02-23 14:06:57.925673 | active | 1970-01-01 08:00:00.000000 | 3.1.2_10000392021123010-d4ace121deae5b81d8f0b40afbc4c02705b7fc1d(Dec 30 2021 02:47:29) |
| zone2 | 192.168.18.28:2901 | 14 | 11.5 | 4 | 3 | 1970-01-01 08:00:00.000000 | 2022-02-23 14:07:00.089905 | active | 1970-01-01 08:00:00.000000 | 3.1.2_10000392021123010-d4ace121deae5b81d8f0b40afbc4c02705b7fc1d(Dec 30 2021 02:47:29) |
| zone3 | 192.168.18.28:2902 | 14 | 11.5 | 4 | 3 | 1970-01-01 08:00:00.000000 | 2022-02-23 14:06:59.915368 | active | 1970-01-01 08:00:00.000000 | 3.1.2_10000392021123010-d4ace121deae5b81d8f0b40afbc4c02705b7fc1d(Dec 30 2021 02:47:29) |
+-------+--------------------+-----------+----------+--------------+-------------+----------------------------+----------------------------+--------+----------------------------+----------------------------------------------------------------------------------------+
3 rows in set (0.01 sec)
注:为了安全起见,应该及时为root用户设置密码
MySQL [mysql]> alter user root identified by 'root';
Query OK, 0 rows affected (0.128 sec)
四、停止和卸载oceanbase集群
obd工具对管理ob集群非常方便。在实验环境中,如果想卸载oceanbase集群,则使用如下方法即可:
# 卸载和清理oceanbase:
方法:停服务,清理软件和数据目录
obd cluster list
obd cluster stop obce-cqiwen --该命令会停止obproxy和observer集群
kill -9 `pidof observer`
rm -rf /home/admin/.obd
rm -rf /home/admin/observer*
rm -rf /home/admin/oceanbase
五、小结
通过操作实践,我发现oceanbase 集群的整体安装过程是比较简单和方便的,其中主要需要注意的就是obd配置文件的各参数是否设置正确。
由于实验环境有限,所以我是在单台服务器上布署的集群,其中有些问题被避开了。比如:ssh互信、时间同步、防火墙等方面可能产生的问题。所以如果大家有条件,还是在多台主机上布署操作一遍,方便发现未知的问题。
另外,我发现如果之前有mysql使用经验,对于oceanbase的上手还是挺快的。
更多关于oceanbase的功能体验,我将会在下一篇文章中陆续发布,敬请期待!