[Oracle -> MySQL]  Oracle通过dblink连接MySQL 



业务上有这么一个需求,需要把Oracle的一些数据同步到MySQL,如果每次都是手动同步的话,实在太麻烦,因此花了点时间研究了下Oracle直连MySQL的方式。

参考文档:Detailed Overview of Connecting Oracle to MySQL Using DG4ODBC Database Link (Doc ID 1320645.1)

版本信息:

Oracle: 11.2.0.1.0     OS: CentOS 5.9

MySQL: 5.5.27          OS: CentOS 5.8

原理:

Oracle使用DG4ODBC数据网关连接其它非Oracle数据库,其原理图如下:

从上图可知,Oracle连接MySQL需要涉及到如下组件:DG4ODBC, ODBC Driver Manager, ODBC Driver,本文将一一讲解它们的配置。

1)判断32位还是64位

因为32位和64位的配置不一样,64位更复杂一些,因此我们首先得确定Oracle和DG4ODBC是32位还是64位:

[plain] view plain copy   print?
  1. [oracle@lx16 ~]$ file $ORACLE_HOME/bin/dg4odbc
  2. /home/oracle/app/oracle/product/11.2.0/dbhome_1/bin/dg4odbc: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), not stripped

从上面的输出可知是64位。

2)下载并安装ODBC Driver Manager

到这个页面(http://www.unixodbc.org/download.html)根据你的OS下载unixodbc(注意:版本不能低于2.2.14)

[plain] view plain copy   print?
  1. $ wget http://sourceforge.net/projects/unixodbc/files/unixODBC/2.2.14/unixODBC-2.2.14-linux-x86-64.tar.gz/download

解压缩:

[plain] view plain copy   print?
  1. $ tar -zxvf unixODBC-2.2.14-linux-x86-64.tar.gz

解压缩后会在当前目录下自动创建usr的目录,我们创建一个目录(~/app/unixodbc-2.2.14)用于放置unixodbc,然后把usr 迁移到该目录下:

[plain] view plain copy   print?
  1. $ mkdir ~/app/unixodbc-2.2.14
  2. $ mv usr ~/app/unixodbc-2.2.14

3)下载并按照ODBC Driver for MySQL

到这个页面(http://dev.mysql.com/downloads/connector/odbc/5.2.html#downloads)根据你的OS下载ODBC-5.2.5,本例选择64位tar版本:

[plain] view plain copy   print?
  1. $ wget http://dev.mysql.com/get/Downloads/Connector-ODBC/5.2/mysql-connector-odbc-5.2.5-linux-glibc2.5-x86-64bit.tar.gz/from/http://cdn.mysql.com/
  2. $ tar -zxvf mysql-connector-odbc-5.2.5-linux-glibc2.5-x86-64bit.tar.gz

解压缩成功后是一个文件夹,把该文件夹迁移至~/app目录下,并给它创建一个软链接:

[plain] view plain copy   print?
  1. $ mv mysql-connector-odbc-5.2.5-linux-glibc2.5-x86-64bit ~/app
  2. $ cd ~/app
  3. $ ln -s mysql-connector-odbc-5.2.5-linux-glibc2.5-x86-64bit myodbc-5.2.5

4)配置ODBC Driver

在~/etc目录下创建odbc.ini如下:

[plain] view plain copy   print?
  1. [myodbc5]
  2. Driver = /home/oracle/app/myodbc-5.2.5/lib/libmyodbc5w.so
  3. Description = Connector/ODBC 5.2 Driver DSN
  4. SERVER = 192.168.1.15
  5. PORT = 3306
  6. USER = mysql_user
  7. PASSWORD = mysql_pwd
  8. DATABASE = mysql_db
  9. OPTION = 0
  10. TRACE = OFF

其中,Driver指向第3步上按照的ODBC Driver,这里要特别注意:MySQL的Datbase是大小写敏感的。

5)验证ODBC连接

[plain] view plain copy   print?
  1. $ export ODBCINI=/home/oracle/etc/odbc.ini
  2. $ export LD_LIBRARY_PATH=/home/oracle/app/unixodbc-2.2.14/usr/local/lib:$LD_LIBRARY_PATH
  3. $ cd ~/app/unixodbc-2.2.14/usr/local/bin
  4. $ ./isql myodbc5 -v
  5. +---------------------------------------+
  6. | Connected!                            |
  7. |                                       |
  8. | sql-statement                         |
  9. | help [tablename]                      |
  10. | quit                                  |
  11. |                                       |
  12. +---------------------------------------+

上面显示连接成功。

6)配置tnsnames.ora

[plain] view plain copy   print?
  1. myodbc5 =
  2. (DESCRIPTION=
  3. (ADDRESS=
  4. (PROTOCOL=TCP) (HOST=localhost) (PORT=1521)
  5. )
  6. (CONNECT_DATA=
  7. (SID=myodbc5)
  8. )
  9. (HS=OK)
  10. )

7)配置listener.ora

