從Oracle訪問SQL Server(GATEWAYS)

wailon發表於2013-11-09

通過資料連結(Database Link),從Oracle 10g 連線 SQL Server 2000(例項)

從Oracle 10g連線SQL Server2000,需要用透明閘道器(Transparent gateways),通過它我們可以 sqlplus 操縱其他資料庫,如 ms sqlserver 、 sybase 、 infomix 等,實現資料庫的異構服務。

一、安裝Transparent gateways for Windows SQL Server:
 注意:安裝的之前必須要將原資料庫伺服器啟動起來,不然會重複安裝OraTg10g_home1資料庫軟體,這樣就會有兩個資料庫軟體,導致listener混亂。
 1. 下載10201_gateways_win32.zip檔案並解壓
 2. 進入10201_gateways_win32資料夾,點選setup.exe
 3. 按Next,選擇Oracle Transparent gateways for Microsoft SQL Server 10.2.0.1.0 ,按Next
 4、有一步需要填寫SQL 伺服器名和SQL的資料庫名
 5. 接著安裝。
 6. 安裝完畢之後,在Oracle的D:/oracle/product/10.2.0/db_1主目錄下,會有tg4msql資料夾

注:設定gateways機器(gateways可以和Oracle Database在同一臺機器上,也可以分開)。
 
二、配置:
 我的環境設定是將gateways和Oracle Database在設定在同一臺機器
 我的環境:
 Oracle 伺服器:
     OS: Windows Server 2003(SP2)
     IP: 192.168.16.82
     PORT: 1521
 SQL 伺服器:
     OS: Windows Server 2003
     IP: 192.168.16.82
     Database Name: wailon

(一)通過tg4msql連線:
 1. 在Oracle的D:/oracle/product/10.2.0/tg_1/tg4msql/admin目錄下,拷貝inittg4msql.ora並改名為init.ora。
    例如,預設的SID是ELE_OA,那麼我的檔名是initELE_OA.ora。

2. 配置init.ora,以我的例子,如下:
 ******************************
 # This is a sample agent init file that contains the HS parameters that are
 # needed for the Transparent gateways for SQL Server

#
 # HS init parameters
 #
 #11g的HS_FDS_CONNECT_INFO格式server_name:port//dbname,server_name不能使用IP地址,如果配置有問題,連LISTENER都無法啟動
 #
 HS_FDS_CONNECT_INFO="SERVER=192.168.16.83;DATABASE=ELE_OA"
 HS_FDS_TRACE_LEVEL=OFF
 HS_FDS_RECOVERY_ACCOUNT=RECOVER
 HS_FDS_RECOVERY_PWD=RECOVER
 *******************************

3、配置Oracle的D:/oracle/product/10.2.0/tg_1/network/admin目錄下的listener.ora
 修改為:
 SID_LIST_LISTENER =
   (SID_LIST =
     (SID_DESC =
       (SID_NAME = PLSExtProc)
       (ORACLE_HOME = D:/oracle/product/10.2.0/db_1)
       (PROGRAM = extproc)
     )
     (SID_DESC=
       (SID_NAME=ELE_OA)    --對應tg4msql\admin目錄下的init檔案
       (ORACLE_HOME=D:/oracle/product/10.2.0/db_1)
       (PROGRAM=tg4msql)  --這個值不能修改,11g的為dg4msql
     )

  )
 LISTENER =
   (DESCRIPTION_LIST =
     (DESCRIPTION =
       (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.16.82)(PORT = 1521))
     )
   )

4.配置Oracle的D:/oracle/product/10.2.0/tg_1/network/admin目錄下的tnsnames.ora如下:
 MSQL =
   (DESCRIPTION =
     (ADDRESS_LIST =
       (ADDRESS = (PROTOCOL = TCP)(HOST =192.168.16.82)(PORT = 1521))
     )
     (CONNECT_DATA =
       (SID = ELE_OA)
     )
     (HS = OK)
   )
 解釋如下:
 HOST:指gateways所在的機器。
 PORT:在gateways機器中listener.ora檔案裡指定的監聽gateways的埠
 SID:必須和gateways機器中listener.ora檔案裡指定的SID_NAME相同
 HS = OK:指定該連線將使用Oracle的異構服務

5、檢查ORACLE_HOME/network/admin/sqlnet.ora
 # sqlnet.ora Network Configuration File: D:/oracle/product/10.2.0/db_1/network/admin/sqlnet.ora
 # Generated by Oracle configuration tools.
 # This file is actually generated by netca. But if customers choose to
 # install "Software Only", this file wont exist and without the native
 # authentication, they will not be able to connect to the database on NT.
 SQLNET.AUTHENTICATION_SERVICES= (NTS,NONE)
 NAMES.DIRECTORY_PATH = (TNSNAMES, HOSTNAME)


注意
如果檔案中包含:
 names.directory_path = (TNSNAMES, HOSTNAME)
 names.default_domain = world
 name.default_zone = world
 這些行,那麼配置的第4點中的connect_descriptor必須新增.world字尾(如上例中MSQL必須為MSQL.world)
 參考文件中的:
 connect_descriptor=
    (DESCRIPTION=
       (ADDRESS=
          (PROTOCOL=TCP)
          (HOST=host_name)
          (PORT=port_number)
       )
       (CONNECT_DATA=
          (SID=gateways_sid))
       (HS=OK))

 connect_descriptor is the description of the object to connect to as specified when creating the database link, such as tg4msql. Check the sqlnet.ora file in the Oracle database server's ORACLE_HOME for the following lines:
 names.directory_path = (TNSNAMES, HOSTNAME)
 names.default_domain = world
 name.default_zone = world
 Note: If the Oracle database server is on Microsoft Windows, the file is ORACLE_HOME/network/admin/sqlnet.ora.

