程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> Oracle數據庫基礎 >> Oracle10g中通過DBLink訪問MySQL數據

Oracle10g中通過DBLink訪問MySQL數據

編輯:Oracle數據庫基礎

前一陣在公司通過Oracle訪問MySQL,測試環境:CentOS5_X64, Oracle10g_X64, MySQL5 。
把一些經驗分享給大家!

1, 首先在Oracle所在計算機安裝MySQL的ClIEnt端軟件,並且x86_64和i386版本的都需要安裝,以便可以連接MySQL數據庫。查看ClIEnt安裝情況:
rpm -qa |grep MySQL
MySQL-5.0.45-7.el5
MySQL-5.0.45-7.el5

得到兩條記錄,一條是x86_64的,一條是i386的。
如果看到還沒有安裝MySQL客戶端軟件,則需要安裝:
yum install MySQL
yum install MySQL.i386

驗證在此Oracle所在計算機是可以連接目標主機MySQL數據庫:
mysql -h 192.168.1.1 -u root -p MySQL

2, 檢查Oracle所在計算機是否已安裝MySQL ODBC客戶端,並且x86_64和i386版本的都需要安裝。
rpm -qa |grep MySQL-connect
如果沒有安裝MySQL-connector-odbc,則用下面命令下載和安裝:
下載 MySQL-connector-odbc:
wget FTP://mirror.switch.ch/pool/3/mirror/centos/5.2/os/i386/CentO S/MySQL-connector-odbc-3.51.12-2.2.i386.rpm
安裝MySQL-connector-odbc:
rpm -ivh MySQL-connector-odbc-3.51.12-2.2.i386.rpm
得到提示 libltdl.so.3 is needed by MySQL-connector-odbc-3.51.12-2.2.i386 ,發現需要安裝libtool的i386版本,因此通過如下命令安裝libtool-ltdl.i386:
yum list *.i386|grep libtool
yum install libtool-ltdl.i386

3, 在Oracle所在計算機編輯/etc/odbc.ini文件,測試ODBC工作
vi /etc/odbc.ini
#odbc.ini內容如下
[test]
Driver=/usr/lib64/libmyodbc3.so
Description=MySQL
Server=192.168.1.1(MySQL Server IP)
Port=3306
User= (MySQL Username)
UID= (MySQL Username)
PassWord= (MySQL PWD)
Database= (MySQL Database Name)
Option=3
Socket=
在命令行中執行下列命令,應能夠順利登入MySQL ClIEnt窗口,即證明ODBC功能正常:
isql -v test
quit

4, 編輯hs配置文件 vi /ora10g/hs/admin/inittest.ora(注意文件名中藍色部分為odbc.ini中藍色名稱)
HS_FDS_CONNECT_INFO = test
HS_FDS_TRACE_LEVEL = on(正式使用後,不需要排錯的時候應設為off)
HS_FDS_TRACE_FILE_NAME = test.trc
HS_FDS_SHAREABLE_NAME=/usr/lib/libmyodbc3.so
set ODBCINI=/etc/odbc.ini

5, 編輯Oracle所在計算機的Oracle listener的配置文件,建立一個模擬Oracle Listener的監聽方式,為將來建立dblink做准備:
vi /ora10g/network/admin/listener.ora 加入如下語句:
(SID_DESC =
   (SID_NAME = test)
   (Oracle_HOME = /ora10g)
   (PROGRAM = hsodbc)
   (ENVS=LD_LIBRARY_PATH=/ora10g/lib32:/usr/lib64:/ora10g/lib)
)
listener.ora文件現在的內容變成:
SID_LIST_LISTENER = (
     SID_LIST =
        (SID_DESC =
           (Oracle_HOME = /ora10g) 
           (PROGRAM = extproc) 
           (GLOBAL_DBNAME=prod) 
           (SID_NAME=prod)
         ) 
        (SID_DESC =
                  (SID_NAME = test)
                  (Oracle_HOME = /ora10g)
                  (PROGRAM = hsodbc)
                  (ENVS=LD_LIBRARY_PATH=/ora10g/lib32:/usr/lib64:/ora10g/lib) 
             )
)
LISTENER = (
     DESCRIPTION_LIST =
        (DESCRIPTION = (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
                                 (ADDRESS = (PROTOCOL = TCP)(HOST = 0.0.0.0)(PORT = 1521)) )
)
執行lsnrctl reload使Listener生效:
su – Oracle
lsnrctl reload
LSNRCTL for Linux: Version 10.2.0.4.0 - Production on 09-FEB-2009 13:59:38 Copyright (c) 1991, 2007, Oracle.
All rights reserved. Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1)))
The command completed successfully
lsnrctl status
LSNRCTL for Linux: Version 10.2.0.4.0 - Production on 12-FEB-2009 08:56:00

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

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 10.2.0.4.0 - Production
Start Date                03-JAN-2009 03:47:39
Uptime                    40 days 5 hr. 8 min. 20 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /ora10g/network/admin/listener.ora
Listener Log File         /ora10g/network/log/listener.log
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=0.0.0.0)(PORT=1521)))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
  Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
Service "test" has 1 instance(s).
  Instance "test", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully

6, 編輯Oracle所在計算機中的tnsnames.ora文件,便於建立dblink。注意,此tnsnames的配置可以支持tnsping,但是不能支持sqlplus登錄,只用於dblink:
vi /ora10g/network/admin/tnsnames.ora 
test =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
    (CONNECT_DATA =
      (SID = test)
    )
      (HS = OK)
  )

7, 在Oracle Database建立dblink:
create public database link MySQL
connect to "mysql username" identifIEd by "MySQL pwd"
using '(DESCRIPTION =
              (ADDRESS = (PROTOCOL = TCP) (HOST = 127.0.0.1) (PORT =1521) )
              (CONNECT_DATA = (SID= test) )
              (HS=OK)
           )';
要注意用戶名和密碼處需要用雙引號,否則Oracle所傳輸的都是大寫字母,可能無法登錄進入MySQL。

8, 由於MySQL中的表名的大小寫敏感,因此需要在進行SQL查詢時對表名用雙引號擴起來
select * from "tablename"@test

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved