Basics of connecting OCI to Oracle in Linux ---- configuring Instant Client to connect to oracle Database
Some time ago, a brother posted a post on my blog about how to use OCI to link oracle Database Configuration methods, A lot of things in that article have not been clearly explained. I can understand that people seem to be in the fog. Oracle's reputation was not covered, and the old man was so anxious that I had to add qq and asked me three or four days before I finally got the OCI link to the oralce database.
You can't blame that guy. Unlike C-connected mysql, the official documentation is very detailed in Chinese. Many mysql books on the market have examples and related introductions about C-connected mysql databases. I thought that at the beginning, I used the OCI interface to connect to the oracle database to the Xinhua Bookstore/Dangdang to read oracle-related books, all of which were about java and php, the example of multi-milk and other languages connected to oracle only contains an introduction to some OCI in Oracle Spatial and OCI advanced programming. The only book does not mention how to configure the OCI connection environment.
The official oracle documents are full of English and are hidden for fear of seeing them. The detailed Chinese documents of mysql are completely confusing. online searches can only find configuration methods in windows, linux is basically unavailable. At that time, I made it step by step based on the configuration methods in windows and some English documents. In order not to let later people repeat the old man's mistakes, through this article, I will teach you how to use C language to connect to the oracle database through OCI in linux step by step from scratch.
It should be noted that oracle has the corresponding client installation package, but the standard client package not only has more than 400 mb of fat, but has added all the things in MB, in addition, in linux, many versions cannot be installed (I do not remember Fedora ).
First, go to the oracle official website to download the C language library file and header file URL as follows, select the corresponding Instant Client Version (I downloaded all zip packages without downloading the rpm package, the rpm package should be configured during installation ):
Http://www.oracle.com/technology/software/tech/oci/instantclient/index.html
The following is the version of linux i386
Http://www.oracle.com/technology/software/tech/oci/instantclient/htdocs/linuxsoft.html
Instant Client Package-Basic contains the dynamic library file libclntsh. so.11.1 (the latest version may be different) and load it to the dynamic library search path. Here we will not talk about the detailed steps. If you have any questions about Baidu/dog search, you cannot leave a message below.
* The Instant Client Package-SDK contains the header file.
* Instant Client Package-SQL * Plus sqlplus is better to be installed next. test whether the Client is configured and check whether all SQL statement execution results require it.
Download file path settings:
/Home/oracle/lib stores various library files of Basic and sqlplus, so in/etc/ld. so. add the path/home/oracle/lib to the conf file, and the path is libclntsh in the current file. so.11.1 create a soft connection ln-s libclntsh in the library file. so.11.1 libclntsh. so allows the code to find the library file when connecting to the dynamic library.
/Home/oracle/include stores the SDK header file.
After the configuration found in the terminal running sqlplus link oracle Database ERROR: ORA-12541: TNS: no listener network on the search said that the oracle database is not configured listener or the listener is not started, the oracle 10g Database Listener is clearly started, and the oracle client is installed on another windows machine in the network, and sqlplus can be used to connect to and operate the database.
Even sqlplus reports the same error, which is not a problem with sqlplus or oci. It is a problem with the Environment configuration of instant. Search Baidu/dog and find that the oracle instant configuration requires an environment variable parameter: ORACLE_HOME. Using the export command is only valid for the currently running system. After the system is restarted, it will be gone. To make the configuration take effect permanently, edit it ~ /. Bashrc file. Add the following sentence to the end of the file:
Export ORACLE_HOME =/home/oracle/lib
Note: the path of the ORACLE_HOME environment variable is to decompress the Instant Client Package-Basic zip Package. The path contains the so file jar files extracted from the Basic Package.
Finally, create an oracle configuration file tnsnames under the $ ORACLE_HOME/network/admin directory (new if no one exists. ora file, my tnsnames. ora is copied directly from other machines. The content is as follows:
# Tnsnames. ora Network Configuration File:/home/oracle/product/10.2.0/db_1/network/admin/tnsnames. ora
TEST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP) (HOST = 192.168.1.8) (PORT = 1521 ))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = test)
)
)
EXTPROC_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC) (KEY = EXTPROC1 ))
)
(CONNECT_DATA =
(SID = PLSExtProc)
(PRESENTATION = RO)
)
)
After the preceding settings are configured, log out of the user and log on again to make the ORACLE_HOME environment variable take effect. The check method is very simple. Run the following commands on the terminal:
[Mgqw @ localhost ~] $ Echo $ ORACLE_HOME
/Home/oracle
If there is a path output as above, the configuration is successful. If there is no output, edit it again ~ /. Bashrc to see what went wrong.
After the check is complete, run sqlplus to log on to the database! The example is as follows. The C program is successfully connected to the database! Finally, the environment has been configured.
[Mgqw @ localhost ~] $ Sqlplus uname/pswd @ dbname
SQL * Plus: Release 11.1.0.7.0-Production on Sun May 31 14:22:00 2009
Copyright (c) 1982,200 8, Oracle. All rights reserved.
Connected:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0-64bit Production
With the Partitioning, OLAP and Data Mining options
SQL> select * from t1;
A B
--------------------
0 3
2 3
20 3
21 3
8 1
9 1
30 32
-1 2
1 4
3 6
5 8
A B
--------------------
7 10
22 2
26 2
14 rows selected.
The following is a description of the compensation configuration file:
The tnsnames. ora file is stored on the client and stored in the ORACLE_HOME/network/admin directory by default. The sqlnet. ora file controls the behaviors of the Oracle Net Services Client, such as the tracking level and session characteristics. We already know the usage and location of the listener. ora and init. ora files.
Basically, when a customer sends a request, the name of Oracle Net Services is parsed by using the tnsnames. ora file. The parameters in the tnsnames. ora file direct the request to the appropriate database node. The listener process listens to customer requests and connects them to a server process. The request is processed and the result is returned to the client machine.