[plain] view plain copy   print?
  1. SID_LIST_LISTENER=
  2. (SID_LIST=
  3. (SID_DESC=
  4. (SID_NAME=myodbc5)
  5. (ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/dbhome_1)
  6. (PROGRAM=dg4odbc)
  7. (ENVS=LD_LIBRARY_PATH=/home/oracle/app/unixodbc-2.2.14/usr/local/lib:/home/oracle/app/oracle/product/11.2.0/dbhome_1/lib)
  8. )
  9. )

如上所示,为了避免和其它已存在的ODBC Driver Manager冲突,强烈设置LD_LIBRARY_PATH在listener.ora
8)创建init.ora文件
创建文件$ORACLE_HOME/hs/admin/initmyodbc5.ora,内容如下:

[sql] view plain copy   print?
  1. HS_FDS_CONNECT_INFO=myodbc5 # Data source name in odbc.ini
  2. HS_FDS_SHAREABLE_NAME=/home/oracle/app/unixodbc-2.2.14/usr/local/lib/libodbc.so
  3. HS_FDS_SUPPORT_STATISTICS=FALSE
  4. HS_LANGUAGE=AMERICAN_AMERICA.WE8ISO8859P15
  5. # ODBC env variables
  6. set ODBCINI=/home/oracle/etc/odbc.ini

9)使上述配置文件生效

[sql] view plain copy   print?
  1. $ lsnrctl reload
  2. $ lsnrctl status
  3. Service "myodbc5" has 1 instance(s).
  4. Instance "myodbc5", status UNKNOWN, has 1 handler(s) for this service...

10)验证配置是否正确

[sql] view plain copy   print?
  1. $ tnsping myodbc5
  2. TNS Ping Utility for Linux: Version 11.2.0.1.0 - Production on 29-AUG-2013 10:54:46
  3. Copyright (c) 1997, 2009, Oracle.  All rights reserved.
  4. Used parameter files:
  5. /home/oracle/app/oracle/product/11.2.0/dbhome_1/network/admin/sqlnet.ora
  6. Used TNSNAMES adapter to resolve the alias
  7. Attempting to contact (DESCRIPTION= (ADDRESS= (PROTOCOL=TCP) (HOST=localhost) (PORT=1521)) (CONNECT_DATA= (SID=myodbc5)) (HS=OK))
  8. OK (0 msec)

11)创建dblink

[sql] view plain copy   print?
  1. SQL> create public database link mysqltest connect to "mysql_user" identified by "mysql_pwd" using 'myodbc5';
  2. SQL> select count(*) from trans_expert_map@mysqltest;
  3. COUNT(*)
  4. ----------
  5. 371


Detailed Overview of Connecting Oracle to MySQL Using DG4ODBC Database Link (文档 ID 1320645.1)

In this Document

Goal
Solution
References

APPLIES TO:

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

GOAL

How to connect Oracle to MySQL Server through ODBC database link

SOLUTION

Oracle database server uses DG4ODBC as the data gateway for connecting to non-Oracle RDBMS. This requires the proper setup for all components involved in transferring data from Oracle to MySQL and back. These components interact with each other in the following way:

+--------------------------------------------------------+

| |

| [ORACLE] <---> [DG4ODBC] <---> [ODBC Driver Manager] <---> [ODBC Driver] |

| |

+--------------------------------------------------------------------------+

/|\

|

NETWORK

|

\|/

+----+

|                       |

| [MySQL Server] |

|                       |

+-----------------------+

In this article we will configure each component and diagnose problems that are most likely to be encountered during the setup process. NOTE: There are few articles in the internet that skip [ODBC Driver Manager] and connect directly [DG4ODBC] <- -=""> [ODBC Driver]. It might work for some

drivers such as DataDirect or specific configurations, but beware that MySQL [ODBC Driver] (versions 3.51.x and 5.1.x) is not supposed to be loaded directly. The recommended driver manager is UnixODBC v.2.2.14 or newer. Configuring ODBC connections in 32-bit OS might be slightly easier than in 64-bit OS. The latter can execute 32 and 64-bit code and more attention must be paid to the components versions. In other words, when configuring ODBC you cannot mix 32-bit and 64-bit components within the Client Host. This is so because 32-bit binaries code can only load 32-bit binaries and 64-bit binaries can only load 64-bit binaries. [MySQL Server] is always independent because all communication with the driver is done through the network protocol. You have the choice to place [MySQL Server] on or on another physical host in the network . In any case, the bit depth of [MySQL Server]

executable is not important.

Step 1

We shall start from determining if [Oracle] and [DG4ODBC] are 32/64-bits:

$ file $ORACLE_HOME/bin/dg4odbc

/home/dbs/app/Ora/product/11.2.0/dbhome_1/bin/dg4odbc: ELF 64-bit LSB

executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped

The above command output says that we must use 64-bit [ODBC Driver Manager] and 64-bit [ODBC Driver]

Step 2

Getting and installing  ODBC Driver Manager.

You should download and install the latest version of the UnixODBC driver manager from the site -

www.unixodbc.org

and click on the 'Download' option.
Follow the steps detailed there to install and configure the driver manager.

Step 3

Getting and installing [ODBC Driver].

