Oracle 11g client的配置
首先從OTN下載幾個壓縮包
為: http://www.oracle.com/technology/software/tech/oci/instantclient/index.html
然後根據你的平台,選擇不同的下載,我是安裝在rhel5下,所以選擇了linux x86 並下載了下面2個包:
instantclient-basic-linux32-11.1.0.7.zip 基本包,提供了OCI,OCCI和JDBC-OCI應用程式的支援。
instantclient-sdk-linux32-11.1.0.7.zip 附加的標頭檔和makefile檔案,部分需要編譯的模板需要,比如編譯php的oci8,和python 的cx_Oracle. (我們這裡僅使用其提供的oci標頭檔)
instantclient-sqlplus-linux32-11.1.0.7.zip 可選安裝,sqlplus..有時候測試,管理什麼的很方便。
1.建立oracle使用者和組:
#groupadd oinstall
#useradd -g oinstall oracle
#passwd oracle
2.建立一個Oracle 基本目錄
#mkdir –p /usr/local/oracle
#chown –R oracle:oinstall /usr/local/oracle
#chmod –R 775 /usr/local/oracle
將上述tar.gz或zip包解壓,拷貝到 /usr/local/oracle目錄中。
3.設定 ORACLE_HOME和LD_LIBRARY_PATH環境變數,配置TNS_ADMIN變數(放置 tnsname.ora的目錄).如果使用sqlplus的話,也設定下PATH環境變數。修改oracle使用者目錄下的 .bash_profile加入下面語句.
export ORACLE_HOME=/usr/local/oracle
#export ORACLE_SID=hbdb
export SQLPATH=/usr/local/oracle
#尋找tnsnames.ora路徑
export TNS_ADMIN=/usr/local/oracle
export NLS_LANG=''american_america.ZHS16GBK''
export LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH
export PATH=$PATH:$ORACLE_HOME
4.tnsnames.ora配置:
MYDB =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.0.5)(PORT = 1521))
)
(CONNECT_DATA =
(SID = HBDB)
(SERVER = DEDICATED)
)
)
5.sqlplus串連資料庫:
sqlplus user/passwd@10.0.0.5/hbdb或sqlplus user/passwd@MYDB
如果出現:sqlplus: error while loading shared libraries: /usr/local/oracle/libnnz11.so: cannot restore segment prot after reloc: Permission denied
最簡單的解決方案莫過於將SElinux設定位PERMISSIVE狀態:
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
[oracle@localhost ~]# sqlplus xfdb/xfdb@MYDB
SQL*Plus: Release 11.1.0.7.0 - Production on Wed Jun 17 16:37:06 2009
Copyright (c) 1982, 2008, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> select sysdate from dual;
SYSDATE
------------
17-JUN-09
SQL>
6.測試eams項目資料庫訪問組件dboci(對oci的c++封裝)
先建立一個libclntsh.so.11.1的連結:
[oracle@localhost oracle]# ln -s libclntsh.so.11.1 libclntsh.so
#include "dboci.h"
#include <iostream>
int main(void)
{
CDbOci oci;
int iret = oci.Open("xfdb", "xfdb", "HBDB");
if (iret < 1)
return 0; //
char*** result = NULL;
int num = oci.Query("select sysdate from dual", &result, 1);
if (num > 0)
{
for (int i=0; i < num; i++)
{
std::cout <<result[i][0] <<std::endl;
}
oci.FreeExecSqlBuf(&result, num, 1);
}
oci.Close();
return 1;
}
scons指令碼:
env = Environment()
env.Append(CCFLAGS='-g')
src_files = Split('DBconnect.cpp ../dboci/source/dboci.cpp')
include = Split('/usr/local/oracle/sdk/include ../dboci/include')
lib_path = Split('/usr/local/oracle')
lib_files = Split('clntsh nnz11')
env.Program(target='dbconnect',source = src_files, LIBS=lib_files, LIBPATH=lib_path,CPPPATH=include)
運行結果:
[root@localhost dbconnecttest]# ./dbconnect
17-JUN-09
至此eams項目資料庫存取的開發與部署測試通過!
順帶說明下oracle 11g即時用戶端在windows下的配置
1. 假設把壓縮檔解壓到d盤根目錄;
2. 編輯環境,將d:\instantclient_11_1添加到PATH中(注意位於其他Oracle目錄之前),增加系統內容變數TNS_ADMIN設定為d:\instantclient_11_1;ORACLE_HOME設定為d:\instantclient_11_1;NLS_LANG為''american_america.ZHS16GBK''
例如,在Windows 2000上,依次單擊“開始”->“設定”->“控制台”->“系統”->“進階”->“環境變數”,編輯系統變數列表中的PATH,系統內容變數TNS_ADMIN設定為d:\instantclient_11_1;系統內容變數ORACLE_HOME設定為d:\instantclient_11_1;系統內容變數NLS_LANG設定為american_america.ZHS16GBK
3. 編輯d:\instantclient_11_1\ tnsnames.ora檔案:
MYDB =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.0.5)(PORT = 1521))
)
(CONNECT_DATA =
(SID = HBDB)
(SERVER = DEDICATED)
)
)
其中:MYDB:資料庫的案例名
10.0.0.5資料庫伺服器地址
HBDB:資料庫的SID;