install cx_Oracle for Linux

1、安裝oracle client

1.1 下載客戶端軟件包

使用cx_Oracle必需要安裝Oracle_client端html

官網下載地址:http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.htmlpython

(我下載的版本:oracle11.2.0.4)linux

下載並安裝如下三個包:sql

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

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

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.rpmspa

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

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

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

 

2、安裝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()

相關文章
相關標籤/搜索