Linux 下运行OCCI程序,一直报如下错误:
Exception: Code – 1804, Message – Error while trying to retrieve text for error ORA-01804
之前也遇到了该错误,当时解决办法是配置 Oracle_HOME 环境变量。
(一) 安装Oracle Instant Client
这次我使用的是 Oracle Instant Client 11.2.0.1.0 ,
下载地址:
http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html
可以下载rpm包或者zip包进行安装.
(1) rpm 包
若下载的是Version 11.2.0.1.0 版本的 rpm 包,
oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.rpm
oracle-instantclient11.2-sqlplus-11.2.0.1.0-1.x86_64.rpm
oracle-instantclient11.2-devel-11.2.0.1.0-1.x86_64.rpm
使用如下命令进行安装
rpm -ivh oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.rpm
rpm -ivh oracle-instantclient11.2-sqlplus-11.2.0.1.0-1.x86_64.rpm
rpm -ivh oracle-instantclient11.2-devel-11.2.0.1.0-1.x86_64.rpm
安装完成后,
ORACLE Instant Client 相关的头文件在 /usr/include/oracle/11.2/client64/下(sdk包中的),
库文件在 /usr/lib/oracle/11.2/client64/下(cliient64目录下有 lib 和 bin两个目录.){sqlplus相关的配置在 (2) 中说明 }
配置如下环境变量: ~/.bashrc
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
export TNS_ADMIN=$ORACLE_HOME/network/admin
export NLS_LANG=’simplified chinese_china’.ZHS16GBK
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
export PATH=$ORACLE_HOME/bin:$PATH
(2) zip 包
若下载的是Version 11.2.0.1.0 版本的 zip 包,则下载如下三个包
oracle-instantclient11.2-sdk-11.2.0.1.0-1.x86_64.zip
oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.zip
oracle-instantclient11.2-sqlplus-11.2.0.1.0-1.x86_64.zip
使用如下命令进行解压安装
unzip oracle-instantclient11.2-sdk-11.2.0.1.0-1.x86_64.zip
unzip oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.zip
unzip oracle-instantclient11.2-sqlplus-11.2.0.1.0-1.x86_64.zip
例如最后解压到目录 /opt/oracle_instant_client/instantclient_11_2 下,目录下的文件和目录如下
[root@localhost instantclient_11_2]# ls
adrci genezi libclntsh.so.11.1 libocci.so.11.1 libocijdbc11.so libsqlplus.so ojdbc5.jar
sdkSQLPLUS_README xstreams.jarBASIC_README glogin.sql libnnz11.so libociei.so
libsqlplusic.sonetworkojdbc6.jar sqlplus sqlplus.sh
{ 头文件在 sdk/include下,库文件在instantclient_11_2目录下 }
{ network/admin目录是我自己创建的,用于sqlplus连接数据库,目录下有一个文件 tnsnames.ora,配置了数据库连接,文件内容如下:
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.232.131)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
ORCL_XP =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.232.133)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
) )
}
配置如下环境变量: ~/.bashrcexport
export ORACLE_HOME=/opt/oracle_instant_client/instantclient_11_2
export TNS_ADMIN=$ORACLE_HOME/network/adminexport
export NLS_LANG=’simplified chinese_china’.ZHS16GBKexport
export LD_LIBRARY_PATH=$ORACLE_HOME
export PATH=$ORACLE_HOME:$PATH
配置完毕后, source ~/.bashrc
执行 sqlplus 命令即可连接数据库进行操作,例如:
sqlplus /nolog
conn scott/tiger@orcl
安装完毕!
(二) 编译OCCI程序
编译OCCI程序可以使用两种方式:
(1) 方法一:使用 Oracle 11.2.0.1 数据库软件中自带的 OCCI 相关库和头文件.
在一个安装好Oracle 11.2.0.1 R2 数据库的机器上拷贝occi相关头文件和库文件到目录 /opt/OCCI_11g_R2/include 和 /opt/OCCI_11g_R2/lib 下,
头文件:
mkdir /opt/OCCI_11g_R2/include
cp -r /sdd1/oracle/11gR2_database_X64/product/11.2.0.1.0/db_1/rdbms/public/* /opt/OCCI_11g_R2/include/
库文件:
mkdir /opt/OCCI_11g_R2/lib
cp /sdd1/oracle/11gR2_database_X64/product/11.2.0.1.0/db_1/lib/libclntsh.so.11.1 /opt/OCCI_11g_R2/lib/
cp /sdd1/oracle/11gR2_database_X64/product/11.2.0.1.0/db_1/lib/libocci.so.11.1 /opt/OCCI_11g_R2/lib/
cp /sdd1/oracle/11gR2_database_X64/product/11.2.0.1.0/db_1/libocci11.a /opt/OCCI_11g_R2/lib/
cp /sdd1/oracle/11gR2_database_X64/product/11.2.0.1.0/db_1/libnnz11.so /opt/OCCI_11g_R2/lib/
cp /sdd1/oracle/11gR2_database_X64/product/11.2.0.1.0/db_1/libnnz11.a /opt/OCCI_11g_R2/lib/
cd /opt/OCCI_11g_R2/lib
创建3个软链接
ln -s libocci.so.11.1 libocci.so
ln -s libclntsh.so.11.1 libclntsh.so
ln -s libclntsh.so libclntsh.so.10.1
[root@localhost lib]# ll
总计 69764
lrwxrwxrwx 1 root root 17 01-07 13:33 libclntsh.so -> libclntsh.so.11.1
lrwxrwxrwx 1 root root 12 01-07 13:33 libclntsh.so.10.1 -> libclntsh.so
-rwxr-xr-x 1 root root 48724689 01-07 12:58 libclntsh.so.11.1
-rw-r–r– 1 root root 11595642 2012-01-07 libnnz11.a
-rw-r–r– 1 root root 7899997 2012-01-07 libnnz11.so
-rw-r–r– 1 root root 1863334 01-07 12:50 libocci11.a
lrwxrwxrwx 1 root root 15 01-07 13:33 libocci.so -> libocci.so.11.1
-rwxr-xr-x 1 root root 1260923 01-07 12:50 libocci.so.11.1
编译OCCI程序时使用如下选项:
OCCI_HOME=/opt/OCCI_11g_R2
OCCI_INCLUDE_DIR=$(OCCI_HOME)/include
OCCI_LIBRARY_PATH=$(OCCI_HOME)/lib
g++ *.cpp -I$(OCCI_INCLUDE_DIR) -L$(OCCI_LIBRARY_PATH) -locci -lclntsh -lnnz11
编译成功!
(2) 方法二:使用 Oracle Instant Client 11.2.0.1.0 sdk来编译程序
OCCI_INCLUDE_DIR=/usr/include/oracle/11.2/client64
OCCI_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
g++ *.cpp -I$(OCCI_INCLUDE_DIR) -L$(OCCI_LIBRARY_PATH) -locci -lclntsh -lnnz11
编译成功!
(三) 运行OCCI程序
运行OCCI程序必须安装 ORACLE INSTANT CLIENT,并配置 ORACLE_HOME 环境变量!以上两种方式安装的 Oracle Instant Client 均可以。
export LD_LIBRARY_PATH=/opt/OCCI_11g_R2/lib:$LD_LIBRARY_PATH
./a.out
执行脚本运行程序,当连接数据库时,一直报错误: Error while trying to retrieve text for error ORA-01804。
弄了半天,最后注释掉 export LD_LIBRARY_PATH=/opt/OCCI_11g_R2/lib:$LD_LIBRARY_PATH,
即直接使用之前配置的 LD_LIBRARY_PATH 执行程序,问题解决!
所以,这里是 LD_LIBRARY_PATH 变量的问题。