问题:报错DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory
解决:
1、连接方法
sqlalchemy连接oracle需要使用到cx_Oracle模块
pip install cx-Oracle==8.3.0
调用方法
create_engine("oracle+cx_oracle://{user}:{password}@{host}:{port}".format(**db_config))
2、报错原因
因为连接oracle需要在sqlalchemy所在的服务器安装Oracle Instant Client
方法:
1、到Oracle官网下载客户端
https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html
2、解压文件
mkdir -p /home/test/oracle
unzip instantclient-basic-linux.x64-11.2.0.4.0.zip
3、配置环境变量
vim /home/test/.bash_profile
export LD_LIBRARY_PATH=/home/test/oracle/instantclient_11_2:$LD_LIBRARY_PATH
source /home/test/.bash_profile
拓展:
下载了上面的客户端,也source运行了,但还是报DPI-1047错,如何解决?
根据官方网站的提示,进入调试模式进行排查
export DPI_DEBUG_LEVEL=64
然后进入python
.venv/bin/python
import cx_Oracle
cx_Oracle.init_oracle_client()
此时已经可以看到调试信息了,可以看到
ODPI [3276089] 2024-10-12 14:01:42.789: load with name libclntsh.so.12.1
ODPI [3276089] 2024-10-12 14:01:42.789: load by OS failure: libclntsh.so.12.1: cannot open shared object file: No such file or directory
ODPI [3276089] 2024-10-12 14:01:42.789: load with name libclntsh.so.11.1
ODPI [3276089] 2024-10-12 14:01:42.790: load by OS failure: libnsl.so.1: cannot open shared object file: No such file or directory
加载libclntsh.so.11.1,但是报错libnsl.so.1没找到,说明缺少这个软件,直接安装
sudo yum install libnsl
参考:
https://docs.sqlalchemy.org/en/20/dialects/oracle.html#module-sqlalchemy.dialects.oracle.cx_oracle
https://zhuanlan.zhihu.com/p/531165078