Home > Backend Development > Python Tutorial > Detailed explanation of the steps to install the cx_Oracle module

Detailed explanation of the steps to install the cx_Oracle module

Y2J
Release: 2017-05-10 11:52:49
Original
3617 people have browsed it

This article mainly introduces the relevant information about the installation and use of the python cx_Oracle module in detail. Friends in need can refer to the following

Installation of the python cx_Oracle module

Recently I need to write a data migration script to migrate the data in a single Oracle to the MySQL Sharding cluster. It is still a bit troublesome to install cx_Oracle under linux. Let’s sort it out and make a Summarize.

For the Oracle client, you not only need to install the corresponding python module (here I used Oracle's official python module - cx_Oracle), but also need to install the Oracle Client. Generally, it is enough to choose Instant Client, and you also need to configure it. tnsnames.ora (of course it can also be accessed simply through host:port/schema).

Installation:

1. First determine the version. Because our Oracle data is a bit old, I chose an older version-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. This is a serious BS against Oracle. You have to register before you can download it. Forget it. The key is that when registering, the password requires numbers and letters, and the letters must be in upper and lower case, and must be at least 8 characters. They forced me to get a password that was safer than my bank password (well, now I have forgotten what I filled in...), and just download basic.

$wget download.oracle.com/otn/linux/instantclient/10204/basic-10.2.0.4.0-linux-x86_64.zip
Copy after login

3. Installation and configuration

$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
Copy after login

4. Configure tnsnames.ora (tns does not need to be configured)

Note that tnsnames.ora does not actually exist, you have to create it yourself (This is also disgusting. At first I thought I needed to install something...). I have not used this method. If you are interested, you can google it.

5. Download and install the cx_Oracle python module

$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的位置,也可能在其他地方,自己找一下吧
Copy after login

6. Verification and problem solving

$python
>>import cx_Oracle
Copy after login

If an error is reported: import cx_Oracle gave ImportError: libclntsh.so.10.1: cannot open shared object file: No such file or directory

means that the dynamic library of the instant client is not found. Check whether the environment variable is configured, whether it is effective, and whether the version is correct.

If an error is reported: 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
Copy after login

So recompile python

$./configure --prefix=/usr/local/python2.6.5 --enable-shared -enable-unicode=ucs4
$make;make install
Copy after login

and verify again, finally the import is normal.

Usage:

1.Basic connection – use 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)
Copy after login

2.User enters password to connect

pwd =getpass.getpass()
connection =cx_Oracle.connect("tp",pwd,"ocn_test")
Copy after login

3.User directly Enter the connection account information in the Python command, in the format such as python script.py tp/tp@ocn_test

connection =cx_Oracle.connect(sys.argv[1])
Copy after login

4. Use Easy Connect syntax to connect to the database through 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')
Copy after login

5. First use DSN to form TNSNAME

tns_name =cx_Oracle.makedsn('10.20.36.19','1521',' ocntest ')
connection =cx_Oracle.connect('tp','tp',tns_name)
Copy after login

6. Log in 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)
Copy after login

An error occurred when performing Oracle operations on the Linux server:

TNS:listener does not currently know of service requested in connect descriptor
Copy after login

[Related recommendations]

1. Python free video tutorial

2. Python object-oriented video tutorial

3. Python Basics Getting Started Manual

The above is the detailed content of Detailed explanation of the steps to install the cx_Oracle module. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template