IvorySQL系列:初始IvorySQL 3.0 在CentOS 7.9下源码安装及不同兼容模式验证测试

一、前言

当提及瀚高(HGDB)国产数据库,许多人对其耳熟能详。这款数据库是基于开源的PostgreSQL经过精心打磨而来,在2011年正式面向数据库市场推出,供行业使用。相较于原生的PostgreSQL,瀚高数据库不仅融合了众多新功能和特性,更成为了一款卓越的国产数据库。目前,它已广泛应用于许多政企单位。

然而,对于IvorySQL数据库,很多人可能并不熟悉。初次接触时,许多人可能会误以为它只是一款用于操作数据库的工具,类似于sqlplus或SQL Developer,难以将其视为一款完整的数据库产品。

就像我一样,最近才开始了解这个数据库。怀着学习的心态,我也渴望更深入地了解它。

第一次接触IvorySQL,文章拙劣之处还望多多指正,非常感谢。

1.1 IvorySQL介绍

IvorySQL项目最初由瀚高公司发起,基于流行的PostgreSQL分支,发展成了一个遵循Apache 2.0开源协议的项目。截至到2024年1月,最新版本为IvorySQL Release 3.0 beta版。与PostgreSQL完全兼容,并且还集成了一些独特特性,这种融合让其在保持兼容性的同时增加了更多的功能。

IvorySQL社区对于与PostgreSQL的兼容性非常重视,并且承诺可以直接替代最新版本的PostgreSQL。为了更好地保持与Oracle和PostgreSQL的兼容性,IvorySQL特别增加了一个名为"ivorysql.compatible_mode"的GUC参数。通过这个参数,可以控制IvorySQL的兼容模式,比如通过使用"-m pg"参数,数据目录将使用PostgreSQL模式;而使用"-m oracle"参数或者不指定"-m"参数,则数据目录将采用兼容Oracle模式。这种设置方式挺有意思的,也能更好地适应不同用户的需求。

1.2 IvorySQL 3.0总体架构

根据IvorySQL介绍,3.0版本拥有丰富的特性及相关技术,比如包含了兼容性、高性能、安全性异构数据库访问等特性,如下是一副描述IvorySQL 3.0总体架构的图。

img

IvorySQL 3.0 采用了双parser和双端口接收外部请求的机制,并在原有架构基础上扩展了PL/iSQL框架。同时,借助插件IvorySQL_ORA,进一步实现了兼容性功能。

1.3 发展历程

下面我们来看下IvorySQL各版本的发展历程,这部分内容可详见github链接Releases · IvorySQL/IvorySQL · GitHub有详细介绍。

以下是IvorySQL各版本发展的时间线:

  • 2021年12月15日,基于PostgreSQL 14.0发布了 IvorySQL 1.0版本。

  • 2022年1月25日,基于PostgreSQL 14.1发布了 IvorySQL 1.1版本。

  • 2022年2月28日,基于PostgreSQL 14.2发布了 IvorySQL 1.2版本。

  • 2022年5月27日,基于PostgreSQL 14.3发布了 IvorySQL 1.3版本。

  • 2022年6月28日,基于PostgreSQL 14.4发布了 IvorySQL 1.4版本。

  • 2022年9月9日,基于PostgreSQL 14.5发布了 IvorySQL 1.5版本。

  • 2022年12月14日,基于PostgreSQL 15.1发布了 IvorySQL 2.1版本。

  • 2023年3月29日,基于PostgreSQL 15.2发布了 IvorySQL 2.2版本。

  • 2023年6月28日,基于PostgreSQL 15.3发布了 IvorySQL 2.3版本。

  • 2023年6月28日,同一天,IvorySQL推出了一个全新的大版本,脱离了IvorySQL 2这个版本系列,命名为IvorySQL 3.0 beta,这个版本是基于PostgreSQL 16.0 二次开发的。

以上是IvorySQL版本的演进过程。对于具体的版本发布内容和更新细节,建议查阅相关的GitHub链接以获取更详尽的信息。

1.4 版本介绍

IvorySQL 3.0基于PostgreSQL 16.0,并集成了来自PostgreSQL 16.0的多项修复。这些修复包括改进的排序性能、压缩技术、日志记录、配置增强以及更精细调节的逻辑复制选项等方面的优化。

与IvorySQL 3.X兼容,对于之前运行IvorySQL 2.X的用户而言,升级到3.0版本无需进行dump/restore操作。

根据官方网站的介绍,IvorySQL 3.0在架构上经历了重大变化,使用方式与2.3版本不同。某些在2.3版本中可用的功能在3.0中不再支持,例如功能模块中的内置数据类型、内置函数、NLS参数和对象大小写转换等,存在较大差异。

对于感兴趣的朋友,建议查阅官方网站IvorySQL :: IvorySQL文档中心,详细了解IvorySQL 3.0与IvorySQL 2.X之间的差异。这样可以更全面地了解新版本的特性和变化,以便作出更明智的选择和应用。

二、安装部署

学习一款新数据库,首要从探索其安装部署开始,这一过程对后续的学习和深入了解数据库的运作至关重要。

为了更深入了解IvorySQL数据库,我选择了通过源码方式安装IvorySQL 3.0。

源码安装数据库通常分为以下四步骤:

