环境背景
系统:CentOS6.8
python:Python 2.7.13 |Anaconda 4.3.1 (64-bit)
cx_Oracle模块:5.0.4
Oracle:11.2.0.1.0
Oracle客户端:11.2.0.1.0
1、下载
http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html
从 下载链接 下载下面两个文件
oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.zip instantclient-sdk-linux.x64-11.2.0.1.0.zip #注这里要根据连接数据库的版本来定,安装的客户端一定要选择基础版本,不要选instantclient11.2-basiclite
解压这两个文件到/opt/instantclient_11_2目录下
2、设置环境变量
# vim /etc/profile #文件尾部添加 export ORACLE_HOME=/opt/instantclient_11_2/ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME # source /etc/profile
3、安装cx_Oracle
安装cx_Oracle之前需要先建立一个链接libclntsh.so,如下:
cd /opt/instantclient_11_2/ ln -s libclntsh.so.11.1 libclntsh.so
下载cx_Oracle
https://sourceforge.net/projects/cx-oracle/files/5.0.4/
# tar -xf cx_Oracle-5.0.4.tar.gz # cd cx_Oracle-5.0.4 # python setup.py install
测试:
>>> import cx_Oracle /root/.pyenv/versions/anaconda2-4.3.1/lib/python2.7/site-packages/cx_Oracle-5.0.4-py2.7-linux-x86_64.egg/cx_Oracle.py:3: UserWarning: Module cx_Oracle was already imported from /root/.pyenv/versions/anaconda2-4.3.1/lib/python2.7/site-packages/cx_Oracle-5.0.4-py2.7-linux-x86_64.egg/cx_Oracle.pyc, but /opt/cx_Oracle-5.0.4 is being added to sys.path >>> >>> print cx_Oracle.version 5.0.4
4、测试数据库连接
>>> import cx_Oracle as cx >>> db=cx.connect(‘finchina/[email protected]/orcl‘) >>> print db.version 11.2.0.1.0 >>> db.close()
FAQ整理:
问题1: conn0=cx.connect(‘finchina/[email protected]:1521/orcl‘)
cx_Oracle.InterfaceError: Unable to acquire Oracle environment handle
解决方案:这里要保证你安装的instantclient和需要连接的数据库版本一致。下载
oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.zip instantclient-sdk-linux.x64-11.2.0.1.0.zip windows出现这个问题 复制 client下所有的.dll文件到python安装包site_pageages目录中即可
然后按照上文进行配置就即可。
问题2: conn0=cx.connect(‘finchina/[email protected]:1521/orcl‘)
cx_Oracle.DatabaseError: ORA-28547: connection to server failed, probable Oracle Net admin error
解决方案:
重新安装 cx_Oracle5.0.4
重装安装 instantclient11.2-basic
下面选段来自Oracle客户端下载地址:http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html
Instant Client Package - Basic: All files required to run OCI, OCCI, and JDBC-OCI applications #注释:即时客户端软件包 - 基本:运行OCI,OCCI和JDBC-OCI应用程序所需的所有文件 oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.zip (48,338,185 bytes) (cksum - 15985569) oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.rpm (47,811,007 bytes) (cksum - 4244105838) Instant Client Package - Basic Light: Smaller version of the Basic, with only English error messages and Unicode, ASCII, and Western European character set support #注释:即时客户端软件包 - 基本轻量:较小版本的Basic,只有英文错误消息和Unicode,ASCII和西欧字符集支持,阿西吧!,原来是字符集问题 oracle-instantclient11.2-basiclite-11.2.0.1.0-1.x86_64.zip (20,825,489 bytes) (cksum - 133027975) oracle-instantclient11.2-basiclite-11.2.0.1.0-1.x86_64.rpm (20,649,392 bytes) (cksum - 1121446971)
总结:
问题很严重,遇到问题,胡乱百度,无思路的Google,反而拖慢解决问题的节奏。希望这次之后,遇到任何问题都能:理清思路,将问题拆分,然后逐个击破
问题3:ImportError: libclntsh.so.11.1: cannot open shared object file: No such file or directory
解决方案:
cd /opt/instantclient_11_2 ln -sv libclntsh.so.11.1 libclntsh.so