Linux中 python2 安装cx_Oracle连接Oracle
环境描述:
操作系统:ubuntu 12.04
Python 环境: python2.7
1 安装python-pip python-dev
# Ubuntu
# apt-get install python-pip python-dev
# Centos
# yum -y install epel-release
# yum -y install python-pip python python-devel
2 cx_Oracle安装步骤
1. 确定ORACLE数据库版本,如11.2.0.4
2. 到ORACLE https://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html 官方网上下载对应的软件包
instantclient-basic-linux.x64-11.2.0.4.0.zip
instantclient-sdk-linux.x64-11.2.0.4.0.zip
3. 将包上传到服务器上
4. 解压到当前目录下,拷贝到/usr/local下
# unzip instantclient-basic-linux.x64-11.2.0.4.0.zip # unzip instantclient-sdk-linux.x64-11.2.0.4.0.zip # mv instantclient_11_2 /usr/local/
5. 执行以下命令
# echo "/usr/local/instantclient_11_2" > /etc/ld.so.conf.d/oracle-instantclient.conf # ldconfig
6. 安装依赖包
# Ubuntu
# apt-get install libaio-dev
# Centos
# yum install install libaio-devel
7. 使用pip安装cx_Oracle
# pip install cx_Oracle
【注】部分机器出现以下报错
Downloading/unpacking cx_Oracle Cannot fetch index base URL http://pypi.python.org/simple/ Could not find any downloads that satisfy the requirement cx_Oracle No distributions at all found for cx_Oracle Storing complete log in /root/.pip/pip.log
解决办法
# pip install --index-url=https://pypi.python.org/simple/ cx_Oracle
8. 在python脚本中测试连接Oracle实例,代码如下:
import cx_Oracle
try: conn = cx_Oracle.connect( 'username/password@xxxxx:1521/instance_name') except Exception as e: print("connect oracle error!!") logger.error(e) else: cur = conn.cursor() ......