步骤一:环境准备

这一阶段通常涉及环境规划,包括调整系统参数(如系统内核参数),网络和主机名配置,创建必要的用户组和相应目录,设置适当的权限,并安装所需的软件依赖包。

步骤二:下载安装包

在这一步,需要从官方网站下载相应版本的源码安装包,并进行MD5校验,确保文件的完整性。接着将软件包上传至服务器指定目录。

步骤三:解压编译安装

解压下载的安装包后,通常会运行configure配置脚本进行编译,然后使用make和make install命令进行安装。

步骤四:初始化并启动数据库

在编译安装完成后,接下来需要运行initdb命令对数据库进行初始化,然后启动并创建所需的数据库等操作。

接下来,我们将按照上述四步编译安装IvorySQL 3.0数据库。

本次我选择的操作系统版本是CentOS Linux release 7.9.2009。

2.1 环境准备

2.1.1 软硬件要求

根据官网介绍,安装IvorySQL 3.0对软硬件有一定要求,软件要求操作系统为CentOS 7 或 CentOS 8,硬件最低配置要求具体如下:

硬件需求
CPU不低于4核
内存不低于4G
存储800MB以上,机械硬盘,推荐SSD或NvME
网络千兆或万兆网络

2.1.2 服务器信息

本次学习测试选择的服务器配置信息如下。

主机名IP地址操作系统版本系统内核版本服务器配置系统架构
ivorysqldb10.110.5.131CentOS 7.9.20093.10.0-693.el7.x86_648C\10Gx86_64位

2.2 环境设置

2.2.1 关闭防护墙

 # 使用 root 用户操作执行
 [root@ivorysqldb ~]# systemctl stop firewalld.service
 [root@ivorysqldb ~]# systemctl disable firewalld.service
 -- 查看状态,状态若为inactive (dead)表示防火墙已关闭
 [root@ivorysqldb ~]# systemctl status firewalld  

2.1.2 关闭selinux

 # 使用 root 用户操作执行
 [root@ivorysqldb ~]# sed -i  "s/SELINUX=enforcing/SELINUX=disabled/"  /etc/selinux/config
 # 也可以采用如下方式临时关闭服务器selinux
 [root@ivorysqldb ~]# setenforce 0

2.1.3 调整内核参数

生产环境为了更好的优化数据库通常会对系统内核参数进行设置。

可参照PostgreSQL 16.0安装配置要求修改内核参数,以下是案例参考,可视具体环境进行配置。

 # 使用 root 用户操作执行修改sysctl.conf配置文件 
 [root@ivorysqldb ~]# cat > /etc/sysctl.conf <<"EOF"
 -- kernel.shmmni 指定系统中可存在的共享内存标识符数量
 kernel.shmmni = 4096
 -- kernel.sem 设置信号量,依次表示:最大信号量数、最大信号量操作数、最大信号量标识符数、单个信号量的最大值
 kernel.sem = 50100 64128000 50100 1280
 -- fs.file-max 设置系统级别的文件描述符限制,即允许打开的文件描述符最大数
 fs.file-max = 7672460
 -- net.ipv4.ip_local_port_range 指定本地端口范围,用于为发起的 TCP/IP 连接分配端口
 net.ipv4.ip_local_port_range = 9000 65000
 -- net.core.rmem_default 设置套接字接收缓冲区的默认大小(字节)
 net.core.rmem_default = 1048576
 -- net.core.rmem_max 设置套接字接收缓冲区的最大大小(字节)
 net.core.rmem_max = 4194304
 -- net.core.wmem_default 设置套接字发送缓冲区的默认大小(字节)
 net.core.wmem_default = 262144
 -- net.core.wmem_max 设置套接字发送缓冲区的最大大小(字节)
 net.core.wmem_max = 1048576
 -- net.ipv4.tcp_tw_recycle TCP 连接快速回收,允许重用处于 TIME_WAIT 状态的连接
 net.ipv4.tcp_tw_recycle = 1
 -- net.ipv4.tcp_max_syn_backlog 设置 SYN 队列的最大长度
 net.ipv4.tcp_max_syn_backlog = 4096
 -- net.core.netdev_max_backlog 网络设备最大入队长度
 net.core.netdev_max_backlog = 10000
 -- net.ipv4.ip_conntrack_max 设置同时跟踪的最大连接数
 net.ipv4.ip_conntrack_max = 655360
 -- fs.aio-max-nr 设置异步 I/O 操作的最大数量
 fs.aio-max-nr = 1048576
 -- net.ipv4.tcp_timestamps 控制是否启用 TCP 时间戳
 net.ipv4.tcp_timestamps = 0
 EOF
 ​
 -- 上述修改执行后,执行sysctl -p 生效参数
 [root@ivorysqldb ~]# sysctl -p
 ​
 # 使用 root 用户操作执行修改limits.conf配置文件
 [root@ivorysqldb ~]# cat >> /etc/security/limits.conf <<"EOF" 
 -- nofile 允许打开的文件数限制
 -- nproc 允许创建的最大进程数限制
 -- core 核心转储文件大小的限制
 -- memlock 内存锁定量的限制
 * soft nofile 131072
 * hard nofile 131072
 * soft nproc 131072
 * hard nproc 131072
 * soft core unlimited
 * hard core unlimited
 * soft memlock 50000000
 * hard memlock 50000000
 EOF