Similar to UnixODBC, it is possible to have several different versions of MySQL Connector/ODBC driver. Installing the driver from tar.gz package does not require root privileges and allows installing the driver at custom locations, so we will do so.

More details about installing MySQL Connector/ODBC can be found here:

http://dev.mysql.com/doc/refman/5.5/en/connector-odbc-installation-binary-unix.html

Download the latest tar.gz package from the following page:

http://dev.mysql.com/downloads/connector/odbc/#downloads

Unpack the driver into ~/app directory:

$ cd ~/app

$ gunzip -c .tar.gz | tar xvf -

This command creates the connector directory and extracts all needed files in it. Create a symbolic link with a shorter name:

$ ln -s myodbc-

The latest versions of all the MySQL software can be downloaded from -

https://edelivery.oracle.com/

Step 4

Configuring ODBC data source for MySQL Connector/ODBC driver is described here:

http://dev.mysql.com/doc/refman/5.5/en/connector-odbc-configuration-dsn-unix.html

So, we will create odbc.ini file in ~/etc:

[myodbc5]

Driver = /home/dbs/app/myodbc-x.x.x/lib/libmyodbc5.so

Description = Connector/ODBC x.x Driver DSN

SERVER = 10.0.0.1

PORT = 3306

USER = mysql_user

PASSWORD = *****

DATABASE = test

OPTION = 0

TRACE = OFF

+---------------------------------------------------------------------------+

| NOTE: Database names are case sensitive in MySQL, so mind what you put |

| in DATABASE parameter. 'DATABASE = test' and 'DATABASE = TEST' will point |

| to different databases. It may cause an error because of trying to use a |

| non-existing database. This error may occur in a system where file names |

| are case sensitive (Linux/Unix systems)                                                |

| Parameter names, however, are not case sensitive:                                |

| 'DATABASE = test' and 'database = test' are equal.                                  |

+---------------------------------------------------------------------------+

Step 5 (Optional)

Verifying the ODBC connection using isql command line.

$ export ODBCINI=/home/dbs/etc/odbc.ini

$ export LD_LIBRARY_PATH=/home/dbs/app/unixodbc-x.x.x/lib:$LD_LIBRARY_PATH

$ cd ~/app/unixodbc-x.x.x/bin/

$ ./isql myodbc5 -v

+---------------------------------------+

| Connected! |

| |

| sql-statement |

| help [tablename] |

| quit |

| |

+---------------------------------------+

The above output will display if connection has been established successfully. Next, trying to send a simple query to list tables in the database:

SQL> show tables;

+-----------------------------------------------------------------+

| Tables_in_test |

+-----------------------------------------------------------------+

| tab1 |

| tab2 |

+-----------------------------------------------------------------+

SQLRowCount returns 2

2 rows fetched

That was the good scenario when everything went smoothly. However, you might get the following errors:

Error 1:

[IM002][unixODBC][Driver Manager]Data source name not found,

no default driver specified

ISQL]ERROR: Could not SQLConnect

error usually comes if ODBCINI variable is not pointing to the correct

odbc.ini file. To fix this error try:

$ cat $ODBCINI

The command should display the contents of odbc.ini file with all settings we have configured on Step 4. If the file is there and the same error comes again, check the data source name. The parameter name for isql must be exactly the same as the section name in odbc.ini file.

Error 2:

./isql: error while loading shared libraries: libodbc.so.1: cannot open shared object file: No such file or directory This error means that LD_LIBRARY_PATH is set wrong and the linker cannot find the main UnixODBC [Driver Manager] library libodbc.so. The solution is to export the directory containing libodbc.so ito LD_LIBRARY_PATH env variable

as shown at the beginning of Step 5.

The following command must not show failing dependencies:

$ ldd isql

linux-vdso.so.1 => (0x00007fffe4ffc000)

libodbc.so.1 => /home/dbs/app/unixodbc-2.2.14/lib/libodbc.so.1

(0x00002ae5263e8000)

libdl.so.2 => /lib64/libdl.so.2 (0x00000036b1c00000)

libreadline.so.5 => /usr/lib64/libreadline.so.5 (0x00000036b1000000)

libncurses.so.5 => /usr/lib64/libncurses.so.5 (0x00000036c4400000)

libpthread.so.0 => /lib64/libpthread.so.0 (0x00000036b2000000)

libc.so.6 => /lib64/libc.so.6 (0x00000036b1400000)

/lib64/ld-linux-x86-64.so.2 (0x00000036b0c00000)

Error 3:

[S1000][unixODBC][MySQL][ODBC 5.1 Driver]Access denied for user 'dbs'@'%'

(using password: YES)

[ISQL]ERROR: Could not SQLConnect

In some cases this error message is not obvious. The user name and password might be correct and mysql command line might work perfectly with the user name and password specified in odbc.ini file. Check the database name in odbc.ini file (DATABASE parameter). As explained in the note on Step 4, the database names are case sensitive in MySQL and the connection might be rejected because the database does not exist or user has no privileges to access a database with this name.

Checking the connectivity from mysql command line is a good idea too.

+---------------------------------------------------------------------------+

