標籤:oracle cx_oracle instantclient11.2 oracle用戶端 python
Linux安裝cx_Oracles
環境背景
系統: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.version5.0.4
4、測試資料庫連接
>>> import cx_Oracle as cx>>> db=cx.connect(‘finchina/[email protected]/orcl‘)>>> print db.version11.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.zipwindows出現這個問題複製 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_2ln -sv libclntsh.so.11.1 libclntsh.so
本文出自 “Ljohn” 部落格,謝絕轉載!
Linux安裝cx_Oracles