2.1.4 安装依赖包

源码编译安装IvorySQL,必须确保操作系统已安装了相应的软件依赖包。

 -- 根据IvorySQL源码安装包 README.md 文件信息,对需要的软件包有以下版本要求。
 ​
 -- 必需软件包 构建 IvorySQL 需要以下软件包:
 make - GNU make 3.80 或更新版本
 gcc - ISO/ANSI C 编译器(至少符合 C99 标准)。建议使用较新的 GCC 版本。
 libreadline - 默认使用 GNU Readline 库。
 zlib - 默认使用 zlib 压缩库。
 Flex - (Flex 2.5.31 或更新)
 Bison - (Bison 1.875 或更新)
 ​
 -- 可选软件包 在默认配置下不需要,但在启用特定构建选项时需要以下可选软件包:
 libperl - (Perl 5.8.3 或更新)用于构建服务器编程语言 PL/Perl。Perl 库需包含头文件,且 libperl 库必须为共享库。
 libpython - (Python 2.6 或更新,Python 3.1 或更新)用于构建服务器编程语言 PL/Python。Python 库需包含头文件、distutils 模块,且 libpython 库必须为共享库。
 Tcl - (Tcl 8.4 或更新)用于构建过程式语言 PL/Tcl。
 Gettext API - (Tcl 8.4 或更新)用于启用本地化语言支持(NLS),以在英语以外的语言中显示程序的消息。
 OpenSSL - (1.0.1 或更新)用于支持加密客户端连接。
 LZ4 - 用于支持数据压缩。

按照上述要求安装软件依赖包。

 # 使用如下命令安装上述软件依赖包
 [root@ivorysqldb ~]# yum install -y bison-devel cmake make gcc zlib gcc-c++ perl readline readline-devel zlib zlib-devel openssl-devel python3-devel
 [root@ivorysqldb ~]# yum groupinstall -y 'Development Tools'
 ​
 # 软件包安装后可采用如下方式检查有无安装和软件版本
 rpm -q --qf '%{NAME}-%{VERSION}-%{RELEASE} (%{ARCH})\n' make \
 bison-devel \
 cmake \
 gcc \
 zlib \
 zlib-devel \
 openssl-devel \
 gcc-c++ \
 perl \
 readline \
 readline-devel \
 python3-devel

2.1.5 创建用户及组

 # 使用 root 用户操作执行
 [root@ivorysqldb ~]# /usr/sbin/groupadd -g 3000 ivorysql
 [root@ivorysqldb ~]# /usr/sbin/useradd -u 3001 -g ivorysql -G ivorysql ivorysql -c "IvorySQL3.0 Server"
 [root@ivorysqldb ~]# echo "ivorysql" | passwd --stdin ivorysql

2.1.7 创建目录

 # 使用 root 用户操作执行
 [root@ivorysqldb ~]# mkdir -p /db/ivorysql/{ivorysql3,data,soft}
 [root@ivorysqldb ~]# chown -R ivorysql:ivorysql /db/ivorysql
 [root@ivorysqldb ~]# chmod -R 775 /db/ivorysql

2.3 下载安装包

可以选择git方式从github官网GitHub - IvorySQL/IvorySQL: Open Source Oracle Compatible PostgreSQL.去拉取IvorySQL 数据库源代码到安装服务器,执行方式如下:

 git clone https://github.com/IvorySQL/IvorySQL.git

该方式要求提前在数据库服务器安装git。

本次选择直接到IvorySQL数据库github 官网 Release IvorySQL 3.0 · IvorySQL/IvorySQL · GitHub 主页下载源代码安装包并将其上传到数据库安装服务器。

登录 Release IvorySQL 3.0 · IvorySQL/IvorySQL · GitHub,在最下端可看到 IvorySQL 源码安装包,有两种压缩类型,分别是zip包和tar.gz包,如下所示。

image20240108143502581.png

本次选择tar.gz安装包进行下载,将下载的 IvorySQL-IvorySQL_3.0.tar.gz 安装包使用ivorysql用户上传到数据库安装服务器 /db/ivorysql/soft目录下。

解压 IvorySQL-IvorySQL_3.0.tar.gz 后,看到相应目录及文件信息如下所示。

image20240108144802899.png

2.4 编译安装

使用ivorysql用户解压下载的IvorySQL-IvorySQL_3.0.tar.gz 源码安装包,并执行编译安装,详细命令如下。

# 使用ivorysql解压安装包
1) 解压安装包
[root@ivorysqldb ~]# su - ivorysql
[ivorysql@ivorysqldb ~]$ cd /db/ivorysql/soft
[ivorysql@ivorysqldb soft]$ tar -zxvf IvorySQL-IvorySQL_3.0.tar.gz 

2) 运行configure
[ivorysql@ivorysqldb soft]$cd /db/ivorysql/soft/IvorySQL-IvorySQL_3.0
[ivorysql@ivorysqldb IvorySQL-IvorySQL_3.0]$ ./configure --prefix=/db/ivorysql/ivorysql3/ --with-pgport=5433 --with-perl --with-python

