注意:如下操作均在 MaxKB 容器中进行。
1. 安装cx_Oracle包
1.1 更换 apt 源
修改 /etc/apt/sources.list 。
deb http://mirrors.aliyun.com/debian/ bookworm main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ bookworm main non-free contrib
deb http://mirrors.aliyun.com/debian-security/ bookworm-security main
deb-src http://mirrors.aliyun.com/debian-security/ bookworm-security main
deb http://mirrors.aliyun.com/debian/ bookworm-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ bookworm-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ bookworm-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ bookworm-backports main non-free contrib
1.2 修改 /tmp 目录权限
过程中会产生临时文件,需开放权限。
chmod -R 777 /tmp
1.3 安装依赖
apt update
apt install libaio1
apt install build-essential
1.4 安装 cx_Oracle
pip install cx_Oracle -i https://mirrors.aliyun.com/pypi/simple/
2. 配置 Oracle Instant Client
2.1 下载 basic 和 sdk
主要涉及到 basic 和 sdk 两个包,注意需要根据 Oracle 的版本选择对应版本,下载地址 Oracle官网。
2.2 解压缩并拷贝至 MaxKB 容器中
mkdir /tmp/oracle
unzip instantclient-basic-linux.x64-11.2.0.4.0.zip -d /tmp/oracle
unzip instantclient-sdk-linux.x64-11.2.0.4.0.zip -d /tmp/oracle
docker cp oracle maxkb:/opt
2.3 配置环境变量
export ORACLE_HOME=/opt/oracle/instantclient_11_2
export LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH
export PATH=$ORACLE_HOME:$PATH
source ~/.bashrc
2.4 建立软链接
ln -s /opt/oracle/instantclient_11_2/libclntsh.so.11.1 /usr/lib/libclntsh.so
ln -s /opt/oracle/instantclient_11_2/libnnz11.so /usr/lib/libnnz11.so
3. 测试连接
import cx_Oracle
username = "system"
password = "xxxxxxxx"
dsn = "xx.xx.xx.xx:1521/xe"
connection = cx_Oracle.connect(user=username, password=password, dsn=dsn, encoding="UTF-8")
此外,还可以通过 Oracle REST Data Services (ORDS) 实现 MaxkB 的 HTTP/HTTPS 对接。Oracle 数据库支持启用 HTTPS 功能,确保数据传输安全(参考官方文档:Oracle REST Data Services)。