Install Oracle Instant Client in Ubuntu

Source: Internet
Author: User

Recently, I need to write a data migration script to migrate data from a single Oracle database to a MySQL Sharding cluster. I just recently learned python and used it to practice it.
I quickly completed MySQL and only needed to install a python module of MySQLdb. However, for the Oracle Client, you not only need to install the corresponding python module (here I use the official python module of Oracle -- cx_Oracle), but also need to install the Oracle Client. Generally, it is enough to select Instant Client, you also need to configure tnsnames. ora (you can also simply access it through host: port/schema ).

The procedure is as follows.

First, confirm the version. Because our Oracle Data is a little old, I chose an old version-Oracle Instant Client 10.2.0.4. Download from the official website. : Http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html. This is a serious issue for BS Oracle. Registration is required before downloading. The key is that the password must contain numbers, letters, and uppercase/lowercase letters during registration, it must have at least 8 digits. Forced me to create a password that is more secure than my bank password (well, now I forgot what I entered ..). When downloading, pay special attention to the need to download the rpm package. zip does not know what it is. Basic.
Forrest @ Ubuntu :~ /Sources $ wget http://download.oracle.com/otn/linux/instantclient/10204/oracle-instantclient-basic-10.2.0.4-1.x86_64.rpm
As it is an rpm package, convert it into a deb package in ubuntu first:
Forrest @ ubuntu :~ /Sources $ sudo alien oracle-instantclient-basic-10.2.0.4-1.x86_64.rpm
Get oracle-instantclient-basic_10.2.0.4-2_amd64.deb.
Yes,
Forrest @ ubuntu :~ /Sources $ sudo dpkg-I oracle-instantclient-basic_10.2.0.4-2_amd64.deb
This will be installed in the default directory --/usr/lib/oracle/10.2.0.4/client64/
Forrest @ ubuntu:/usr/lib/oracle/10.2.0.4/client64 $ ls
Bin lib


After the installation is complete, some environment variables need to be exposed; otherwise, an error is reported:
* Import cx_Oracle gave ImportError: libclntsh. so.10.1: cannot open shared object file: No such file or directory until I set LD_LIBRARY_PATH =/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/lib/
* Conn = cx_Oracle.Connection ('Scott/tiger @ xe ') gave RuntimeError: unable to acquire Oracle environment handle until I set ORACLE_HOME =/usr/lib/oracle/xe/app/oracle/product/10.2.0/server


Forrest @ ubuntu :~ /Sources $ sudo vim ~ /. Profile
Add the following statement at the end:
Export ORACLE_HOME =/usr/lib/oracle/10.2.0.4/client64
Export PATH = $ PATH: $ ORACLE_HOME/bin
Export LD_LIBRARY_PATH = $ LD_LIBRARY_PATH: $ ORACLE_HOME/lib
Export TNS_ADMIN = $ ORACLE_HOME/network/admin


I noticed that the directory where TNS_ADMIN is located does not actually exist. It was created by myself (this is also disgusting. At first I thought I had to install something else ..)
$ Sudo mkdir-p $ ORACLE_HOME/network/admin
$ Sudo cp tnsnames. ora $ ORACLE_HOME/network/admin

If you have installed sqlplus, you can use it to test whether the installation is correct:
$ Sqlplus 'username/password @ SID'

But linux sqlplus is too bad, I do not intend to Install, so keep it, next Install python module -- cx_Oracle -- refer to Install cx_Oracle in ubuntu (http://leojay.blog.163.com/blog/static/1739841912009101165546640)
To SourceForge search cx_Oracle, according to my python version and the database version to operate, select the cx_Oracle-5.1-10g-py26-1.x86_64.rpm version, can only say that I believe that the high version is backward compatible. Try it first.
Decompress the package and put cx_Oracle.so under dist-packages:
$ Sudo cp cx_Oracle.so/usr/local/lib/python2.6/dist-packages/
In this case, OK.
Write a simple test program and test it:
#! Bin/python
Import cx_Oracle

Conn = cx_Oracle.connect ('user/passwd @ sid ')

Cursor = conn. cursor ()
Cursor.exe cute ("SELECT * from product_detail where product_id = 232896483 ")
Row = cursor. fetchone ()
Print "result:", row

Cursor. close ()
Conn. close ()

If there is no problem, you can see the result. In this case, you may encounter the following problems:
Forrest @ ubuntu :~ /Work/data-migration $ python oracledb. py
Traceback (most recent call last ):
File "oracledb. py", line 5, in <module>
Conn = cx_Oracle.connect (''user/passwd @ sid ')
Cx_Oracle.DatabaseError: ORA-12514: TNS: listener does not currently know of service requested in connect descriptor
Check your/usr/lib/oracle/10.2.0.4/client64/network/admin/tnsnames. ora configuration file to ensure that the sid is correctly configured.
Or directly use host: port/schema:
Conn = cx_Oracle.connect ('user/passwd @ host: port/scheme ')


If you have other problems, you can add a sqlnet. ora file under/usr/lib/oracle/10.2.0.4/client64/network/admin and run it in trace mode:
If for some reason you have some trouble connecting, you can create a sqlnet. ora file under $ ORACLE_HOME with some tracing options.
$ Sudo vi $ ORACLE_HOME/network/admin/sqlnet. ora
TRACE_DIRECTORY_CLIENT =/tmp
TRACE_LEVEL_CLIENT = SUPPORT


The next time the Oracle Instant Client is used, it will create a detailed log file under/tmp like the following: cli_1968.trc. make sure to turn this option off when you are done as the logfile can get quite large!

PS: currently, it seems that cx_Oracle is backward compatible. Hopefully, it is much more difficult to build an environment than to write a program.

Related Article

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.