Oracle10g 中通過DBLink 訪問MySQL 資料

來源:互聯網
上載者:User

前一陣在公司通過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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.