The installation and use of the Python cx_oracle module is described in detail

Source: Internet
Author: User
Installation of the Python cx_oracle module

Recently need to write a data migration script, the single Oracle data migration to the MySQL sharding cluster, installed under Linux cx_oracle feel still a little trouble, tidy up, to do a summary.

For Oracle clients, it is not just necessary to install the appropriate Python module (I use Oracle's official Python module--cx_oracle), but also to install the Oracle client, which is generally sufficient to select the instant client. You also need to configure Tnsnames.ora (and, of course, simply access via Host:port/schema).

Installation:

1. First determine the version. Because our Oracle data is in a bit old, so I chose an older version of--oracle Instant Client 10.2.0.4.

2. Download Instantclient-basic. Download Address: http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html. Here to serious BS Oracle, actually have to register to download, this also forget, the key is to register, the password actually requires that there is a number of letters, letters should also have the case, and must be at least 8 bits. Forcing me to make a password that's safer than my bank password (OK, now I've forgotten what I've filled ...), and Basic is OK.

$wget Http://download.oracle.com/otn/linux/instantclient/10204/basic-10.2.0.4.0-linux-x86_64.zip

3. Installation Configuration

$unzip INSTANTCLIENT-BASIC-LINUX.X64-10.2.0.4.0.ZIP$CD INSTANTCLIENT_10_2$CP */usr/lib  #直接放到动态库搜索路径中, No additional environment configuration required or $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. Configuring Tnsnames.ora (without the need to configure TNS)

Note that Tnsnames.ora actually does not exist, is to create their own (this is also very disgusting, I initially thought to install what stuff. I did not use this way, interested can Google a bit.

5. Download and install the Cx_oracle python module

$wget HTTP://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 #有这个文件表示安装成功, based on the location of Python, Maybe somewhere else, find out for yourself.

6. Verification and problem solving

$python >>import cx_oracle

If error: Import Cx_oracle gave Importerror:libclntsh.so.10.1:cannot open shared object file:no such file or directory

Indicates that no dynamic library was found for the instant client, check that the environment variable is configured, whether it is valid, and whether the version is correct.

If error: Importerror:./cx_oracle.so:undefined symbol:pyunicodeucs4_decode

Google Info: There is nothing wrong with Debian. Python supports-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

So recompile python

$./configure--prefix=/usr/local/python2.6.5--enable-shared-enable-unicode=ucs4$make;make Install

Verify again, and finally the normal import.

Use:

1. Basic connectivity – using Oracle TNS Alias

Connection =cx_oracle.connect ("Tp/tp@ocn_test") #查看tns alias command cmd>tnsping ocn_testtns Ping Utility forlinux: Version 9.2.0.8.0-production on 27-sep-201110:47:48copyright (c) 1997, 2006, Oracle Corporation. Allrights reserved. Used parameter files:/opt/....../sqlnet.oraused TNSNames adapter to resolve the aliasattempting to contact (DESCRIPTION = (ADDR Ess_list = (ADDRESS = (PROTOCOL =tcp) (HOST =10.20.36.19) (PORT =1520))) (Connect_data = (SID =ocntest)) Ok (10msec)

2. User Input Password connection

PWD =getpass.getpass () connection =cx_oracle.connect ("TP", pwd, "ocn_test")

3. The user enters the connection account information directly in the Python command, formatted as Python script.py tp/tp@ocn_test

Connection =cx_oracle.connect (Sys.argv[1])

4. Connect to the database via drive using the Easy Connect syntax

Connection =cx_oracle.connect (' TP ', ' TP ', ' 10.20.36.19:1521/ocntest ') #orconnection =cx_oracle.connect (' tp/ Tp@10.20.36.19:1521/ocntest ')

5. First Use DSN to make Tnsname

Tns_name =cx_oracle.makedsn (' 10.20.36.19 ', ' 1521 ', ' ocntest ') connection =cx_oracle.connect (' TP ', ' TP ', tns_name)

6. Log in as SYSDBA

Connection =cx_oracle.connect (' Tp/tp@ocn_test ', MODE=CX_ORACLE.SYSDBA) #or as Sysoperconnection =cx_oracle.connect (' Tp/tp@ocn_test ', Mode=cx_oracle.sysoper)

An error occurred on the Linux server performing the Oracle Operations times:

Tns:listener does not currently know of service requested in Connect descriptor

How to resolve:

Analysis of the problem see http://ora-12514.ora-code.com/, a toss, and finally use the 5th type of connection, an instant solution to this problem.

Thank you for reading, hope to help everyone, thank you for the support of this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.