登录  /  注册

安装cx_Oracle模块的步骤详解

Y2J
发布: 2017-05-10 11:52:49
原创
3410人浏览过

这篇文章主要介绍了python cx_oracle模块的安装和使用详细介绍的相关资料,需要的朋友可以参考下

python cx_Oracle模块的安装

最近需要写一个数据迁移脚本,将单一Oracle中的数据迁移到MySQL Sharding集群,在linux下安装cx_Oracle感觉还是有一点麻烦的,整理一下,做个总结。

对于Oracle客户端,不只需要安装相应的python模块(这里我用了Oracle官方的python模块——cx_Oracle),还需要安装Oracle Client,一般选择Instant Client就足够了,还需要配置tnsnames.ora(当然也可以简单的通过host:port/schema访问)。

安装:

1. 首先确定版本。因为我们的Oracle数据是在是有点老,所以我选择了一个比较老的版本——Oracle Instant Client 10.2.0.4。

2. 下载instantclient-basic。下载地址:http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html。这里要严重BS Oracle,居然要先注册才能下载,这也算了,关键是注册的时候,密码居然要求有数字有字母,字母还要有大小写,还必须至少8位。逼迫我搞了一个比我银行密码还要安全的密码(好吧,现在我已经忘记我填了什么了...),下basic就可以了。

$wget download.oracle.com/otn/linux/instantclient/10204/basic-10.2.0.4.0-linux-x86_64.zip
登录后复制

3.安装配置

$unzip instantclient-basic-linux.x64-10.2.0.4.0.zip
$cd instantclient_10_2
$cp * /usr/lib  #直接放到动态库搜索路径中,不需要额外的环境配置

或
$unzip instantclient-basic-linux.x64-10.2.0.4.0.zip
$cp -rf instantclient_10_2 /opt/
$vi /etc/profile
   export ORACLE_HOME=/opt/instantclient_10_2
   export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME

$source /etc/profile
登录后复制

4.配置tnsnames.ora(可不用配置tns)

注意tnsnames.ora其实并不存在,是要自己创建的(这个也很恶心,我一开始以为还要安装什么东东。。),我没有使用这种方式,有兴趣的可以google一下。

5.下载安装cx_Oracle python模块

$wget downloads.sourceforge.net/project/cx-oracle/5.1.2/cx_Oracle-5.1.2-10g-py26-1.x86_64.rpm
$rpm -ivh cx_Oracle-5.1.2-10g-py26-1.x86_64.rpm 
$ls /usr/lib/python2.6/site-packages/cx_Oracle.so #有这个文件表示安装成功,根据python的位置,也可能在其他地方,自己找一下吧
登录后复制

6.验证及问题解决

$python
>>import cx_Oracle
登录后复制

若报错:import cx_Oracle gave ImportError: libclntsh.so.10.1: cannot open shared object file: No such file or directory

表示没有找到instant client的动态库,check一下环境变量是否配置,是否生效,版本是否正确。

若报错:ImportError: ./cx_Oracle.so: undefined symbol: PyUnicodeUCS4_Decode

Google的信息:There is nothing wrong with Debian. Python supports two incompatible 
 modes of operation for Unicode, UCS2 (the default), and UCS4. Debian uses the default,
 Redhat uses UCS4. You need to recompile the extension for UCS-2 mode
 (i.e. using a Debian installation); this would fix the undefined symbol: PyUnicodeUCS4_Decode
登录后复制

所以重新编译python

$./configure --prefix=/usr/local/python2.6.5 --enable-shared -enable-unicode=ucs4
$make;make install
登录后复制

再次验证,终于正常import了。

使用:

1.基本连接–使用Oracle tns alias

connection =cx_Oracle.connect("tp/tp@ocn_test")
#查看tns alias命令
cmd>tnsping ocn_test
TNS Ping Utility forLinux: Version 9.2.0.8.0-Production on 27-SEP-201110:47:48
Copyright (c) 1997, 2006, Oracle Corporation. Allrights reserved.
Used parameter files:
/opt/……/sqlnet.ora
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL =TCP)(HOST =10.20.36.19)(PORT =1520))) (CONNECT_DATA =(SID =ocntest)))
OK (10msec)
登录后复制

2.用户输入密码连接

pwd =getpass.getpass()
connection =cx_Oracle.connect("tp",pwd,"ocn_test")
登录后复制

3.用户直接在Python命令中输入连接账号信息,格式如python script.py tp/tp@ocn_test

connection =cx_Oracle.connect(sys.argv[1])
登录后复制

4.使用Easy Connect语法,通过Drive连接数据库

connection =cx_Oracle.connect('tp','tp','10.20.36.19:1521/ocntest')
#or
connection =cx_Oracle.connect('tp/tp@10.20.36.19:1521/ocntest')
登录后复制

5.先使用DSN构成TNSNAME

tns_name =cx_Oracle.makedsn('10.20.36.19','1521',' ocntest ')
connection =cx_Oracle.connect('tp','tp',tns_name)
登录后复制

6.登陆as SYSDBA

connection =cx_Oracle.connect('tp/tp@ocn_test', mode=cx_Oracle.SYSDBA)
#or as SYSOPER
connection =cx_Oracle.connect('tp/tp@ocn_test', mode=cx_Oracle.SYSOPER)
登录后复制

在Linux服务器执行Oracle操作时报了一个错误:

TNS:listener does not currently know of service requested in connect descriptor
登录后复制

【相关推荐】

1. Python免费视频教程

2. Python面向对象视频教程

3. Python基础入门手册

以上就是安装cx_Oracle模块的步骤详解的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号