| NOTE: mysql command line does not use ODBC, so it might work even if ODBC |

| fails. The idea of this check is to make sure we provided the correct                |

| connection credentials to ODBC driver.                                                         |

+---------------------------------------------------------------------------+

mysql command line should use EXACTLY the same user name, host, password, port and be executed on the same host with [Oracle] and [DG4ODBC]:

$ mysql "user=mysql_user "password=****** --host=10.0.0.1 --port=3306

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.1.52-community-log MySQL Community Server (GPL)

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

This software comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to modify and redistribute it under the GPL v2 license

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

mysql> show databases;

+------------------------+

| Database |

+------------------------+

| information_schema |

| entitytest |

| mysql |

| test |

| test2 |

| test_db |

+------------------------+

6 rows in set (0.07 sec)

Step 6:

Configuring tnsnames.ora.

Add the following lines to $ORACLE_HOME/network/admin/tnsnames.ora

myodbc5 =

(DESCRIPTION=

(ADDRESS=

(PROTOCOL=TCP) (HOST=localhost) (PORT=1521)

)

(CONNECT_DATA=

(SID=myodbc5))

(HS=OK)

)

Step 7:

Configuring listener.ora

Open $ORACLE_HOME/network/admin/listener.ora in a text editor, find

SID_LIST_LISTENER definition and add a new entry for myodbc5:

SID_LIST_LISTENER=
     (SID_LIST=
        (SID_DESC=
             (SID_NAME=myodbc5)
             (ORACLE_HOME=/home/dbs/app/Ora/product/11.2.0/dbhome_1)
             (PROGRAM=dg4odbc)
             (ENV="LD_LIBRARY_PATH=/home/dbs/app/unixodbc-2.2.14/lib:/home/dbs/app/Ora/product/11.2.0/dbhome_1/lib")
       )
   )

We strongly recommend to add the LD_LIBRARY_PATH to the listener.ora file to avoid any conflicts with already existing ODBC driver managers. 
The LD_LIBRARY_PATH must contain the fully qualified path to the $ORACLE_HOME/lib and also the library paths of the ODBC driver manager and the ODBC driver itself.

Step 8:

Configuring gateway init.ora file.

This file does not exist and you have to create it.

In listener.ora, tnsnames.ora we use the name myodbc5 just for convenience, but it can be literally anything. The init file (initmyodbc5.ora) is a different story because HS_FDS_CONNECT_INFO is the DSN name in odbc.ini.

$ vi $ORACLE_HOME/hs/admin/initmyodbc5.ora

HS_FDS_CONNECT_INFO=myodbc5 
# Data source name in odbc.ini
HS_FDS_TRACE_LEVEL=OFF
HS_FDS_SHAREABLE_NAME=/home/dbs/app/unixodbc-2.3/lib/libodbc.so
HS_FDS_SUPPORT_STATISTICS=FALSE
HS_LANGUAGE=AMERICAN_AMERICA.WE8ISO8859P15
#
# ODBC env variables
set ODBCINI=/home/dbs/etc/odbc.ini

+---------------------------------------------------------------------------+

NOTE: HS_FDS_SHAREABLE_NAME must point to the [ODBC Driver Manager] library. It is an error to put there the [ODBC Driver] library. The [ODBC Driver] DSN is referenced in HS_FDS_CONNECT_INFO=myodbc5.

+---------------------------------------------------------------------------+

+---------------------------------------------------------------------------+

| NOTE: UnixODBC [ODBC Driver Manager] might not support any character set |

| used in Oracle, so the connection string will be corrupted. |

| To avoid the connection string corruption it is recommended to set |

| HS_LANGUAGE parameter. I the present case AMERICAN_AMERICA.WE8ISO8859P15 |

| worked ok |

+---------------------------------------------------------------------------+

Step 9

Applying the settings in the configuration files.  The listeners must be restarted in order to pick up the changes we just made into tnsnames.ora, listener.ora and initmyodbc5.ora:

$ lsnrctl stop

$ lsnrctl start

+---------------------------------------------------------------------------+

NOTE: The start status has to contain information about the service:

......... |

Services Summary...

Service "myodbc5" has 1 instance(s).

Instance "myodbc5", status UNKNOWN, has 1 handler(s) for this service...

The command completed successfully

+---------------------------------------------------------------------------+

Step 10 (Optional)

Checking the service status:

$ lsnrctl status

LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 06-MAY-2011 19:42:10

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

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost.localdomain)(PORT=1521)))

STATUS of the LISTENER

------------------------

Alias LISTENER

Version TNSLSNR for Linux: Version 11.2.0.1.0 - Production

Start Date 06-MAY-2011 19:41:39

Uptime 0 days 0 hr. 0 min. 30 sec

Trace Level off

Security ON: Local OS Authentication

SNMP OFF

Listener Parameter File /home/dbs/app/Ora/product/11.2.0/dbhome_1/network/admin/listener.ora

Listener Log File /home/dbs/app/Ora/diag/tnslsnr/dbs-pc/listener/alert/log.xml

Listening Endpoints Summary...

(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost.localdomain)(PORT=1521)))

Services Summary...

