python cx-Oracle 驱动安装

安装oracle驱动

cx-Oracle驱动二进制版本下载地址
http://download.oracle.com/otn/linux/instantclient/185000/instantclient-basic-linux.x64-18.5.0.0.0dbru.zip
## 下载时需要oracle开发者帐号

说明:本次测试的操作系统为:Ubuntu-16.04,其他操作系统可以在oracle网站上查找并下载对应版本的zip包

mkdir -p /opt/oracle
cd /opt/oracle
unzip instantclient-basic-linux.x64-18.5.0.0.0dbru.zip
mv instantclient_18_5 /opt/oracle/
apt-get install libaio1 -y

配置:

echo /opt/oracle/instantclient_18_5 > /etc/ld.so.conf.d/oracle-instantclient.conf
ldconfig

echo “export LD_LIBRARY_PATH=/opt/oracle/instantclient_18_5:$LD_LIBRARY_PATH” >> /etc/profile.d/ora_env.sh

测试驱动连接

cat test_ora.py

import cx_Oracle
from sqlalchemy import create_engine

ip = 'xxx'
port = '1521'
uname = 'user'
pwd = 'pwd'
tnsname = 'xe'

dsnStr = cx_Oracle.makedsn(ip, port, service_name=tnsname)
connect_str = "oracle://%s:%s@%s" %(uname, pwd, dsnStr)
engine = create_engine(connect_str, encoding='utf-8')

conn = cx_Oracle.connect(uname, pwd, dsn=dsnStr)

if __name__ == '__main__':
    print(engine.url)
    import pandas as pd
    r = pd.read_sql('select * from v$version',con=engine)
    print(r)

驱动测试运行结果

python test_ora.py

oracle://user:pwd@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=IP)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=xe)))
                                               banner
 0  Oracle Database 11g Express Edition Release 11…
 1             PL/SQL Release 11.2.0.2.0 - Production
 2                       CORE\t11.2.0.2.0\tProduction
 3     TNS for Linux: Version 11.2.0.2.0 - Production
 4             NLSRTL Version 11.2.0.2.0 - Production

参考文档

https://cx-oracle.readthedocs.io/en/latest/installation.html#installing-cx-oracle-on-linux

https://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html