暂无图片
暂无图片
2
暂无图片
暂无图片
暂无图片

【生产篇】_Oracle如何访问MySql_透明网关

原创 杨磊 2019-11-01
2607

【前言】
最近碰到一个问题,随着公司MySQL数开源库的推广使用,以及一些系统从oracle 迁移至mysql ,这时会面临一个问题:应用的一部分数据访问在oracle端,一部分数据在MySQL端,这种情况下如何做关联查询。一个方法是把其中一部分数据冗余一份到另外一个库中做表关联查询;另一个方法为异构数据库间的关联查询。
本文使用方式2,通过oracle,本身的透明网关验证了oracle库访问MySQL库的数据的可行性。

【官方文档参考】
 Detailed Overview of Connecting Oracle to MySQL Using DG4ODBC DatabaseLink [ID 1320645.1]
适用范围:
MySQL Connectors - Version 3.51 to 6.8 [Release 3.51 to 6.8]
Oracle Database - Enterprise Edition - Version 11.1.0.6 to 12.1.0.2 [Release 11.1 to 12.1]
MySQL Server - Version 5.5 to 5.7 [Release 5.5 to 5.7]

Information in this document applies to any platform.ODBC, UnixODBC, DG4ODBC, MySQL, ODBCINI, Database Link
 
【环境说明】
LINUX 64 BIT Oracle 11.2.0.3    ip:192.168.0.100
LINUX 64 BIT MYSQL    5.7.19      ip:192.168.0.101
Oracle 通过透明网关链接mysql顺序(oracle和mysql之间双向打通)
[ORACLE] <—> [DG4ODBC] <—> [ODBC Driver Manager] <—> [ODBC Driver] <—>  [NETWORK]<—> [MySQL Server Host]<—> [MySQL Server]

1. 登录mysql服务器端(192.168.0.101)
MYSQLserver 端建立一个用户用于ORACLE DBLINK
[mysql@dataserver03 ~]$ /mysqlsoft/mysql/bin/mysql -uroot –pXXXXXXXXXXmysql: [Warning] Using a password on the command line interface can be insecure.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 977044Server version: 5.7.19-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial)

mysql> create user ‘fin’@’%’ identified by ‘mysql’;mysql> grant select,update on schema.* to ‘fin’@’%’;

2. 配置前要保证mysql数据库可以远程连接。
a) 即为oracle服务器和mysql服务器之间的3306端口和1521端口互为开通;
b) 使用telnet命令即可验证。

3. 11gR2 默认安装了透明网关检查一下
Oracle服务器端(ip:192.168.0.101)
-bash-4.1$ dg4odbcOracle Corporation — MONDAY JUN 17 2019 22:02:05.111 Heterogeneous Agent Release 11.2.0.3.0 - 64bit Production Built withOracle Database Gateway for ODBC
 PS:dg4odbc 是database gateway for odbc 的简称

4. 安装 MYSQL-connector
下载Mysql的odbc驱动,下载时要下载符合自己服务器版本的驱动包Connector-ODBC。下载链接:http://dev.mysql.com/downloads/connector/odbc/ Oracle服务器为Linux64位,故下载的是:mysql-connector-odbc-5.3.10-linux-glibc2.12-x86-64bit.tar.gz
 [root@elearningtest ~]Tar –xzvf mysql-connector-odbc-5.3.10-linux-glibc2.12-x86-64bit.tar.gz
依赖包主要是,可通过yum或者rpm方式进行安装
mysql-connector*.x86_64.rpmunixODBC*.x86_64.rpmunixODBC*.i386.rpm

5. 配置odbc配置信息
默认就在/etc/下建立odbc.ini;配置如下:
[root@elearningtest ~]#vi /etc/odbc.ini

[mysql_server]                 #连接名
    Driver = /usr/lib64/libmyodbc5w.so    #安装mysql的odbc驱动后会有对应版本的文件
Server= 192.168.0.101          #mysql服务器ip
Port= 3306                    #MYSQL服务器服务端口
User= fin                     #建立的MYSQL用户
Password= mysql                 #密码
Database= schema               #待访问的数据

6. 测试链接
[root@roseha1 etc]# isql mysql_server

±--------------------------------------+
| Connected!                            ||                                       || sql-statement                         || help [tablename]                      || quit                                  ||                                       |±--------------------------------------+成功

7. 在oracle端创建mysql的虚拟实例
[orared@roseha1 admin] pwd/home/orared/product/11.2.4/hs/admin     建立配置文件格式为init.ora这个sid就是listener中配置的sid;备注:#如果是拷贝ORACLE_HOME/hs/admin/initdg4odbc.ora这个文件,要把该文件的内容#都注释掉,最后的文件名为initmysqlServer.ora.使用tail -200f打开trace,正式使用需要关系trace日志在/oracle/product/11.2.0/db_1/hs/log/initmysqlServer_agt_130782.trc用于排错
 
-bash-4.1$ id
uid=500(oracle) gid=500(oinstall) groups=500(oinstall),501(dba)
-bash-4.1$ pwd
/oracle/product/11.2.0/db_1/hs/admin
 
-bash-4.1$ vi initmysqlServer
HS_FDS_CONNECT_INFO=mysql_192.168.0.101
HS_FDS_TRACE_LEVEL=debug
HS_FDS_SUPPORT_STATISTICS=FALSE
HS_FDS_SHAREABLE_NAME=/oracle/product/11.2.0/db_1/lib/libodbc.so
HS_LANGUAGE=AMERICAN_AMERICA.UTF8
HS_NLS_NCHAR=UCS2
set ODBCINI=/etc/odbc.ini
 
NOTE:
    1、 HS_FDS_SHAREABLE_NAME must point to the [ODBC Driver Manager] library. It is an error to put there the [ODBC Driver]
   2、 因为ODBC driver使用的是/usr/lib64/libmyodbc5w.so 的Unicode编码,then HS_NLS_NCHAR 必须为 UCS2,否则最终创建dblink后汇报如下错误:

image.png

8. oracle端为mysql虚拟实例配置监听
为不影响现有的监听,逻辑上进行分开管理,新建了一个静态监听LISTENER2
配置如下:
LISTENER2= (DESCRIPTION= (ADDRESS_LIST= (ADDRESS=(PROTOCOL=tcp)(HOST=192.168.0.100)(PORT=1521)) (ADDRESS=(PROTOCOL=ipc)(KEY=extproc)))) SID_LIST_LISTENER2= (SID_LIST= (SID_DESC= (SID_NAME=mysqlServer) (ORACLE_HOME=/oracle/product/11.2.0/db_1) (PROGRAM = dg4odbc) ) )

9.配置TNSNEAMS.ORA
我这里配置如下注意HS=OK不能少
mysqlServer=
  (DESCRIPTION =
     (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.100)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
       (SID=mysqlServer)
    )
    (HS = OK)
  )

10. 启动ORACLE端的listener2监听服务
[oracle@web admin]$lsnrctl start listener2

11. 测试mysql服务
[oracle@web admin]$ tnsping mysqlServer
PS:至此,Oracle服务器端就模拟了一个能连通MySQL的虚拟instance:mysqlServer
12. 建立Oracle本地实例访问mysqlServer实例的DBlink
sql> create  database link oracle_to_mysql connect to “fin” identified by “mysql” using ’ mysqlServer’;

12.oracle连接mysql数据库验证
Oracle 端创建访问mysql的用户fin_ora
-bash-4.1$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.3.0 Production
Copyright © 1982, 2011, Oracle.  All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
 
1、 创建fin_ora用户

SQL>create user fin_ora identified by oracle;SQL>grant connect,resource to fin_ora;
使用fin_ora用户登录oracle服务器

-bash-4.1$ sqlplus fin_ora/oracle
SQLPlus: Release 11.2.0.3.0 Production
 Copyright © 1982, 2011, Oracle.  All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
 
2、 验证是否整查询到mysql服务器中的数据
SQL> select count(
) from “a_parentdep”@oracle_to_mysql;
返回mysql端数据。
 
NOTE:
配置过程遇到一些错误,打开HS_FDS_TRACE_LEVEL = debug 能够很好的找到错误来源,然后解决。另,mysql数据库的字段名和表名都必须用双引号"引起来!!!
结语:
    1. 通过Oracle本身的透明网关,能实现Oracle和Mysql之间的数据联通,但增删改查不能操作。
    2. 基于上述原因,项目组在Oracle迁移MySql后,如有和异步库间的访问,需慎重考虑。
    3. 市面上有一些第三方工具可实现异步间数据互通,但一般涉及费用,非长久之计,且第三方工具的引入也带来一些不稳定因素,需进行一些监控手段。

[官方文献] Detailed Overview of Connecting Oracle to MySQL Using DG4ODBC DatabaseLink [ID 1320645.1]

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论