Service "myodbc5" has 1 instance(s).

Instance "myodbc5", status UNKNOWN, has 1 handler(s) for this service...

The command completed successfully

The command prints the status of "myodbc5" service. UNKNOWN is not a problem

in this case because we have not tried using the service yet.

Pinging:

$ tnsping myodbc5

TNS Ping Utility for Linux: Version 11.2.0.1.0 - Production on

06-MAY-2011 19:42:22

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

Used parameter files:

Used TNSNAMES adapter to resolve the alias

Attempting to contact (DESCRIPTION= (ADDRESS= (PROTOCOL=TCP) (HOST=localhost)

(PORT=1521)) (CONNECT_DATA= (SID=myodbc5)) (HS=OK))

OK (10 msec)

If the above commands report errors check thsnames.ora and listener.ora and set them as shown on Step 6-7.

Step 11:

Creating the database link and getting the data.

Start sqlplus and type the following command (user and password are the same as in odbc.ini):

SQL> create public database link myodbc5 connect to "mysql_user" identified by

"********" using 'myodbc5';

Database link created.

SQL> select * from "tab1"@myodbc5;

id

----------

txt1

-----------------------------------------------------------------------------

1

some text

2

some more text

DONE!

+---------------------------------------------------------------------------+

NOTE: When selecting data from MySQL linked table it is recommended to enclose the table name into double quotes as "tab1" unless MySQL Server is set to ANSI_QUOTES |

+---------------------------------------------------------------------------+

THINGS TO CHECK IF SELECT DISPLAYS AN ERROR:

Oracle HS provides very little information about the error if it occurs, so be prepared to examine lots of different log files and command outputs. First of all, make sure UnixODBC [ODBC Driver Manager] is loaded in memory. To do so run sqlplus and try to execute SELECT as on Step 11. Open another terminal session without closing sqlplus and type the following command:

$ lsof | grep "dg4odbc" | grep "libodbc"

dg4odbc 8,3 1753075 3474018 /home/dbs/app/unixodbc-2.2.14/lib/libodbc.so.1.0.0

Next, check if the driver library is loaded:

$ lsof | grep "dg4odbc" | grep "libmyodbc"

dg4odbc 8,3 5717703 3474035 /home/dbs/app/mysql-connector-odbc-5.1.8-linux-

glibc2.3-x86-64bit/lib/libmyodbc5-5.1.8.so

WHERE TO LOOK FOR LOG AND TRACE FILES:

[Oracle], [DG4ODBC] and [ODBC Driver Manager] write logs that allow to identify problems if connect fails with unknown error.

1. alert.log file.

Sqlplus command line shows the current location (directory) of the trace file:

SQL> show parameter BACKGROUND_DUMP_DEST

NAME TYPE VALUE

------------------------------------ ----------- ------------------------------

background_dump_dest string /home/dbs/app/Ora/diag/rdbms/o

rcl/orcl/trace

2. HS trace file.

With the current settings the HS trace is disabled. To enable it uncomment the following line in initmyodbc5.ora:

HS_FDS_TRACE_LEVEL=user

The file is named as myodbc5_xxxxx.trc and located in $ORACLE_HOME/hs/log directory.

3. ODBC trace file is written by [ODBC Driver Manager].

To enable ODBC tracing set the following option for your DSN in odbc.ini

TRACE = ON

The trace file is located in /tmp directory. The name of the file is sql.log.

REFERENCES

NOTE:1083703.1 - Master Note for Oracle Gateway Products
NOTE:561033.1 - How to Configure DG4ODBC on 64bit Unix OS (Linux, Solaris, AIX, HP-UX Itanium) to Connect to Non-Oracle Databases Post Install



ORACLE 11g 通过odbc建立连接到mysql库的database link

