install cx_Oracle for Linux

一、安装oracle client

1.1 下载客户端软件包

使用cx_Oracle必须要安装Oracle_client端

官网下载地址:http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html

(我下载的版本:oracle11.2.0.4)

下载并安装以下三个包:

oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm

oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm

oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm

1.2 安装

[root@maomao opt]# rpm -ivh oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm

[root@maomao opt]# rpm -ivh oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm

[root@maomao opt]# rpm -ivh oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm

1.3 配置环境变量

这三个安装包需要用root用户安装,会写在/usr/lib/oracle/11.2/client64下,然后修改一下环境变量:

vi .bash_profile

PATH=$PATH:$HOME/bin

LD_LIBRARY_PATH=.:$ORACLE_HOME:/lib:/usr/lib

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

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

export ORACLE_BASE=/usr/lib/oracle/11.2

export LD_LIBRARY_PATH

export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK

PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin

export PATH

[root@maomao ~]# source .bash_profile(使配置文件生效)

1.3 更新系统库文件(非常重要)

[root@maomao opt]# echo /usr/lib/oracle/11.2/client64/lib/ >> /etc/ld.so.conf

[root@maomao opt]# ldconfig  (立刻刷新cache)

如果不进行ldconfig配置,在运行cx_Oracle时会报以下错误:

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

1.4 sqlplus测试连接

[root@maomao opt]# sqlplus usrname/passwd@ipaddress/instance_name

SQL*Plus: Release 11.2.0.4.0 Production on Tue Jan 9 23:14:29 2018

Copyright (c) 1982, 2013, Oracle.  All rights reserved.

Connected to:

Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL>

下面是sqlplus登录时出现的报错的解决方法:

在ORACLE_HOME/lib下新建一个连接符:libclntsh.so -> libclntsh.so.11.1

[root@maomao lib]# ln –s libclntsh.so.11.1 libclntsh.so

二、安装cx_Oracle

官网下载地址:https://pypi.python.org/pypi/cx_Oracle/5.3

2.1 安装

[root@maomao opt]# tar -xvzf cx_Oracle-6.1.tar.gz

[root@maomao opt]# cd cx_Oracle-6.1/

[root@maomao cx_Oracle-6.1]# python setup.py install

2.2 安装成功后进行测试

[root@maomao ~]# python

Python 2.7.2 (default, Aug 21 2013, 12:12:55) [GCC 4.4.4 20100726 (Red Hat 4.4.4-13)] on linux2Type "help", "copyright", "credits" or "license" for more information.

>>>import cx_Oracle

>>> dsn=cx_Oracle.makedsn('ip',1521,'instance_name')

>>> db=cx_Oracle.connect('username','passwd',dsn)

>>> db.close()

猜你喜欢

转载自my.oschina.net/u/3478888/blog/1604678