# 以上参数相关含义如下:
--prefix 指定源码安装目录
–with-pgport 指定psql端口
–with-perl 指定使用Perl语法的PL/Perl语言编写自定义函数
--with-python 指定使用Python语法的PL/Python语言编写自定义函数

3) 执行make
[ivorysql@ivorysqldb IvorySQL-IvorySQL_3.0]$ make

4) 执行make install
-- 待make执行完毕,无error报错信息,然后执行make install
[ivorysql@ivorysqldb IvorySQL-IvorySQL_3.0]$ make install

=====================================================================
-- 附录备注
1) 缺少ICU库
在执行configure时,若未安装ICU库,会编译报错,报错原因是系统默认未安装ICU库。
-- ICU(International Components for Unicode,Unicode国际组件)是一套成熟,使用广泛的开源全球化API。ICU利用Unicode庞大的通用语言环境数据存储库(Common Locale Data Repository,CLDR)作为其数据库,为软件应用程序提供了全球化支持。ICU具有广泛的可移植性,可以在所有平台上为应用程序提供相同的结果。
configure --prefix=/db/ivorysql/ivorysql3/ --with-pgport=5433 --with-perl --with-python在默认未安装ICU库执行结果及报错信息如下
[ivorysql@ivorysqldb IvorySQL-IvorySQL_3.0]$ ./configure --prefix=/db/ivorysql/ivorysql3/ --with-pgport=5433 --with-perl --with-python
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
-- 此处省略部分代码
checking whether to build with ICU support... yes
checking for icu-uc icu-i18n... no
configure: error: ICU library not found
If you have ICU already installed, see config.log for details on the
failure.  It is possible the compiler isn't looking in the proper directory.
Use --without-icu to disable ICU support.

-- 解决办法
可以编译安装时添加--without-icu 或者是安装icu库
通过如下方式安装ICU库
[root@ivorysqldb ~]# yum install -y libicu libicu-devel libunwind

2) 未安装Python3
若未安装Python3,执行编译的时候会报错,提示未定义_Py_LegacyLocaleDetected,如下所示
[ivorysql@ivorysqldb IvorySQL-IvorySQL_3.0]$ ./configure --prefix=/db/ivorysql/ivorysql3/ --with-pgport=5433 --with-perl --with-python
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
-- 省略部分代码
checking for Perl useshrplib... true
checking for CFLAGS recommended by Perl... -D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
checking for CFLAGS to compile embedded Perl... 
checking for flags to link embedded Perl...   -L/usr/lib64/perl5/CORE -lperl -lresolv -lnsl -ldl -lm -lcrypt -lutil -lpthread -lc
checking for python3... /bin/python3
/bin/python3: symbol lookup error: /bin/python3: undefined symbol: _Py_LegacyLocaleDetected
configure: using python 
./configure: line 10665: test: : integer expression expected
checking for Python sysconfig module... no
configure: error: sysconfig module not found

-- 解决办法
采用如下方式安装Python3
# root用户执行【所有节点】
-- 安装依赖包
[root@ivorysqldb ~]# yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
-- 创建安装目录
[root@ivorysqldb ~]# mkdir /usr/local/python3
[root@ivorysqldb ~]# cd /home/soft
-- 如服务器可以连接网络,可通过wget获取安装包,如无法联网,可下载安装包并上传至服务器
[root@ivorysqldb soft]# wget https://www.python.org/ftp/python/3.6.10/Python-3.6.10.tar.xz
-- 解压源码包
[root@ivorysqldb soft]# tar xvJf Python-3.6.10.tar.xz
[root@ivorysqldb soft]# cd Python-3.6.10
-- 配置编译参数
[root@ivorysqldb Python-3.6.10]# ./configure --prefix=/usr/local/python3 --enable-optimizations --enable-shared CFLAGS=-fPIC --with-ssl
-- 执行安装
[root@ivorysqldb Python-3.6.10]# make && make install
-- 创建链接
[root@ivorysqldb Python-3.6.10]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
=====================================================================

2.5 设置环境变量

设置ivorysql用户环境变量,使用ivorysql用户编辑/home/ivorysql/.bash_profile文件,设置如下内容。

[root@ivorysqldb ~]# su - ivorysql
[ivorysql@ivorysqldb ~]$  cat >>  ~/.bash_profile <<"EOF"
export LANG=en_US.UTF-8
export PGPORT=5433
export PGDATA=/db/ivorysql/data
export PGHOME=/db/ivorysql/ivorysql3
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH:.
export DATE=`date +"%Y%m%d%H%M"`
export PGHOST=$PGDATA
EOF

# 上述环境变量各参数含义如下
LANG: 设定语言为美式英语(UTF-8编码)
PGPORT: 设置 ivorysql 服务器监听的端口为 5433
PGDATA: 指定数据库存储位置为 /db/ivorysql/data
PGHOME: 设置 ivorysql 安装目录为 /db/ivorysql/ivorysql3
LD_LIBRARY_PATH: 设置动态链接库的搜索路径
PGHOST: ivorysql 数据库服务器地址设为本地

-- 执行source  ~/.bash_profile生效环境变量
[ivorysql@ivorysqldb ~]$ source  ~/.bash_profile

2.6 初始化数据库

ivorysql可以初始化至Oracle模式,也可以初始化至PG模式,通过-m 参数来控制初始模式。

-- 通过initdb --help 可以查看初始化相关参数
-- 和PostgreSQL 数据库initdb对比,可以发现ivorysql多了如下两个参数
  -m, --dbmode=MODE         set database mode, default is oracle
  -C, --case-conversion-mode=MODE   set case conversion mode, default is interchange

2.6.1 初始化至Oracle模式

# 使用ivorysql用户执行初始化操作,本次选择-m oracle模式,默认也是这种初始化模式
[ivorysql@ivorysqldb ~]$ /db/ivorysql/ivorysql3/bin/initdb -D /db/ivorysql/data/ -E UTF8 --locale=en_US.utf8 -U ivorysql -m oracle
The files belonging to this database system will be owned by user "ivorysql".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /db/ivorysql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /db/ivorysql/ivorysql3/bin/pg_ctl -D /db/ivorysql/data/ -l logfile start

2.6.2 初始化至PG模式

-- 若要在同一台数据库服务器初始化了Oracle模式,又要初始化PG模式,要保证两者初始化的目录不能相同。
-- 此处我将PG模式初始化至/db/ivorysql/pgdata目录下
# 使用ivorysql用户执行初始化操作至PG模式
[ivorysql@ivorysqldb ~]$ /db/ivorysql/ivorysql3/bin/initdb -D /db/ivorysql/pgdata/ -E UTF8 --locale=en_US.utf8 -U ivorysql -m pg
The files belonging to this database system will be owned by user "ivorysql".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /db/ivorysql/pgdata ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /db/ivorysql/ivorysql3/bin/pg_ctl -D /db/ivorysql/pgdata/ -l logfile start

同初始化至Oracle模式对比,Oracle模式下初始化目录多了ivorysql.conf这个配置文件,可以看下这个配置文件的内容,如下。

#------------------------------------------------------------------------------
#Oracle COMPATIBLE Mode GUC parameters
#------------------------------------------------------------------------------

#ivorysql.listen_addresses = 'localhost'                # what IP address(es) of Oracle mode to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
#ivorysql.port = 1521           # (change requires restart)

ivorysql.enable_emptystring_to_NULL = 'on'
shared_preload_libraries = 'liboracle_parser, ivorysql_ora'             # (change requires restart)
ivorysql.identifier_case_switch = interchange                           # set the case conversion mode. range [normal,interchange,lowercase]

-- ivorysql.conf配置文件用来配置Oracle兼容模式下的端口和一些其它兼容性的配置。
-- 比如ivorysql.port用来配置Oracle模式连接的端口,可以手动修改,重启后生效
-- liboracle_parser和ivorysql_ora这两项是实现Oracle兼容性的关键组件

2.7 修改配置文件参数

主要是修改postgresql.conf及pg_hba.conf两个配置文件

-- postgresql.conf 这个文件包含了 PostgreSQL 服务器的全局配置信息,可以控制诸如连接数量、日志记录、监听地址和端口等服务器行为,它影响整个 PostgreSQL 实例的运行
-- pg_hba.conf: 这个文件定义了客户端如何通过认证方式来连接到 PostgreSQL 数据库服务器,规则可以看作是一种访问的“白名单”或“黑名单”。

# 使用ivorysql用户进行修改
1) 修改postgresql.conf配置文件, 内容如下
[ivorysql@ivorysqldb ~]$ cd /db/ivorysql/data
[ivorysql@ivorysqldb data]$  cat >> /db/ivorysql/data/postgresql.conf <<"EOF"
listen_addresses = '*'
port=5433
unix_socket_directories='/db/ivorysql/data'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'ivorysql-%m-%d.log'
log_truncate_on_rotation = on
EOF

-- 上述配置文件各参数具体含义如下:
listen_addresses = '*': 允许服务器监听所有可用的网络接口,允许远程连接
port=5433: 数据库监听的端口号为 5433。
unix_socket_directories='/db/ivorysql/data': 指定 Unix 域套接字文件的目录位置
logging_collector = on: 开启日志收集器,用于收集数据库服务器的日志信息
log_directory = 'pg_log': 日志文件的存储目录为 'pg_log'
log_filename = 'ivorysql-%m-%d.log': 设置会使日志文件名包含月份和日期信息
log_truncate_on_rotation = on: 在日志轮转时清空旧日志文件

2) 修改pg_hba.conf配置文件, 内容如下
[ivorysql@ivorysqldb ~]$ cd /db/ivorysql/data
[ivorysql@ivorysqldb data]$ cat >> /db/ivorysql/data/pg_hba.conf << EOF
# TYPE  DATABASE    USER    ADDRESS       METHOD
host      all       all    0.0.0.0/0        md5
EOF

-- 上述配置文件具体含义如下:
TYPE: 指定认证的类型,这里是host,表示针对主机的连接。
DATABASE: 指定数据库名称,这里是all,表示适用于所有数据库。
USER: 指定数据库用户,这里也是all,表示适用于所有用户。
ADDRESS: 指定允许连接的主机地址,0.0.0.0/0表示允许来自任何IP地址的连接。
METHOD: 指定认证的方式,这里使用的是md5,表示使用MD5加密的密码验证方式。

2.8 启动数据库