以前做过在oracle通过gateway建立到sqlserver的database link(详情参见:http://blog.csdn.net/mchdba/article/details/52719278),现在需要无缝访问mysql,所以也需要建立类似这样的连接通道。

1、分配mysql账号

先在mysql库上建立连接账号:

GRANT SELECT ON test.* TO data_query@'192.168.129.%' IDENTIFIED BY 'data_query_1125';

2、安装dg4odbc

详细的gateways安装步骤参考http://blog.csdn.net/mchdba/article/details/52719278      里面的 “2、安装模块” 里面有详细的步骤描述。

在安装的时候,有一个关键的选择步骤就是选择odbc组件包进行安装,如下所示:E:\u\oracle\install\pic\11.png,

3、查看下安装的dg4odbc

查看安装是否完成,就直接用莫N股了dg4odbc命令来看

[oracle@hch_test_121_61 ~]$ dg4odbc

Oracle Corporation --- TUESDAY   NOV 15 2016 19:17:19.106

Heterogeneous Agent Release 11.2.0.1.0 - 64bit Production  Built with

Oracle Database Gateway for ODBC

[oracle@hch_test_121_61 ~]$

4、安装mysql组件

准备安装mysql客户端:yum install mysql-connector-odbc.x86_64 -y;

这样会安装一些依赖包主要是

mysql-connector*.x86_64.rpm

unixODBC*.x86_64.rpm

unixODBC*.i386.rpm

[root@hch_test_121_90 ~]# yum install mysql-connector-odbc.x86_64 -y

Loaded plugins: fastestmirror, security

Determining fastest mirrors

* base: ftp.sjtu.edu.cn

* extras: mirrors.163.com

* updates: mirrors.163.com

base                                                                                                                                                                    | 3.7 kB     00:00

epel                                                                                                                                                                    | 4.3 kB     00:00

epel/primary_db                                                                                                                                                         | 5.9 MB     00:06

extras                                                                                                                                                                  | 3.4 kB     00:00

updates                                                                                                                                                                 | 3.4 kB     00:00

updates/primary_db                                                                                                                                                      | 3.1 MB     00:00

zabbix                                                                                                                                                                  |  951 B     00:00

zabbix-non-supported                                                                                                                                                    |  951 B     00:00

Setting up Install Process

Resolving Dependencies

--> Running transaction check

---> Package mysql-connector-odbc.x86_64 0:5.1.5r1144-7.el6 will be installed

--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================================================================================

Package                                               Arch                                    Version                                             Repository                             Size

===============================================================================================================================================================================================

Installing:

mysql-connector-odbc                                  x86_64                                  5.1.5r1144-7.el6                                    base                                  114 k

Transaction Summary

===============================================================================================================================================================================================

Install       1 Package(s)

Total download size: 114 k

Installed size: 345 k

Downloading Packages:

mysql-connector-odbc-5.1.5r1144-7.el6.x86_64.rpm                                                                                                                        | 114 kB     00:00

Running rpm_check_debug

Running Transaction Test

Transaction Test Succeeded

Running Transaction

Installing : mysql-connector-odbc-5.1.5r1144-7.el6.x86_64                                                                                                                                1/1

Verifying  : mysql-connector-odbc-5.1.5r1144-7.el6.x86_64                                                                                                                                1/1

Installed:

mysql-connector-odbc.x86_64 0:5.1.5r1144-7.el6

Complete!

[root@hch_test_121_90 ~]#

5、配置odbc-mysql连接

[root@hch_test_121_90 ~]# more /etc/odbc.ini

# Setup from the unixODBC64-libs package

[rmt]

Driver          = /usr/lib64/libmyodbc5.so

Server          = 10.254.3.72

Port            = 3306

User            = data_query

Password        = data_query_1125

Database        = test

[root@hch_test_121_90 ~]#

6、测试mysql组件的连接:

[root@hch_test_121_90 ~]#  isql rmt

+---------------------------------------+

| Connected!                            |

|                                       |

| sql-statement                         |

| help [tablename]                      |

| quit                                  |

|                                       |

+---------------------------------------+

SQL>

7、配置oracle连接

进去$ORACLE_HOME/hs/admin/,配置initrmt.ora

[oracle@hch_test_121_90 admin]$ more initrmt.ora

HS_FDS_CONNECT_INFO=rmt

# Data source name in odbc.ini

HS_FDS_TRACE_LEVEL= debug

HS_FDS_SHAREABLE_NAME=libodbc.so

HS_FDS_SUPPORT_STATISTICS=FALSE

HS_LANGUAGE=AMERICAN_AMERICA.WE8ISO8859P15

#

# ODBC env variables

set ODBCINI=/etc/odbc.ini

HS_FDS_SHAREABLE_NAME=libodbc.so

[oracle@hch_test_121_90 admin]$

配置$ORACLE_HOME/network/admin/listener.ora

SID_NAME is the DSN for the remote database. 
ORACLE_HOME
 is the actual Oracle home file path. 
PROGRAM tells Oracle to use heterogeneous services.

(SID_DESC =

(SID_NAME = rmt)

(ORACLE_HOME = /home/oracle/app/oracle/product/11.2.0/dbhome_1)

(PROGRAM = dg4odbc)

#(ENVS=LD_LIBRARY_PATH=/usr/lib64:/usr/lib:/home/orared/product/11.2.4/lib:/home/orared/product/11.2.4/odbc/lib)

)

配置$ORACLE_HOME/network/admin/tnsnams.ora

rmtmysql =

(DESCRIPTION =

(ADDRESS_LIST =

(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.121.90 )(PORT = 1521))

)

(CONNECT_DATA =

(SID = rmt)

)

(HS = OK)

)

8、建立dblink

create database link tg4mysql connect to data_query identified by "data_query_1125"

using '(DESCRIPTION =

(ADDRESS_LIST =

(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.121.90)(PORT = 1521))

)

(CONNECT_DATA =

(SID = rmtmysql)

)

(HS = OK)

)';

测试:

SQL>  select * from z_whs@tg4mysql;

select * from z_whs@tg4mysql

*

ERROR at line 1:

ORA-28545: error diagnosed by Net8 when connecting to an agent

Unable to retrieve text of NETWORK/NCR message 65535

ORA-02063: preceding 2 lines from TG4MYSQL

SQL>

解决办法:重启lsnrctl服务

9、问题ORA-28511

SQL>  select * from Z_WHS@gwmysql;

ERROR:

ORA-28511: lost RPC connection to heterogeneous remote agent using

SID=ORA-28511: lost RPC connection to heterogeneous remote agent using

SID=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.121.90)(PORT

=1521)))(CONNECT_DATA=(SID=rmt)))

ORA-02063: preceding line from GWMYSQL

Process ID: 20358

Session ID: 11 Serial number: 46988

no rows selected

SQL>

再次查看:

SQL> select * from Z_WHS@gwmysql;

ERROR:

ORA-28511: lost RPC connection to heterogeneous remote agent using

SID=ORA-28511: lost RPC connection to heterogeneous remote agent using

SID=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.121.90)(PORT

=1521)))(CONNECT_DATA=(SID=rmt)))

ORA-02063: preceding line from GWMYSQL

Process ID: 20497

Session ID: 134 Serial number: 41341

no rows selected

SQL> select ID,cname from Z_WHS@gwmysql;

select ID,cname from Z_WHS@gwmysql

*

ERROR at line 1:

ORA-00904: "CNAME": invalid identifier

SQL> select ID from Z_WHS@gwmysql;

ID

----------

1

2

3

SQL>

为什么查询单个字段ID能查到记录,查询*查不出记录呢,查看Z_WHS表结构,发现有一个字段cname为小写,尝试改成大写字段,就可以查询出来了。

mysql> show create table Z_WHS;

+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                                                                                              |

+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| Z_WHS | CREATE TABLE `Z_WHS` (

`ID` bigint(20) NOT NULL DEFAULT '0' COMMENT '投诉主题表主键',

`cname` varchar(16) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4        |

+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

mysql>

mysql> alter table Z_WHS change  `cname` CNAME varchar(16) DEFAULT NULL;

Query OK, 0 rows affected (38.95 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql>

# PS:这个时候,oracle的网关对mysql的表是锁定状态,任何对mysql表的dml、ddl操作都会处于等待状态,如下所示:

| 13762502 | root            | localhost             | test         | Query       |       27 | Waiting for table metadata lock                                       | alter table Z_WHS change  `cname` CNAME varchar(16) DEFAULT NULL |

需要在oracle的窗口退出来,释放掉通过网关对mysql的锁,才能让mysql窗口的alter语句sesseion会话得到这个表的使用权限来顺利执行完成alter操作。

BUT、但是,但是可惜的是alter语句执行完后,select cname 还是报一样的错误出来

新添加一列数字列:

去mysql库添加数字列

mysql> alter table Z_2 add column NUM int;

Query OK, 0 rows affected (0.07 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> update Z_2 set NUM=1;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1  Changed: 1  Warnings: 0

mysql>

再通过gateway查询结果:

SQL> conn dw/dwys0418

Connected.

SQL> select ID,NUM from "Z_2"@gwmysql;

ID       NUM

---------- ----------

1         1

SQL> select ID,NUM,CN from "Z_2"@gwmysql;

ERROR:

ORA-28511: lost RPC connection to heterogeneous remote agent using

SID=ORA-28511: lost RPC connection to heterogeneous remote agent using

SID=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.121.90)(PORT

=1521)))(CONNECT_DATA=(SID=rmt)))

ORA-02063: preceding line from GWMYSQL

Process ID: 21333

Session ID: 11 Serial number: 47108

no rows selected

SQL>

问题依旧,等待后续进一步解决。



About Me

.............................................................................................................................................

● 本文作者:小麦苗,部分内容整理自网络,若有侵权请联系小麦苗删除

