針對oracle資料庫不同例項之間的資料訪問,我們可以直接通過dblink訪問,如果oracle資料庫想訪問mysql/sqlserver等資料庫的資料,我們可以通過配置oracle透明閘道器實現異構資料庫dblink訪問。
好久沒做透明閘道器的配置了,最近有業務需求,這裡將部署過程做個記錄,希望對有需要的朋友有所幫助。
一、Oracle資料庫通過透明閘道器訪問MySQL資料庫環境說明
RHEL6.6 oracle 11.2.0.4
RHEL6.6 MySQL5.7
odbc
二、資料訪問流程
oracle——dg4odbc——odbc——mysql
三、Oracle透明閘道器(MySQL)安裝
oracle 11.2.0.4預設安裝了odbc透明閘道器
驗證:
[oracle@test ~]$ cd $ORACLE_HOME/hs
[oracle@test hs]$ dg4odbc
Oracle Corporation --- FRIDAY APR 27 2018 10:07:44.375 Heterogeneous Agent Release 11.2.0.4.0 - 64bit Production Built with Oracle Database Gateway for ODBC
##database gateway for odbc 簡稱 dg4odbc
四、mysql-connector安裝
下載:
https://dev.mysql.com/downloads/connector/odbc/
https://dev.mysql.com/get/Downloads/Connector-ODBC/8.0/mysql-connector-odbc-8.0.11-1.el6.x86_64.rpm
安裝:
[root@test ~]# rpm -ivh mysql-connector-odbc-8.0.11-1.el6.x86_64.rpm warning: mysql-connector-odbc-8.0.11-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparing... ########################################### [100%] 1:mysql-connector-odbc ########################################### [100%] Success: Usage count is 1 Success: Usage count is 1
依賴包安裝:
yum install unixODBC*
rpm -qa |grep unixODBC
unixODBC-devel-2.2.14-14.el6.x86_64
unixODBC-2.2.14-14.el6.x86_64
五、ODBC配置
[root@test ~]# vi /etc/odbc.ini
[mysql_test] Description = ODBC for MySQL Driver = /usr/lib64/libmyodbc8w.so Server = mysql_ipaddr Port = 3306 User = dbtest Password = abcd1234 Database = test
六、MySQL資料庫建立賬號、授權並測試連通性
賬號建立: (root:localhost:Fri Apr 27 10:16:11 2018)[(none)]>create database test; (root:localhost:Fri Apr 27 10:16:22 2018)[(none)]>grant all on test.* to dbtest@'%' identified by 'abcd1234'; (root:localhost:Fri Apr 27 10:16:40 2018)[(none)]>flush privileges; 連通性測試: [root@test ~]# isql mysql_test +---------------------------------------+ | Connected! | | | | sql-statement | | help [tablename] | | quit | | | +---------------------------------------+ SQL>
七、Oracle資料庫相關配置
(1)hs透明閘道器配置
[oracle@test ~]$ cd $ORACLE_HOME/hs
[oracle@test hs]$ cd admin
[oracle@test admin]$ vi initmysql_test.ora
##HS Configuration HS_FDS_CONNECT_INFO = mysql_test HS_FDS_TRACE_LEVEL = debug HS_FDS_SHAREABLE_NAME = /usr/lib64/libodbc.so HS_FDS_SUPPORT_STATISTICS=FALSE HS_LANGUAGE=AMERICAN_AMERICA.zhs16gbk ##ODBC Configuration set ODBCINI=/etc/odbc.ini
##這裡配置的是資料庫例項名、odbc lib包,oracle資料庫字符集、odbc配置檔案路徑
(2)監聽配置
[oracle@test admin]$ vi /U01/app/oracle/product/11.2.0.4/network/admin/listener.ora
SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (GLOBAL_DBNAME = dbsid) (SID_NAME = dbsid) (ORACLE_HOME=/U01/app/oracle/product/11.2.0.4) ) (SID_DESC= (SID_NAME=mysql_test) (ORACLE_HOME=/U01/app/oracle/product/11.2.0.4) (PROGRAM=dg4odbc) ) ) LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.1.1)(PORT = 1521)) ) )
重啟監聽
lsnrctl stop
lsnrctl start
(3)tnsname配置
配置tnsname
[oracle@test admin]$ vi /U01/app/oracle/product/11.2.0.4/network/admin/tnsnames.ora
dbsid_mysql = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.1.1 )(PORT = 1521)) ) (CONNECT_DATA = (SID = mysql_test) ) (HS = OK) )
測試tnsname連線
[oracle@test admin]$ tnsping dbsid_mysql
TNS Ping Utility for Linux: Version 11.2.0.4.0 - Production on 27-APR-2018 12:17:58 Copyright (c) 1997, 2013, Oracle. All rights reserved. Used parameter files: Used TNSNAMES adapter to resolve the alias Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.1.1)(PORT = 1521))) (CONNECT_DATA = (SID = mysql_test)) (HS = OK)) OK (0 msec)
八、dblink建立以及資料訪問測試
SQL>create PUBLIC DATABASE LINK dlk connect to "dbtest" identified by "abcd1234" using 'dbsid_mysql';
SQL> select * from "t1"@dlk;
id
----------
10
11
SQL> insert into "t1"@dlk values(30);
1 row created.
九、錯誤資訊以及處理方法
(1)錯誤01
錯誤資訊:
SQL> select * from t1@dlk; select * from t1@dlk * ERROR at line 1: ORA-28500: connection from ORACLE to a non-Oracle system returned this message: ORA-02063: preceding line from DLK
錯誤原因以及處理方法:hs/admin/init[sid].ora裡配置的HS_FDS_SHAREABLE_NAME = /usr/lib64/libodbc.so不正確,應該是odbc的Lib包
(2)錯誤02
錯誤資訊:
SQL> select * from "t1"@dlk; select * from "t1"@dlk * ERROR at line 1: ORA-28500: connection from ORACLE to a non-Oracle system returned this message: [
錯誤原因以及處理方法:hs/admin/init[sid].ora裡配置的HS_LANGUAGE=AMERICAN_AMERICA.zhs16gbk字符集不正確,應該是oracle資料庫字符集
(3)錯誤03:
錯誤資訊:
SQL> select * from t1@dlk; select * from t1@dlk * ERROR at line 1: ORA-00942: table or view does not exist [MySQL][ODBC 8.0(w) Driver][mysqld-5.7.18-log]Table 'test.T1' doesn't exist {42S02,NativeErr = 1146} ORA-02063: preceding 2 lines from DLK
錯誤原因以及處理方法:執行的查詢操作,表名需要帶雙引號,因為mysql預設表名是區分大小寫,而oracle是不區分大小寫的 select * from "t1"@dlk;
(4)參考文件
https://blog.csdn.net/u012514278/article/details/51741698
http://blog.itpub.net/7728585/viewspace-2128158/
http://www.docin.com/p-113642416.html