在Ubuntu下安裝Oracle Instant Client

來源:互聯網
上載者:User

最近需要寫一個資料移轉指令碼,將單一Oracle中的資料移轉到MySQL Sharding叢集,剛好最近在學習python,就用它來練手。
很快搞定了MySQL,只需要安裝一個MySQLdb的python模組就可以了。但是對於Oracle用戶端,不只需要安裝相應的python模組(這裡我用了Oracle官方的python模組——cx_Oracle),還需要安裝Oracle Client,一般選擇Instant Client就足夠了,還需要配置tnsnames.ora(當然也可以簡單的通過host:port/schema訪問)。

下面是具體步驟。

首先確定版本。因為我們的Oracle資料是在是有點老,所以我選擇了一個比較老的版本——Oracle Instant Client 10.2.0.4。一般從官方網站下載就可以了。:http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html。這裡要嚴重BS Oracle,居然要先註冊才能下載,這也算了,關鍵是註冊的時候,密碼居然要求有數字有字母,字母還要有大小寫,還必須至少8位。逼迫我搞了一個比我銀行密碼還要安全的密碼(好吧,現在我已經忘記我填了什麼了。。)。下載的時候要特別注意,一定要下載rpm包,zip不知道是什麼。下basic就可以了。
forrest@Ubuntu:~/Sources$ wget http://download.oracle.com/otn/linux/instantclient/10204/oracle-instantclient-basic-10.2.0.4-1.x86_64.rpm
由於是rpm包,在ubuntu下先將其轉成deb包:
forrest@ubuntu:~/Sources$ sudo alien oracle-instantclient-basic-10.2.0.4-1.x86_64.rpm
得到oracle-instantclient-basic_10.2.0.4-2_amd64.deb。
可以安裝了,
forrest@ubuntu:~/Sources$ sudo dpkg -i oracle-instantclient-basic_10.2.0.4-2_amd64.deb
這樣會安裝在預設的目錄下——/usr/lib/oracle/10.2.0.4/client64/
forrest@ubuntu:/usr/lib/oracle/10.2.0.4/client64$ ls
bin  lib 


安裝完成之後,還需要暴露一些環境變數,否則會報錯:
 * import cx_Oracle gave ImportError: libclntsh.so.10.1: cannot open shared object file: No such file or directory until I set LD_LIBRARY_PATH=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/lib/
 * conn = cx_Oracle.Connection('scott/tiger@xe') gave RuntimeError: Unable to acquire Oracle environment handle until I set ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server


forrest@ubuntu:~/Sources$ sudo vim ~/.profile
在最後添加如下語句:
export ORACLE_HOME=/usr/lib/oracle/10.2.0.4/client64
export PATH=$PATH:$ORACLE_HOME/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib
export TNS_ADMIN=$ORACLE_HOME/network/admin


注意到TNS_ADMIN所在目錄其實並不存在,是要自己建立的(這個也很噁心,我一開始以為還要安裝什麼東東。。)
$ sudo mkdir -p $ORACLE_HOME/network/admin
$ sudo cp tnsnames.ora $ORACLE_HOME/network/admin

如果你有安裝sqlplus,此時就可以用它來測試安裝是否正確了:
$ sqlplus 'username/password@SID'

不過linux下的sqlplus太爛,我沒打算安裝,所以留著吧,接下去安裝python模組——cx_Oracle——參考Install cx_Oracle in ubuntu(http://leojay.blog.163.com/blog/static/1739841912009101165546640/)
到SourceForge搜尋cx_Oracle,根據我的python版本和要操作的資料庫版本,選擇了cx_Oracle-5.1-10g-py26-1.x86_64.rpm這個版本,只能說我相信高版本是向後相容的。先試一下吧,呵呵。
下完之後解壓,將cx_Oracle.so放在dist-packages下:
$ sudo cp cx_Oracle.so /usr/local/lib/python2.6/dist-packages/
這樣就OK了。
寫個簡單的測試程式測試一下吧:
#!bin/python
import cx_Oracle

conn = cx_Oracle.connect('user/passwd@sid')

cursor = conn.cursor()
cursor.execute("SELECT * from product_detail where product_id = 232896483")
row = cursor.fetchone()
print "result: ", row

cursor.close()
conn.close()

如果沒有問題應該就可以看到結果了。這時候一般會遇到這樣的問題:
forrest@ubuntu:~/work/data-migration$ python oracledb.py
Traceback (most recent call last):
  File "oracledb.py", line 5, in <module>
    conn = cx_Oracle.connect(''user/passwd@sid')
cx_Oracle.DatabaseError: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
查看一下你的/usr/lib/oracle/10.2.0.4/client64/network/admin/tnsnames.ora設定檔,確保sid是配置正確的。
或者直接使用host:port/schema方式:
conn = cx_Oracle.connect('user/passwd@host:port/schema')


如果有其他問題,可以在/usr/lib/oracle/10.2.0.4/client64/network/admin下添加一個sqlnet.ora檔案,以trace方式運行:
If for some reason you have some trouble connecting, you can create a sqlnet.ora file under $ORACLE_HOME with some tracing options.
$ sudo vi $ORACLE_HOME/network/admin/sqlnet.ora
TRACE_DIRECTORY_CLIENT=/tmp
TRACE_LEVEL_CLIENT=SUPPORT


The next time the Oracle Instant Client is used, it will create a detailed log file under /tmp like the following: cli_1968.trc. Make sure to turn this option off when you are done as the logfile can get quite large!

PS:目前看來cx_Oracle還是有向下相容的。希望如此,搞個環境比寫個程式麻煩多了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.