python cx_Oracle模塊的安裝和使用詳細(xì)介紹
python cx_Oracle模塊的安裝
最近需要寫一個數(shù)據(jù)遷移腳本,將單一Oracle中的數(shù)據(jù)遷移到MySQL Sharding集群,在linux下安裝cx_Oracle感覺還是有一點麻煩的,整理一下,做個總結(jié)。
對于Oracle客戶端,不只需要安裝相應(yīng)的python模塊(這里我用了Oracle官方的python模塊——cx_Oracle),還需要安裝Oracle Client,一般選擇Instant Client就足夠了,還需要配置tnsnames.ora(當(dāng)然也可以簡單的通過host:port/schema訪問)。
安裝:
1. 首先確定版本。因為我們的Oracle數(shù)據(jù)是在是有點老,所以我選擇了一個比較老的版本——Oracle Instant Client 10.2.0.4。
2. 下載instantclient-basic。下載地址:http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html。這里要嚴(yán)重BS Oracle,居然要先注冊才能下載,這也算了,關(guān)鍵是注冊的時候,密碼居然要求有數(shù)字有字母,字母還要有大小寫,還必須至少8位。逼迫我搞了一個比我銀行密碼還要安全的密碼(好吧,現(xiàn)在我已經(jīng)忘記我填了什么了...),下basic就可以了。
$wget http://download.oracle.com/otn/linux/instantclient/10204/basic-10.2.0.4.0-linux-x86_64.zip
3.安裝配置
$unzip instantclient-basic-linux.x64-10.2.0.4.0.zip $cd instantclient_10_2 $cp * /usr/lib #直接放到動態(tài)庫搜索路徑中,不需要額外的環(huán)境配置 或 $unzip instantclient-basic-linux.x64-10.2.0.4.0.zip $cp -rf instantclient_10_2 /opt/ $vi /etc/profile export ORACLE_HOME=/opt/instantclient_10_2 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME $source /etc/profile
4.配置tnsnames.ora(可不用配置tns)
注意tnsnames.ora其實并不存在,是要自己創(chuàng)建的(這個也很惡心,我一開始以為還要安裝什么東東。。),我沒有使用這種方式,有興趣的可以google一下。
5.下載安裝cx_Oracle python模塊
$wget http://downloads.sourceforge.net/project/cx-oracle/5.1.2/cx_Oracle-5.1.2-10g-py26-1.x86_64.rpm $rpm -ivh cx_Oracle-5.1.2-10g-py26-1.x86_64.rpm $ls /usr/lib/python2.6/site-packages/cx_Oracle.so #有這個文件表示安裝成功,根據(jù)python的位置,也可能在其他地方,自己找一下吧
6.驗證及問題解決
$python >>import cx_Oracle
若報錯:import cx_Oracle gave ImportError: libclntsh.so.10.1: cannot open shared object file: No such file or directory
表示沒有找到instant client的動態(tài)庫,check一下環(huán)境變量是否配置,是否生效,版本是否正確。
若報錯:ImportError: ./cx_Oracle.so: undefined symbol: PyUnicodeUCS4_Decode
Google的信息:There is nothing wrong with Debian. Python supports two incompatible modes of operation for Unicode, UCS2 (the default), and UCS4. Debian uses the default, Redhat uses UCS4. You need to recompile the extension for UCS-2 mode (i.e. using a Debian installation); this would fix the undefined symbol: PyUnicodeUCS4_Decode
所以重新編譯python
$./configure --prefix=/usr/local/python2.6.5 --enable-shared -enable-unicode=ucs4 $make;make install
再次驗證,終于正常import了。
使用:
1.基本連接–使用Oracle tns alias
connection =cx_Oracle.connect("tp/tp@ocn_test") #查看tns alias命令 cmd>tnsping ocn_test TNS Ping Utility forLinux: Version 9.2.0.8.0-Production on 27-SEP-201110:47:48 Copyright (c) 1997, 2006, Oracle Corporation. Allrights reserved. Used parameter files: /opt/……/sqlnet.ora Used TNSNAMES adapter to resolve the alias Attempting to contact (DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL =TCP)(HOST =10.20.36.19)(PORT =1520))) (CONNECT_DATA =(SID =ocntest))) OK (10msec)
2.用戶輸入密碼連接
pwd =getpass.getpass() connection =cx_Oracle.connect("tp",pwd,"ocn_test")
3.用戶直接在Python命令中輸入連接賬號信息,格式如python script.py tp/tp@ocn_test
connection =cx_Oracle.connect(sys.argv[1])
4.使用Easy Connect語法,通過Drive連接數(shù)據(jù)庫
connection =cx_Oracle.connect('tp','tp','10.20.36.19:1521/ocntest') #or connection =cx_Oracle.connect('tp/tp@10.20.36.19:1521/ocntest')
5.先使用DSN構(gòu)成TNSNAME
tns_name =cx_Oracle.makedsn('10.20.36.19','1521',' ocntest ') connection =cx_Oracle.connect('tp','tp',tns_name)
6.登陸as SYSDBA
connection =cx_Oracle.connect('tp/tp@ocn_test', mode=cx_Oracle.SYSDBA) #or as SYSOPER connection =cx_Oracle.connect('tp/tp@ocn_test', mode=cx_Oracle.SYSOPER)
在Linux服務(wù)器執(zhí)行Oracle操作時報了一個錯誤:
TNS:listener does not currently know of service requested in connect descriptor
解決方式:
問題分析見http://ora-12514.ora-code.com/,一番折騰,最后使用第5種連接方式,瞬間解決此問題。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
使用Python代碼實現(xiàn)Linux中的ls遍歷目錄命令的實例代碼
這次我就要試著用 Python 來實現(xiàn)一下 Linux 中的 ls 命令, 小小地證明下 Python 的不簡單,需要的朋友可以參考下2019-09-09使用python將微信image下.dat文件解密為.png的方法
這篇文章主要介紹了使用python將微信image下.dat文件解密為.png的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11Python Dataframe 指定多列去重、求差集的方法
今天小編就為大家分享一篇Python Dataframe 指定多列去重、求差集的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Python著名游戲?qū)崙?zhàn)之方塊連接 我的世界
讀萬卷書不如行萬里路,學(xué)的扎不扎實要通過實戰(zhàn)才能看出來,本篇文章手把手帶你模仿著名游戲——我的世界,大家可以在過程中查缺補漏,看看自己掌握程度怎么樣2021-10-10Python數(shù)據(jù)分析:pandas中Dataframe的groupby與索引用法
這篇文章主要介紹了pandas中Dataframe的groupby與索引用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02