The installation and use of the python cx_Oracle module are described in detail

  • 2020-05-26 09:29:28
  • OfStack

Installation of the python cx_Oracle module

Recently, I need to write a data migration script to migrate the data from single 1Oracle to MySQL Sharding cluster. It feels like there is still a bit of trouble to install cx_Oracle under linux. Please tidy up 1 and make a summary.

For the Oracle client, you not only need to install the python module (I used Oracle's official python module -- cx_Oracle), you also need to install Oracle Client, and you need to configure tnsnames.ora (you can also easily access host:port/schema).

Installation:

1. Determine the version first. Since 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. Here is serious BS Oracle, you have to register before you can download, which also forget, the key is that when you register, the password is required to have Numbers and letters, letters also have case, must also be at least 8 bits. Forced me to get a password that was even more secure than my bank password (well, now I've forgotten what I put in...). , just hit basic.


$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  # Directly into the dynamic library search path, no additional environment configuration is 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. Configure tnsnames.ora (no need to configure tns)

Note that tnsnames.ora doesn't really exist, it's to be created (this is also gross, I was beginning to think I needed to install something). , I did not use this way, interested can google1 under.

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 # Having this file indicates a successful installation, according to python Find your own location, possibly somewhere else 1 Next! 

6. Validation 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 the dynamic library of instant client was not found, whether the environment variable under check1 is configured, whether it takes effect, and whether the version is correct.

If there is an error: ImportError:./ cx_Oracle.so: undefined symbol: PyUnicodeUCS4_Decode


Google Information: 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

So recompile python


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

Again, import is working.

Use:

1. The basic connection wok USES Oracle tns alias


connection =cx_Oracle.connect("tp/tp@ocn_test")
# To view tns alias The command 
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. User enters password to connect


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

3. The user enters the connection account information directly into the Python command in the form python script.py tp/tp @ocn_test


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

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


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. Use DSN to form TNSNAME


$unzip instantclient-basic-linux.x64-10.2.0.4.0.zip
$cd instantclient_10_2
$cp * /usr/lib  # Directly into the dynamic library search path, no additional environment configuration is 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

0

6. Log on to as SYSDBA


$unzip instantclient-basic-linux.x64-10.2.0.4.0.zip
$cd instantclient_10_2
$cp * /usr/lib  # Directly into the dynamic library search path, no additional environment configuration is 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

1

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


$unzip instantclient-basic-linux.x64-10.2.0.4.0.zip
$cd instantclient_10_2
$cp * /usr/lib  # Directly into the dynamic library search path, no additional environment configuration is 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

2

Solution:

For problem analysis, see http:// ora-12514. ora-code. com/.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: