oracle通過gateway創建dblink訪問sqlserver數據庫


 

 背景:

最近有個客戶希望oracle能夠訪問sqlserver的數據,上網查了一下,可以透明網關加dblink來實現,來測試一下

一.環境介紹:

測試環境為vmware workstation 12 里面的兩台虛擬機,網卡模式都是”自定義NAT模式“,兩邊網絡是通的。

oracle端:

操作系統:Red Hat Enterprise Linux Server release 6.4 (Santiago)

數據庫:oracle 11.2.0.4  單實例

IP地址:192.168.59.180

sqlserver端:

操作系統:windows server 2012 Datacenter

數據庫:sqlserver 2012

IP地址:192.168.59.121

二.所需軟件

透明網關(Transparent Gateway)

11g安裝包如下:

p13390677_112040_Linux-x86-64_5of7.zip

網盤鏈接分享如下:

鏈接:https://pan.baidu.com/s/1b7lBHmRDZDH0-tcKIMUitg

提取碼:1234

如果過期了請聯系。

三.步驟概覽

  1. 在oracle服務端安裝gateway

  2. 在oracle服務端配置gateway的配置文件

  3. 在oracle服務端配置listener.ora文件

  4. 在oracle服務端配置tnsnams.ora文件

  5. 在sqlserver端獲取到登錄的用戶密碼

  6. 在oracle服務端創建dblink

  7. 開始使用

四.安裝軟件

1.執行安裝

[root@11g u01]# chown oracle:oinstall p13390677_112040_Linux-x86-64_5of7.zip 
[root@11g u01]# su - oracle
[oracle@11g ~]$ cd /u01
[oracle@11g u01]$ unzip p13390677_112040_Linux-x86-64_5of7.zip 
然后會出現如下文件夾
gateways
[oracle@11g u01]$ cd gateways/
[oracle@11g gateways]$ ll
total 56
drwxr-xr-x  4 oracle oinstall  4096 Aug 26  2013 install
drwxr-xr-x  4 oracle oinstall  4096 Aug 26  2013 legacy
-rw-r--r--  1 oracle oinstall 30016 Aug 27  2013 readme.html
drwxr-xr-x  2 oracle oinstall  4096 Aug 26  2013 response
-rwxr-xr-x  1 oracle oinstall  3267 Aug 26  2013 runInstaller
drwxr-xr-x 14 oracle oinstall  4096 Aug 26  2013 stage
-rw-r--r--  1 oracle oinstall   500 Aug 27  2013 welcome.html
[oracle@11g gateways]$ export DISPLAY=192.168.59.1:0.0
[oracle@11g gateways]$ ./runInstaller

 

 

 

 

 

 這里我是用oracle用戶執行安裝的,所以路徑直接就是讀取的環境變量里面的ORACLE_HOME.

 

 

 

 

 

 這里需要注意就是把sql server選項勾選

 

 

 

可以看到,需要sqlserver數據庫的host name,port number,instancename,database name四個參數。

可以看到,需要sqlserver數據庫的host name,port number,instancename,database name四個參數。

Host name:sqlserver所在主機的主機名,寫ip地址

Port number:打開sql servermanager studio,然后打開一個數據庫查詢,用如下命令獲取:

execsys.sp_readerrorlog 0, 1, 'listening'

如:

 

 

 端口為1433

instance name:

操作系統看服務,例如:MSSQLSERVER

database name:

sql servermanager studio里面數據庫,填寫你想被oracle查詢的數據庫。

 

 

 

 

 

 

 

 

 

 

 

 

 

用root用戶執行上述腳本
[root@11g ~]# /u01/app/oracle/product/11.1.0/dbhome_2/root.sh
Performing root user operation for Oracle 11g 

The following environment variables are set as:
    ORACLE_OWNER= oracle
    ORACLE_HOME=  /u01/app/oracle/product/11.1.0/dbhome_2

Enter the full pathname of the local bin directory: [/usr/local/bin]: 
The contents of "dbhome" have not changed. No need to overwrite.
The contents of "oraenv" have not changed. No need to overwrite.
The contents of "coraenv" have not changed. No need to overwrite.

Entries will be added to the /etc/oratab file as needed by
Database Configuration Assistant when a database is created
Finished running generic part of root script.
Now product-specific root actions will be performed.
Finished product-specific root actions.
[root@11g ~]#

 

 

 Exit

安裝完成

2.安裝后修改配置文件

安裝完成之后在ORACLE_HOME會生成一個文件夾dg4msql

[oracle@11g ~]$ cd $ORACLE_HOME
[oracle@11g dbhome_2]$ ll dg4msql/
total 20
drwxr-xr-x 2 oracle oinstall 4096 Dec 15 16:30 admin
drwxr-xr-x 2 oracle oinstall 4096 Dec 15 16:30 demo
drwxr-xr-x 4 oracle oinstall 4096 Dec 15 16:30 driver
drwxr-xr-x 2 oracle oinstall 4096 Dec 15 16:30 lib
drwxr-xr-x 2 oracle oinstall 4096 Dec 15 16:30 log
[oracle@11g dbhome_2]$
[oracle@11g dbhome_2]$ cd dg4msql/
[oracle@11g dg4msql]$ cd admin/
[oracle@11g admin]$ ll
total 28
-rw-r--r-- 1 oracle oinstall 10976 Nov 30  2011 dg4msql_cvw.sql
-rw-r--r-- 1 oracle oinstall   746 Jun  8  2007 dg4msql_tx.sql
-rw-r--r-- 1 oracle oinstall   368 Dec 15 16:30 initdg4msql.ora
-rw-r--r-- 1 oracle oinstall   411 Dec 15 16:30 listener.ora.sample
-rw-r--r-- 1 oracle oinstall   244 Dec 15 16:30 tnsnames.ora.sample
[oracle@11g admin]$ cat initdg4msql.ora 
# This is a customized agent init file that contains the HS parameters
# that are needed for the Database Gateway for Microsoft SQL Server

#
# HS init parameters
#
HS_FDS_CONNECT_INFO=[192.168.59.121]:1433//db_test1
# alternate connect format is hostname/serverinstance/databasename
HS_FDS_TRACE_LEVEL=OFF
HS_FDS_RECOVERY_ACCOUNT=RECOVER
HS_FDS_RECOVERY_PWD=RECOVER

[oracle@11g admin]$

可以看到,之前我們在圖形化安裝的時候輸入的信息在這里體現。

但是不知道為什么,instance_name沒有體現出來,我們修改為如下:

HS_FDS_CONNECT_INFO=192.168.59.121:1433/MSSQLSERVER/db_test1
[oracle@11g admin]$ cat initdg4msql.ora 
# This is a customized agent init file that contains the HS parameters
# that are needed for the Database Gateway for Microsoft SQL Server
#
# HS init parameters
#
#HS_FDS_CONNECT_INFO=[192.168.59.121]:1433//db_test1
# alternate connect format is hostname/serverinstance/databasename
HS_FDS_CONNECT_INFO=192.168.59.121:1433/MSSQLSERVER/db_test1
HS_FDS_TRACE_LEVEL=OFF
HS_FDS_RECOVERY_ACCOUNT=RECOVER
HS_FDS_RECOVERY_PWD=RECOVER
[oracle@11g admin]$

3.修改lintener.ora文件

[oracle@11g admin]$ lsnrctl status

LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 15-DEC-2021 16:46:04

Copyright (c) 1991, 2013, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=11g)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date                15-DEC-2021 16:45:35
Uptime                    0 days 0 hr. 0 min. 28 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/11.1.0/dbhome_2/network/admin/listener.ora
Listener Log File         /u01/app/oracle/diag/tnslsnr/11g/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=11g)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "orcl" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orclXDB" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
The command completed successfully
[oracle@11g admin]$ vi /u01/app/oracle/product/11.1.0/dbhome_2/network/admin/listener.ora
添加如下內容:
SID_LIST_LISTENER=
  (SID_LIST=
      (SID_DESC=
         (SID_NAME=dg4msql)
         (ORACLE_HOME=/u01/app/oracle/product/11.1.0/dbhome_2)
         (PROGRAM=dg4msql)
      )
  )
4.修改tnsnames.ora文件
vi /u01/app/oracle/product/11.1.0/dbhome_2/network/admin/tnsnames.ora

--添加如下內容:

dg4msql  =
  (DESCRIPTION=
    (ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))
    (CONNECT_DATA=(SID=dg4msql))
    (HS=OK)
  )


--然后重啟監聽
[oracle@11g admin]$ lsnrctl status

LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 15-DEC-2021 16:53:12

Copyright (c) 1991, 2013, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=11g)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date                15-DEC-2021 16:52:58
Uptime                    0 days 0 hr. 0 min. 14 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/11.1.0/dbhome_2/network/admin/listener.ora
Listener Log File         /u01/app/oracle/diag/tnslsnr/11g/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=11g)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "dg4msql" has 1 instance(s).
  Instance "dg4msql", status UNKNOWN, has 1 handler(s) for this service...
Service "orcl" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orclXDB" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
The command completed successfully
[oracle@11g admin]$

5.創建dblink

--sqlserver獲取登錄賬號和密碼,我忘記密碼了,所以直接重置了
sql server manager studio -- 登錄名 -- sa--屬性--重置密碼
--oracle數據庫創建dblink
create PUBLIC database link sqlserver1 connect to "sa" identified by "scott1!"  using 'dg4msql';
SQL> create PUBLIC database link sqlserver1 connect to "sa" identified by "scott1!"  using 'dg4msql';
Database link created.
SQL>

訪問數據:

Sqlserver本地訪問:

 

 

 在oracle訪問:

SQL> SELECT * FROM test1@ sqlserver1;
SELECT * FROM test1@ sqlserver1
                     *
ERROR at line 1:
ORA-28510: heterogeneous database link initialization failed
ORA-28508: invalid value 192.168.59.121:1433/MSSQLSERVER/db_test1 for Heterogeneous Services initialization parameter HS_FDS_CONNECT_INFO
ORA-02063: preceding 2 lines from SQLSERVER1

可能是因為我修改了initdg4msql.ora這個文件導致的,我們注釋掉我們的修改如下:

[oracle@11g admin]$ cat initdg4msql.ora 
# This is a customized agent init file that contains the HS parameters
# that are needed for the Database Gateway for Microsoft SQL Server

#
# HS init parameters
#
HS_FDS_CONNECT_INFO=[192.168.59.121]:1433//db_test1
# alternate connect format is hostname/serverinstance/databasename
#HS_FDS_CONNECT_INFO=192.168.59.121:1433/MSSQLSERVER/db_test1
HS_FDS_TRACE_LEVEL=OFF
HS_FDS_RECOVERY_ACCOUNT=RECOVER
HS_FDS_RECOVERY_PWD=RECOVER

[oracle@11g admin]$

再次執行查詢

SQL> SELECT * FROM test1@sqlserver1;

        id name
---------- --------------------
         1 sqlserver
         2 oracle

SQL> 

五.如何配置多個sqlserver數據庫 ?

按照上面的步驟,我們可以訪問db_test1的數據了,但是我們sqlserver這邊有很多數據庫,所以還需要配置訪問多個sqlserver數據庫。

1.配置多個initdg4msql.ora

[oracle@11g admin]$ cp initdg4msql.ora initdg4msql0.ora
[oracle@11g admin]$ cat initdg4msql0.ora 
# This is a customized agent init file that contains the HS parameters
# that are needed for the Database Gateway for Microsoft SQL Server

#
# HS init parameters
#
HS_FDS_CONNECT_INFO=[192.168.59.121]:1433//db_test2
# alternate connect format is hostname/serverinstance/databasename
#HS_FDS_CONNECT_INFO=192.168.59.121:1433/MSSQLSERVER/db_test1
HS_FDS_TRACE_LEVEL=OFF
HS_FDS_RECOVERY_ACCOUNT=RECOVER
HS_FDS_RECOVERY_PWD=RECOVER

2.在listener.ora里面配置多個

[oracle@11g admin]$ cat listener.ora
# listener.ora Network Configuration File: /u01/app/oracle/product/11.1.0/dbhome_2/network/admin/listener.ora
# Generated by Oracle configuration tools.

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 11g)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

SID_LIST_LISTENER=
  (SID_LIST=
      (SID_DESC=
         (SID_NAME=dg4msql)
         (ORACLE_HOME=/u01/app/oracle/product/11.1.0/dbhome_2)
         (PROGRAM=dg4msql)
      )
  (SID_DESC=
         (SID_NAME=dg4msql0)
         (ORACLE_HOME=/u01/app/oracle/product/11.1.0/dbhome_2)
         (PROGRAM=dg4msql)
      )
  )

3.在tnsnames.ora里面配置多個

[oracle@11g admin]$ cat tnsnames.ora 
LISTENER_ORCL =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.6.180)(PORT = 1521))
    )


orcl =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.6.180)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  )

orcl_dg =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.6.181)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl_dg)
    )
)

dg4msql  =
  (DESCRIPTION=
    (ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))
    (CONNECT_DATA=(SID=dg4msql))
    (HS=OK)
  )

dg4msql0  =
  (DESCRIPTION=
    (ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))
    (CONNECT_DATA=(SID=dg4msql0))
    (HS=OK)
  )
[oracle@11g admin]$

4.創建dblink進行查詢

--重啟監聽
lsnrctl stop
lsnrctl start

創建dblink
sqlplus / as sysdba
SQL> create PUBLIC database link sqlserver0 connect to "sa" identified by "scott1!"  using 'dg4msql0';
Database link created.
SQL> SELECT * FROM test2@sqlserver0;
        id name
---------- --------------------
         1 liang
         2 xue
         3 bai
SQL>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM