Oracle access MySql via unixODBC by DBLINK

原来公司的同事询问了一个问题:
我问一下oracle连接远程mysql的方法 我之前用hsodbc 但是不能显示不同属性的字段
oracle好像也没for mysql的透明网关  请问有什么方法能远程获取mysql的数据? 我oracle专门做分析的 会调用mysql的数据 所以就要获取mysql的数据 又不想做同步
------------、

介个你懂吗



对MySql了解仅限于通用的SQL语句,其他完全不懂;但是我知道至少有超过2中以上的办法来实现这个需求。

他这里应该是借助unixODBC来访问MySql的数据,以下是测试过程。
环境:
MySql5.6 For Windows2003
Oracle10G For CentOS5.7

0. MySql安装部署数据准备
MySql在windows2003上的安装部署非常简单,简单的一步一步next就好了,这里略过,我们将创建一个数据库用户gtlions,创建一个表access_by_oracle并插入几条测试数据。
C:\Documents and Settings\Administrator>mysql -u root -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.10-log MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> help

For information about MySQL products and services, visit:
   http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
   http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
   https://shop.mysql.com/

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
notee     (\t) Don't write into outfile.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog
with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.

For server side help, type 'help contents'
mysql> CREATE USER 'gtlions'@'%' IDENTIFIED BY '000000';
ERROR 1396 (HY000): Operation CREATE USER failed for 'gtlions'@'%'
mysql> CREATE USER 'gtlions' IDENTIFIED BY '000000';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT USAGE ON * . * TO 'gtlions' IDENTIFIED BY '000000' WITH MAX_QUERIES
_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIO
NS 0 ;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE DATABASE IF NOT EXISTS `gtlions` ;
Query OK, 1 row affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON `gtlions` . * TO 'gtlions';
Query OK, 0 rows affected (0.00 sec)

mysql>exit
Bye
C:\Documents and Settings\Administrator>mysql -u gtlions -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 5.6.10-log MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use gtlions
Database changed
mysql> create table access_by_oracle(id int(5),name char(20));
Query OK, 0 rows affected (0.09 sec)

mysql> insert into access_by_oracle values(1,'gtlions');
Query OK, 1 row affected (0.00 sec)

mysql> insert into access_by_oracle values(2,'laidye');
Query OK, 1 row affected (0.00 sec)

mysql> select * from access_by_oracle;
+------+---------+
| id   | name    |
+------+---------+
|    1 | gtlions |
|    2 | laidye  |
+------+---------+
2 rows in set (0.00 sec)

mysql>

好了,MySql已经安装部署完毕并准备可以对外访问了。

1. Oracle访问端配置
我们需要借助unixODBC来访问,如果系统有自带安装好的就可以直接使用,如果没有就要安装它,可以使用yum或者源码包的方式,由于我这里系统已经安装好了,不再描述,如果需要yum安装它,命令参考如下:
yum -y unixODBC
我本机上已经安装完成的如下包:
[root@gtserdev ~]# rpm -qa|grep unixODBC
unixODBC-devel-2.2.11-10.el5
unixODBC-libs-2.2.11-10.el5
unixODBC-2.2.11-10.el5
unixODBC-kde-2.2.11-10.el5
安装完成后编辑/etc/odbc.ini,内容如下:
[oracle@gtserdev admin]$ cat /etc/odbc.ini
[test]
Driver=/usr/lib/libmyodbc3_r.so
Description=MySQL
Server=192.168.56.101
Port=3306
User=gtlions
Password=000000
Database=gtlions
Option=3
Socket=
上述内容第3行开始为MySql服务器地址、端口、用户、密码、数据库,酌情修改吧。
然后执行下列命令进行测试,如果能够顺利登入MySQL Client窗口,说明我们的unixODBC功能可以正常:
[root@gtserdev ~]# isql -v test
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL>
我们可以使用select查询下上面我们在MySql当中创建的表access_by_oracle中的数据:
SQL> select * from access_by_oracle;
+-----------+---------------------+
| id        | name                |
+-----------+---------------------+
| 1         | gtlions             |
| 2         | laidye              |
+-----------+---------------------+
SQLRowCount returns 2
2 rows fetched
这是我们预期的结果,一切正常。
接下来我们就要配置能够在Oracle当中直接访问MySql的数据了。
首先配置数据访问连接文件:
[oracle@gtserdev ~]$ vi $ORACLE_HOME/hs/admin/inittest.ora
[oracle@gtserdev admin]$ cat $ORACLE_HOME/hs/admin/inittest.ora
# this is a sample agent init file that contains the HS parameters that are
# needed for an ODBC Agent.
 
#
# HS init parameters
#
HS_FDS_CONNECT_INFO = test
HS_FDS_TRACE_LEVEL = off
HS_FDS_SHAREABLE_NAME = /usr/lib/libodbc.so
#
# ODBC specific environment variables
#
set ODBCINI=/etc/odbc.ini
#

修改监听文件,添加如下语句:
  (SID_DESC =
      (PROGRAM = hsodbc)
      (ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)
      (SID_NAME = test)
      (ENVS=LD_LIBRARY_PATH = /usr/lib:/u01/app/oracle/product/10.2.0/db_1/lib)
    )
修改之后结果如下:
[oracle@gtserdev admin]$ cat listener.ora
# listener.ora Network Configuration File: /u01/app/oracle/product/10.2.0/db_1/network/admin/listener.ora
# Generated by Oracle configuration tools.

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = PLSExtProc)
      (ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)
      (PROGRAM = extproc)
    )
  (SID_DESC =
      (PROGRAM = hsodbc)
      (ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)
      (SID_NAME = test)
      (ENVS=LD_LIBRARY_PATH = /usr/lib:/u01/app/oracle/product/10.2.0/db_1/lib)
    )
  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = gtserdev)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
    )
  )
配置连接串:
[oracle@gtserdev admin]$ more $ORACLE_HOME/network/admin/tnsnames.ora
# tnsnames.ora Network Configuration File: /u01/app/oracle/product/10.2.0/db_1/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
test =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS =
        (PROTOCOL = TCP)
        (HOST = 127.0.0.1)
        (PORT = 1521)
      )  
    )     
    (CONNECT_DATA =
      (SID= test)
    )
    (HS=OK)  
  )

然后启动监听和数据,创建链接并测试:
ODBCINI=/etc/odbc.ini; export ODBCINI
ODBCSYSINI=/etc; export ODBCSYSINI
[oracle@gtserdev admin]$ lsnrctl stop

LSNRCTL for Linux: Version 10.2.0.5.0 - Production on 26-MAR-2013 17:12:09

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

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=gtserdev)(PORT=1521)))
The command completed successfully
[oracle@gtserdev admin]$ lsnrctl start

LSNRCTL for Linux: Version 10.2.0.5.0 - Production on 26-MAR-2013 17:12:19

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

Starting /u01/app/oracle/product/10.2.0/db_1/bin/tnslsnr: please wait...

TNSLSNR for Linux: Version 10.2.0.5.0 - Production
System parameter file is /u01/app/oracle/product/10.2.0/db_1/network/admin/listener.ora
Log messages written to /u01/app/oracle/product/10.2.0/db_1/network/log/listener.log
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=gtserdev)(PORT=1521)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC0)))

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=gtserdev)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 10.2.0.5.0 - Production
Start Date                26-MAR-2013 17:12:19
Uptime                    0 days 0 hr. 0 min. 0 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/10.2.0/db_1/network/admin/listener.ora
Listener Log File         /u01/app/oracle/product/10.2.0/db_1/network/log/listener.log
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=gtserdev)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC0)))
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
[oracle@gtserdev admin]$ tnsping test

TNS Ping Utility for Linux: Version 10.2.0.5.0 - Production on 26-MAR-2013 17:20:03

Copyright (c) 1997,  2010, Oracle.  All rights reserved.

Used parameter files:


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (HOST = 127.0.0.1) (PORT = 1521))) (CONNECT_DATA = (SID= test)) (HS=OK))
OK (0 msec)
[oracle@gtserdev admin]$ sqlplus "/as sysdba"

SQL*Plus: Release 10.2.0.5.0 - Production on Tue Mar 26 16:17:37 2013

Copyright (c) 1982, 2010, Oracle.  All Rights Reserved.

Connected to an idle instance.

SQL> startup
ORACLE instance started.

Total System Global Area  167772160 bytes
Fixed Size                  1272600 bytes
Variable Size              62915816 bytes
Database Buffers          100663296 bytes
Redo Buffers                2920448 bytes
Database mounted.
Database opened.

SQL> drop database link mysql;

Database link dropped.

SQL> CREATE PUBLIC DATABASE LINK mysql CONNECT TO "gtlions" IDENTIFIED BY "000000" USING 'TEST';

Database link created.

SQL> select * from "access_by_oracle"@mysql;

        id name
---------- --------------------
         1 gtlions
         2 laidye
-EOF-        

你可能感兴趣的:(Oracle access MySql via unixODBC by DBLINK)