-- 根据上面初始化后提供的启动数据库命令执行如下:
[ivorysql@ivorysqldb ~]$  /db/ivorysql/ivorysql3/bin/pg_ctl -D /db/ivorysql/data/ -l logfile start
-- 执行结果如下
waiting for server to start.... done
server started

-- 也可以使用更简洁的启动方式,如下所示:
[ivorysql@ivorysqldb ~]$ pg_ctl start
waiting for server to start....2024-01-08 19:14:33.236 CST [1660148] LOG:  redirecting log output to logging collector process
2024-01-08 19:14:33.236 CST [1660148] HINT:  Future log output will appear in directory "pg_log".
 done
server started

-- 查看端口号
[ivorysql@ivorysqldb ~]$ netstat -ano|grep 5433
tcp        0      0 0.0.0.0:5433            0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp6       0      0 :::5433                 :::*                    LISTEN      off (0.00/0/0)
unix  2      [ ACC ]     STREAM     LISTENING     24531128 /db/ivorysql/data/.s.PGSQL.5433

-- 查看数据库状态
[ivorysql@ivorysqldb ~]$ pg_ctl status
pg_ctl: server is running (PID: 1660148)
/db/ivorysql/ivorysql3/bin/postgres

-- 可以使用如下两种方式查看ivorysql3所基于的PostgreSQL版本都是16.0
[ivorysql@ivorysqldb ~]$ psql --version
psql (PostgreSQL) 16.0
[ivorysql@ivorysqldb ~]$ postgres --version
postgres (PostgreSQL) 16.0

-- 停止数据库可使用psql stop命令
[ivorysql@ivorysqldb ~]$ pg_ctl stop
waiting for server to shut down.... done
server stopped

2.9 登录数据库

2.9.1 Oracle兼容模式登录

-- 使用psql连接数据库
-- 此时如果直接使用psql连接数据库会报错,如下所示
[ivorysql@ivorysqldb ~]$ psql
psql: error: connection to server on socket "/db/ivorysql/data/.s.PGSQL.5433" failed: FATAL:  database "ivorysql" does not exist

-- 应该使用如下方式连接数据库
psql -d postgres 或者 psql -p1521 -d postgres
[ivorysql@ivorysqldb ~]$ psql -d postgres 
psql (16.0)
Type "help" for help.

postgres=# 
postgres=# \l
                                                      List of databases
   Name    |  Owner   | Encoding | Locale Provider |  Collate   |   Ctype    | ICU Locale | ICU Rules |   Access privileges   
-----------+----------+----------+-----------------+------------+------------+------------+-----------+-----------------------
 postgres  | ivorysql | UTF8     | libc            | en_US.utf8 | en_US.utf8 |            |           | 
 template0 | ivorysql | UTF8     | libc            | en_US.utf8 | en_US.utf8 |            |           | =c/ivorysql          +
           |          |          |                 |            |            |            |           | ivorysql=CTc/ivorysql
 template1 | ivorysql | UTF8     | libc            | en_US.utf8 | en_US.utf8 |            |           | =c/ivorysql          +
           |          |          |                 |            |            |            |           | ivorysql=CTc/ivorysql
(3 rows)

-- 此时可查看初始化至Oracle模式下相关参数,如下所示。
postgres=# select name,setting,short_desc from pg_settings where name like '%ivory%';
                name                 |   setting   |                              short_desc                              
-------------------------------------+-------------+----------------------------------------------------------------------
 ivorysql.compatible_mode            | pg          | Set default sql parser compatibility mode
 ivorysql.database_mode              | oracle      | Set database mode
 ivorysql.datetime_ignore_nls_mask   | 0           | Sets the datetime type input is not controlled by the NLS parameter.
 ivorysql.enable_emptystring_to_NULL | on          | whether convert empty string to NULL.
 ivorysql.identifier_case_switch     | interchange | Set character case conversion mode.
 ivorysql.listen_addresses           | localhost   | Sets oracle host name or IP address(es) to listen to.
 ivorysql.port                       | 1521        | Sets the Oracle TCP port the server listens on.
(7 rows)

-- 查看端口号
[ivorysql@ivorysqldb ~]$ netstat -ano|grep 1521
tcp        0      0 127.0.0.1:1521          0.0.0.0:*               LISTEN      off (0.00/0/0)
unix  2      [ ACC ]     STREAM     LISTENING     24667903 /db/ivorysql/data/.s.PGSQL.1521
[ivorysql@ivorysqldb ~]$ netstat -ano|grep 5433
tcp        0      0 127.0.0.1:5433          0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp6       0      0 :::5433                 :::*                    LISTEN      off (0.00/0/0)
unix  2      [ ACC ]     STREAM     LISTENING     24651770 /tmp/.s.PGSQL.5433
unix  2      [ ACC ]     STREAM     LISTENING     24667902 /db/ivorysql/data/.s.PGSQL.5433

-- ivorysql.compatible_mode表示兼容模式是PG
-- ivorysql.database_mode 表示数据库模式是Oracle
-- ivorysql.port  表示数据库端口号是1521

image20240109102711971.png

2.9.2 PG兼容模式登录

-- 使用如下方式以PG兼容模式登录数据库
[ivorysql@ivorysqldb ~]$ psql -p 5433 -d postgres
psql (16.0)
Type "help" for help.

postgres=# select name,setting,short_desc from pg_settings where name like '%ivory%';
                name                 |   setting   |                              short_desc                              
-------------------------------------+-------------+----------------------------------------------------------------------
 ivorysql.compatible_mode            | pg          | Set default sql parser compatibility mode
 ivorysql.database_mode              | oracle      | Set database mode
 ivorysql.datetime_ignore_nls_mask   | 0           | Sets the datetime type input is not controlled by the NLS parameter.
 ivorysql.enable_emptystring_to_NULL | on          | whether convert empty string to NULL.
 ivorysql.identifier_case_switch     | interchange | Set character case conversion mode.
 ivorysql.listen_addresses           | localhost   | Sets oracle host name or IP address(es) to listen to.
 ivorysql.port                       | 1521        | Sets the Oracle TCP port the server listens on.
(7 rows)

postgres=# 

image20240109102743605.png

三、 测试

3.1 Oracle兼容模式测试

 -- 使用如下方式登录Oracle兼容模式
 [ivorysql@ivorysqldb ~]$ psql -p 1521 postgres
 psql (16.0)
 Type "help" for help.
 ​
 -- 可以看到熟悉的dual虚拟表
 postgres=# select * from dual;
  dummy
 -------
  X
 (1 row)
 ​
 postgres=# select sysdate,systimestamp from dual;
   sysdate   |           systimestamp
 ------------+-----------------------------------
  2024-01-09 | 2024-01-09 11:11:55.758130 +08:00
 (1 row)
 ​
 postgres=# CREATE TABLE CORE_PRODUCT
 postgres-# (
 postgres(#   COM_ID                         NUMBER(10)     NOT NULL,
 postgres(#   PROD_ID                        NUMBER(10)     NOT NULL,
 postgres(#   OTHER_CAT_CODE                 VARCHAR2(255 BYTE),
 postgres(#   PROD_KEYWORD                   VARCHAR2(500 BYTE) NOT NULL,
 postgres(#   PROD_PRICE                     VARCHAR2(50 BYTE) DEFAULT '0',
 postgres(#   PROD_CUSTOM                    VARCHAR2(4000 BYTE),
 postgres(#   PROD_DESCRIPT                  VARCHAR2(4000 BYTE) NOT NULL,
 postgres(#   RECOMMEND                      VARCHAR2(1 BYTE) DEFAULT '0' NOT NULL,
 postgres(#   PHOTO_EXI_FLAG                 VARCHAR2(1 BYTE) DEFAULT '0',
 postgres(#   PREP_TIME                      DATE           DEFAULT TO_DATE('1900-01-01','YYYY-MM-DD HH24:MI:SS'),
 postgres(#   UPDATE_TIME                    DATE           DEFAULT TO_DATE('1900-01-01','YYYY-MM-DD HH24:MI:SS') NOT NULL,
 postgres(#   ADD_TIME                       DATE           DEFAULT TO_DATE('1900-01-01','YYYY-MM-DD HH24:MI:SS') NOT NULL,
 postgres(#   COM_NAME                       VARCHAR2(255 BYTE) DEFAULT ' ',
 postgres(#   REP_TIME                       TIMESTAMP(6) WITH TIME ZONE DEFAULT TO_DATE('1900-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS') NOT NULL,
 postgres(#   LAST_CHECK_TIME                DATE           DEFAULT TO_DATE('1900-01-01','YYYY-MM-DD HH24:MI:SS')
 postgres(# );
 CREATE TABLE
 postgres=#
 postgres=# ALTER TABLE CORE_PRODUCT ADD (
 postgres(#   CONSTRAINT PK_COREPRODSEARCH
 postgres(#   PRIMARY KEY(PROD_ID));
 ERROR:  syntax error at or near "CONSTRAINT"
 LINE 2:   CONSTRAINT PK_COREPRODSEARCH
           ^
 postgres=#
 postgres=# CREATE OR REPLACE TRIGGER REP_CORE_PRODUCT
 postgres-# BEFORE
 postgres-#   INSERT OR UPDATE ON CORE_PRODUCT FOR EACH ROW
 postgres-# BEGIN
 postgres-#    IF :OLD.REP_TIME IS NULL OR :OLD.REP_TIME < SYSTIMESTAMP THEN
 postgres-#      :NEW.REP_TIME := SYSTIMESTAMP;
 ERROR:  syntax error at or near "BEGIN"
 LINE 4: BEGIN                                                       ...
         ^
 postgres=#    ELSE
 postgres-#      :NEW.REP_TIME := :OLD.REP_TIME + 1 / 86400;
 ERROR:  syntax error at or near "ELSE"
 LINE 1: ELSE                                                        ...
         ^
 postgres=#    END IF;
 ERROR:  syntax error at or near "IF"
 LINE 1: END IF;
             ^
 postgres=# END;
 WARNING:  there is no transaction in progress
 COMMIT
 postgres=# /
 ​
 postgres=# alter table core_product add (
 postgres(#   constraint pk_coreprodsearch
 postgres(#   primary key(prod_id));
 ERROR:  syntax error at or near "constraint"
 LINE 2:   constraint pk_coreprodsearch
 ​
 -- 通过上面测试,可以看到Oracle兼容模式下支持查询dual,而且对TIMESTAMP WITH TIME ZONE也是支持的
 -- 但当前不支持触发器创建
 -- 竟然添加约束也是报错,而且SQL语句换成小写也是报错
 -- 若是换成如下格式SQL可以正常执行
 postgres=# alter table core_product add   constraint pk_coreprodsearch  primary key(prod_id);

image20240109111404546.png

image20240109114459346.png

3.2 PG兼容模式测试

 [ivorysql@ivorysqldb ~]$ psql -p 5433 postgres
 psql (16.0)
 Type "help" for help.
 ​
 postgres=# select * from dual;
 ERROR:  relation "dual" does not exist
 LINE 1: select * from dual;
                       ^
 postgres=# CREATE TABLE CORE_PRODUCT
 postgres-# (
 postgres(#   COM_ID                         NUMBER(10)     NOT NULL,
 postgres(#   PROD_ID                        NUMBER(10)     NOT NULL,
 postgres(#   OTHER_CAT_CODE                 VARCHAR2(255 BYTE),
 postgres(#   PROD_KEYWORD                   VARCHAR2(500 BYTE) NOT NULL,
 postgres(#   PROD_PRICE                     VARCHAR2(50 BYTE) DEFAULT '0',
 postgres(#   PROD_CUSTOM                    VARCHAR2(4000 BYTE),
 postgres(#   PROD_DESCRIPT                  VARCHAR2(4000 BYTE) NOT NULL,
 postgres(#   RECOMMEND                      VARCHAR2(1 BYTE) DEFAULT '0' NOT NULL,
 postgres(#   PHOTO_EXI_FLAG                 VARCHAR2(1 BYTE) DEFAULT '0',
 postgres(#   PREP_TIME                      DATE           DEFAULT TO_DATE('1900-01-01','YYYY-MM-DD HH24:MI:SS'),
 postgres(#   UPDATE_TIME                    DATE           DEFAULT TO_DATE('1900-01-01','YYYY-MM-DD HH24:MI:SS') NOT NULL,
 postgres(#   ADD_TIME                       DATE           DEFAULT TO_DATE('1900-01-01','YYYY-MM-DD HH24:MI:SS') NOT NULL,
 postgres(#   COM_NAME                       VARCHAR2(255 BYTE) DEFAULT ' ',
 postgres(#   REP_TIME                       TIMESTAMP(6) WITH TIME ZONE DEFAULT TO_DATE('1900-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS') NOT NULL,
 postgres(#   LAST_CHECK_TIME                DATE           DEFAULT TO_DATE('1900-01-01','YYYY-MM-DD HH24:MI:SS')
 postgres(# );
 ERROR:  syntax error at or near "BYTE"
 LINE 5:   OTHER_CAT_CODE                 VARCHAR2(255 BYTE),
                                                       ^
 postgres=#
 postgres=# CREATE TABLE PGTB01
 postgres-# (
 postgres(#   COM_ID    NUMBER(10)     NOT NULL,
 postgres(#   OTHER_CAT_CODE           VARCHAR(255 BYTE)
 postgres(# );
 ERROR:  syntax error at or near "BYTE"
 LINE 4:   OTHER_CAT_CODE           VARCHAR(255 BYTE)
                                                ^
 postgres=#
 postgres=# CREATE TABLE PGTB01
 postgres-# (
 postgres(#   COM_ID    NUMBER(10)     NOT NULL,
 postgres(#   REP_TIME                TIMESTAMP(6) WITH TIME ZONE DEFAULT TO_DATE('1900-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS') NOT NULL
 postgres(# );
 ERROR:  type "number" does not exist
 LINE 3:   COM_ID    NUMBER(10)     NOT NULL,
                     ^
 postgres=#
 ​
 postgres=# create table pgtb01
 postgres-# (
 postgres(#   com_id    number(10)  not null,
 postgres(#   rep_time  timestamp(6) with time zone default to_date('1900-01-01 00:00:00','yyyy-mm-dd hh24:mi:ss') not null
 postgres(# );
 ERROR:  type "number" does not exist
 LINE 3:   com_id    number(10)  not null,
 ​
 -- 通过上面测试可以看到在PG模式下,不支持查看dual虚拟表,对于有些Oracle类型也不支持

image20240109112654183.png

四、附录

1、通过对IvorySQL 3.0的测试,特别是同时提供了Oracle和PG两种兼容模式,这个还是比较新颖的,但不知对于后续数据库升级有无影响。

2、文档资料不全,这也是当前很多国产数据库通病,在文档这块还需要进一步加强,比如安装时ICU库和Python版本都没有提到,发现安装时报错。

3、Oracle兼容模式,对于触发器不支持,另外在创建约束的时候有报错,这块也已提交IvorySQL 官网github待修复。

本文是初探IvorySQL 3.0,后面还将继续进行相关的测试与学习。

注:本人现供职于某上市互联网公司担任DBA,荣获 PG ACE称号,拥有 Oracle OCM、AWS等及国产数据库等产品认证。喜欢技术分享,热爱交友,也热爱健身。

如喜欢文章内容,可关注我公众号:

  • 13
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尚雷5580

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值