6、動態修改global_names引數值:
 alter system set global_names = false;

否則會在執行sql時報錯:ORA-02085: 資料庫連結MSQL與HO.WORLD相連結
原因如下:The GLOBAL_NAMES parameter when set to TRUE implies that database link name should be similar to the Global database name to which you are trying to connect.

7. 重啟LISTENER服務
 lnsrctl stop
 lsnrctl start

8. 用sqlplus建立並測試Database Link
 sqlplus /nolog
 SQL> connect sys/test@ORCL88 as sysdba      ----&gt用sysdba的身份登陸
 --建立Database Link
1) SQL>CREATE PUBLIC DATABASE LINK MSQL CONNECT TO  sa IDENTIFIED BY sa USING 'MSQL';
 注:connect to後面是使用者名稱,identified by後面是密碼,using後面是SID。這些資訊都是SQL SERVER的。

2)SQL> create public database link ELE_OA connect to "sa" identified by "sa" using
 '(DESCRIPTION =
 (ADDRESS=(PROTOCOL=tcp)(HOST=192.168.16.82)(PORT=1522)) (CONNECT_DATA=(SID=ELE_OA))
 (HS = ok))';

SQL>select * from dual@MSQL;
 查詢成功!

--例子
 create public synonym t_email for t_email@ELE_OA;
 insert into t_email("UnitId") values('11110);

drop public database link msql;


常見問題:

ORA-28545,配置的全過程中使用的SID與SQL SERVER的庫名保持一致,或者由於server_name使用了IP地址

ORA-28513  listener.ora的ORACLE_HOME配置了db的路徑,應改為gateways的路徑

ORA-12514  配置本地db的tnsnames.ora增加sid

 


(二)通過hsodbc連線:
 1. 在ODBC中建立SQL Server連線的System DSN,使用機器名WAILON-PC。(步驟不詳述,請查MSDN)
 可參考http://www.databasejournal.com/features/oracle/article.php/10893_3442661_1/Making-a-Connection-from-Oracle-to-SQL-Server.htm

2. 在Oracle主目錄下D:/oracle/product/10.2.0/db_1/hs/admin的目錄下,拷貝inithsodbc.ora並改名為initwailon.ora。這次,我用的SID是WAILON,所以檔名是WAILON。如下:
 HS_FDS_CONNECT_INFO後面是資料來源名稱。
 ******************************
 # This is a sample agent init file that contains the HS parameters that are
 # needed for an ODBC Agent.
 #
 # HS init parameters
 #
 HS_FDS_CONNECT_INFO = WAILON-PC
 HS_FDS_TRACE_LEVEL = OFF
 #
 # Environment variables required for the non-Oracle system
 #
 #set =
 ******************************
 注意:HS_FDS_CONNECT_INFO後面是資料來源名稱

3. 配置listener.ora,加入下面一段:
 ******************************
    (SID_DESC=
       (ORACLE_HOME = D:/oracle/product/10.2.0/db_1)
       (SID_NAME = wailon)
       (PROGRAM = hsodbc)
     )
 ******************************
 修改為:
 # listener.ora Network Configuration File: D:/oracle/product/10.2.0/db_1/network/admin/listener.ora
 # Generated by Oracle configuration tools.

SID_LIST_LISTENER =
   (SID_LIST =
     (SID_DESC =
       (SID_NAME = PLSExtProc)
       (ORACLE_HOME = D:/oracle/product/10.2.0/db_1)
       (PROGRAM = extproc)
     )
    (SID_DESC=
       (ORACLE_HOME = D:/oracle/product/10.2.0/db_1)
       (SID_NAME = wailon)
       (PROGRAM = hsodbc)
     )
   )

LISTENER =
   (DESCRIPTION_LIST =
     (DESCRIPTION =
       (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.16.82)(PORT = 1521))
     )
   )

4. 配置tnsnames.ora,如下:
 ******************************
wailon  =
   (DESCRIPTION=
     (ADDRESS=(PROTOCOL=tcp)(HOST=192.168.16.82)(PORT=1521))
     (CONNECT_DATA= (SID = wailon))
     (HS=OK)
   )

此處的HOST是指MS SQL資料庫所在的IP
 ******************************
 5、重啟LISTENER服務
 6、連線測試:
 建立資料庫連線:
 drop  database link sqlLink ;
 sql>create database link sqlLink connect to sa identified by "sa" using 'wailon';
 sql>select * from ttemp4@sqlLink;


 
常見問題:
ORA-28500: 連線 ORACLE 到非 Oracle 系統時返回此資訊:

 [Generic Connectivity Using ODBC][H006] The init parameter is not set. Please set it in init.ora file.
 ORA-02063: 緊接著 2 lines (起自 SQLLINK)
 解決:init.ora中的orasid是MS SQL資料庫中的指定的資料庫名稱


ORA-28500: 連線 ORACLE 到非 Oracle 系統時返回此資訊:
 [Generic Connectivity Using ODBC][Microsoft][ODBC 驅動程式管理器] 未發現資料來源名稱並且未指定預設驅動程式 (SQL State: IM002; SQL Code: 0)
 ORA-02063: 緊接著 2 lines (起自 SQLLINK)
 解決:HS_FDS_CONNECT_INFO = WAILON-PC,此時的HS_FDS_CONNECT_INFO的應該賦予配置的ODBC資料來源的名稱


ORA-02085: 資料庫連結 SQLLINK.TEST.COM.CN 連線到 HO.WORLD
 解決:執行alter system set global_names = false;語句

解決上面的問題之後查詢成功!!

 

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/429786/viewspace-776193/,如需轉載,請註明出處,否則將追究法律責任。

相關文章