● 本文在itpub(http://blog.itpub.net/26736162/abstract/1/)、博客园(http://www.cnblogs.com/lhrbest)和个人微信公众号(xiaomaimiaolhr)上有同步更新

● 本文itpub地址:http://blog.itpub.net/26736162/abstract/1/

● 本文博客园地址:http://www.cnblogs.com/lhrbest

● 本文pdf版、个人简介及小麦苗云盘地址:http://blog.itpub.net/26736162/viewspace-1624453/

● 数据库笔试面试题库及解答:http://blog.itpub.net/26736162/viewspace-2134706/

● DBA宝典今日头条号地址:http://www.toutiao.com/c/user/6401772890/#mid=1564638659405826

.............................................................................................................................................

● QQ群号:230161599(满)、618766405

● 微信群:可加我微信,我拉大家进群,非诚勿扰

● 联系我请加QQ好友(646634621),注明添加缘由

● 于 2017-09-01 09:00 ~ 2017-09-30 22:00 在魔都完成

● 文章内容来源于小麦苗的学习笔记,部分整理自网络,若有侵权或不当之处还请谅解

● 版权所有,欢迎分享本文,转载请保留出处

.............................................................................................................................................

● 小麦苗的微店:https://weidian.com/s/793741433?wfr=c&ifr=shopdetail

● 小麦苗出版的数据库类丛书:http://blog.itpub.net/26736162/viewspace-2142121/

.............................................................................................................................................

使用微信客户端扫描下面的二维码来关注小麦苗的微信公众号(xiaomaimiaolhr)及QQ群(DBA宝典),学习最实用的数据库技术。

小麦苗的微信公众号      小麦苗的DBA宝典QQ群1     小麦苗的DBA宝典QQ群2        小麦苗的微店

.............................................................................................................................................

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26736162/viewspace-2144661/,如需转载,请注明出处,否则将追究法律责任。

[Oracle- MySQL] Oracle通过dblink连接MySQL相关推荐

  1. oracle通过dblink连接mysql配置详解(全Windows下)

    oracle通过dblink连接mysql配置详解(全Windows下) 关于oracle通过dblink连接mysql,经过了两周的空闲时间研究学习,终于配置好了,真是不容易啊,仔细想想的话,其实也 ...

  2. 记一次oracle通过dblink连接mysql实施

    本周由于公司一个系统的oracle数据库需要采集另一个系统mysql数据库的数据,于是对oracle通过dblink连接mysql进行了研究,测试环境是单实例oracle 10g 64位,折腾了半天搞 ...

  3. php设置mysql查询编码,php连接mysql时怎么设置编码方式

    php连接mysql时怎么设置编码方式 php连接mysql数据库时,也就是在mysql_connect()语句之后添加"mysql_query("set names utf8&q ...

  4. php连接mysql乱码原因_PHP连接MYSQL出现乱码的原因与解决办法

    PHP连接mysql出现乱码的原因与解决方法 解决MySql数据库乱码的一个首要前提是保证 数据库+数据表+前端显示 编码一致,国内项目一般统一设定为GB2312或GBK,而国际化项目则一般使用utf ...

  5. 【全教程】qt连接mysql——从qt编译mysql驱动到qt连接mysql数据库(二、编译连接)

    本篇教程分为三个部分: [全教程]qt连接mysql--从qt编译mysql驱动到qt连接mysql数据库(一.编译连接前准备) [全教程]qt连接mysql--从qt编译mysql驱动到qt连接my ...

  6. macOS安装MySQL,使用Navicat连接MySQL数据库/2022

    macOS安装MySQL,使用Navicat连接MySQL数据库/2022 intel chip 下载安装文件 进入官网 MySQL :: MySQL Downloads 选择页尾MySQL Comm ...

  7. 【全教程】qt连接mysql——从qt编译mysql驱动到qt连接mysql数据库(一、编译连接前准备)

    一.说明 电脑系统:win10 qt版本:5.13.2和5.14.1(测试均成功) mysql版本:MySQL-5.5 本篇教程分为三个部分: [全教程]qt连接mysql--从qt编译mysql驱动 ...

  8. Oracle 10g 中通过DBLink访问MySQL数据库

    前一阵在公司通过Oracle访问MySQL,测试环境:CentOS5_X64, Oracle10g_X64, MySQL5 . 把一些经验分享给大家! 1, 首先在Oracle所在计算机安装MySQL ...

  9. php连接虚拟机中mysql数据库吗,PHP连接MySQL数据库的三种方式

    本篇文章给大家介绍一下PHP连接MysqL数据库的三种方式(MysqL.MysqLi.pdo),结合实例形式分析了PHP基于MysqL.MysqLi.pdo三种方式连接MysqL数据库的相关操作技巧与 ...

最新文章

  1. JS 数组A有数组B的数据就删除
  2. ajax post对象参数,ajax用post方法传递参数
  3. ASP.NET系统 + Access数据库
  4. 函数的参数-列表使用+=本质上是调用extend方法
  5. [转] 前端中的MVC
  6. shell中的 trap
  7. React 第十章 状态提升
  8. Pandas创建DataFrame对象的几种常用方法
  9. 2017秋-软件工程第八次作业-第九周例行总结
  10. mac废纸篓清空的心得、mac设置不睡眠不待机不锁屏、如何快速锁屏待机睡眠、mac重启、mac学习的必备软件-城...
  11. HBase: 看上去很美
  12. 惠普打印机换硒鼓图解_惠普激光打印机硒鼓安装方法(图文)
  13. windowsxp主题包把windowsXP主题改成Windows7风格
  14. 《神经网络与深度学习从理论到实践-邱锡鹏》笔记(持续更行ing)
  15. lumion基础操作(一)
  16. 中国股市十大风云人物
  17. oracle 保留池,oracle的内存结构之--查看内存信息+保留池和循环池(摘自文平书)...
  18. MyEclipse 安装教程
  19. yolov5不能检测长宽比超过20的目标的解决方法
  20. c语言switch函数的作用,c语言的switchcase语句

热门文章

  1. MSDC 4.3 接口规范(19)
  2. 模拟卷Leetcode【普通】198. 打家劫舍
  3. LeetCode - Pascal's Trangle2
  4. 云脉H5文档识别的性能评估指标
  5. html中页码居中,如何把Word2007的页脚设置为页码并居中?
  6. Python | 人脸识别系统 — 用户操作
  7. USB OTG(Host) 、 USB ADB(Device)、DWC3 Charge
  8. .net core 中的[FromBody]和[FromFrom]的区别
  9. vmware+双显示器实现双系统同时使用
  10. Ubuntu系统电池管理软件TLP(Thinkpad设置电池充电阈值)