pythoncx_Oracle模块的安装和使⽤详细介绍
python cx_Oracle模块的安装
最近需要写⼀个数据迁移脚本,将单⼀Oracle中的数据迁移到MySQL Sharding集,在linux下安装cx_Oracle感觉还是有⼀点⿇烦的,整理⼀下,做个总结。
对于Oracle客户端,不只需要安装相应的python模块(这⾥我⽤了Oracle官⽅的python模块——cx_Oracle),还需要安装Oracle Client,⼀般选择Instant Client就⾜够了,还需要配置a(当然也可以简单的通过host:port/schema访问)。
安装:
1. ⾸先确定版本。因为我们的Oracle数据是在是有点⽼,所以我选择了⼀个⽐较⽼的版本——Oracle Instant Client 10.
2.0.4。
$wget acle/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.配置a(可不⽤配置tns)
注意a其实并不存在,是要⾃⼰创建的(这个也很恶⼼,我⼀开始以为还要安装什么东东。。),我没有使⽤这种⽅式,有兴趣的可以google⼀下。
5.下载安装cx_Oracle python模块
$wget downloads.sourceforge/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=ucs4linux安装数据库oracle
$make;make install
再次验证,终于正常import了。
使⽤:
1.基本连接–使⽤Oracle tns alias
connection =t("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/……/a
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 =pass()
connection =t("tp",pwd,"ocn_test")
3.⽤户直接在Python命令中输⼊连接账号信息,格式如python script.py tp/tp@ocn_test
connection =t(sys.argv[1])
4.使⽤Easy Connect语法,通过Drive连接数据库
connection =t('tp','tp','10.20.36.19:1521/ocntest')
#or
connection =t('tp/tp@10.20.36.19:1521/ocntest')
5.先使⽤DSN构成TNSNAME
tns_name =cx_Oracle.makedsn('10.20.36.19','1521',' ocntest ')
connection =t('tp','tp',tns_name)
6.登陆as SYSDBA
connection =t('tp/tp@ocn_test', mode=cx_Oracle.SYSDBA)
#or as SYSOPER
connection =t('tp/tp@ocn_test', mode=cx_Oracle.SYSOPER)
在Linux服务器执⾏Oracle操作时报了⼀个错误:
TNS:listener does not currently know of service requested in connect descriptor
解决⽅式:
感谢阅读,希望能帮助到⼤家,谢谢⼤家对本站的⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论