Ubuntu 下安装ox_oracle

Introduction

Oracle Instant Client is a free Oracle database client. The current version is 11.2.0.1.0, and several versions back to 10.1.0.5 are available.

 Install RPMs

  • Download the Oracle Instantclient RPM files fromhttp://www.oracle.com/technetwork/database/features/instant-client/index-097480.html. Everyone needs either "Basic" or "Basic lite", and most users will want "SQL*Plus" and the "SDK".

  • Convert these .rpm files into .deb packages and install using "alien" ("sudo apt-get install alien" if you don't have it):

    转为位deb包,然后直接安装
    alien -i oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
    alien -i oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm
    alien -i oracle-instantclient11.2-sqlplus-11.2.0.3.0-1.x86_64.rpm
    由于是rpm包,在ubuntu上转换位deb包: sudo alien oracle-instantclient-basic-11.2.x86_64.rpm
    得到deb包,在ubuntu上安装命令为:sudo dpkg -i oracle-instantclient-basic_11.2_amd64.deb
  • Test your Instantclient install by using "sqlplus" to connect to your database:
sqlplus  username/password@//dbhost:1521/SID

If sqlplus complains of a missing libaio.so.1 file, run 

sudo apt-get install libaio1

If sqlplus complains of a missing libsqlplus.so file, follow the steps in the section "Integrate Oracle Libraries" below.

If you execute sqlplus and get "sqlplus: command not found", see the section below about adding the ORACLE_HOME variable. 

 

Integrate Oracle Libraries| ORACLE_HOME

If oracle applications, such as sqlplus, are complaining about missing libraries, you can add the Oracle libraries to the LD_LIBRARY_PATH each time it is used, or to add it to the system library list create a new file as follows:

sudo vim /etc/profile

增加以下环境变量:

       export ORACLE_HOME=/usr/lib/oracle/11.2/client64

  export PATH=$PATH:$ORACLE_HOME/bin

  export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib

  export TNS_ADMIN=$ORACLE_HOME/network/admin

source /etc/profile 文件 更新环境变量

如果需要配置tnsnames.ora还需要这样操作:

sudo mkdir -p $ORACLE_HOME/network/admin

sudo cp tnsnames.ora $ORACLE_HOME/network/admin 

 Download and build cx_Oracle

Download cx_Oracle at http://cx-oracle.sourceforge.net and unzip wherever you want, then...

选择源码安装:解压下载回来的tar.gz包,tar -zxvf cx_oracle.tar.gz
cd [your cx_Oracle installation path]
python setup.py build
python setup.py install

  ImportError: libclntsh.so.11.1: cannot open shared object file: No such file or directory

  此时是由于python在操作oracle数据库的时候需要用到oracle的一些库,而上面的问题就是说python需要的这些库不在环境的路径里,在linux上就是不在LD_LIBRARY_PATH环境变量里,此时时就需要把这些库路径加到LD_LIBRARY_PATH中

$ export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/lib/oracle/11.2/client/lib  
也可以选择rpm安装,解压出来里面有cx_Oracle.so ,执行命令:sudo cp cx_Oracle.so /usr/local/lib/python2.7/dist-packages/

Make a quick check:

验证安装成功:
luca@ubuntu:~$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> print cx_Oracle.version
5.1.2
另外参考:

 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

  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还是有向下兼容的。希望如此,搞个环境比写个程序麻烦多了。

posted @ 2012-08-06 13:25  苍术厚朴  阅读(2079)  评论(0编辑  收藏  举报