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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: [Oracle -> MySQL]  Oracle通过dblink连接MySQL  业务上有这么一个需求,需要把Oracle的一些数据同步到MySQL,如果每次都是手动同步的话,实在太麻烦,因此花了点时间研究了下Oracle直连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.   
  6. # ODBC env variables  
  7. 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.   
  3. TNS Ping Utility for Linux: Version 11.2.0.1.0 - Production on 29-AUG-2013 10:54:46  
  4.   
  5. Copyright (c) 1997, 2009, Oracle.  All rights reserved.  
  6.   
  7. Used parameter files:  
  8. /home/oracle/app/oracle/product/11.2.0/dbhome_1/network/admin/sqlnet.ora  
  9.   
  10.   
  11. Used TNSNAMES adapter to resolve the alias  
  12. Attempting to contact (DESCRIPTION= (ADDRESS= (PROTOCOL=TCP) (HOST=localhost) (PORT=1521)) (CONNECT_DATA= (SID=myodbc5)) (HS=OK))  
  13. 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.   
  4.   COUNT(*)  
  5. ----------  
  6.        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表的dmlddl操作都会处于等待状态,如下所示:

| 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        小麦苗的微店

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


DBA笔试面试讲解群1
DBA笔试面试讲解群2
欢迎与我联系



相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
23天前
|
关系型数据库 MySQL 网络安全
DBeaver连接MySQL提示Access denied for user ‘‘@‘ip‘ (using password: YES)
“Access denied for user ''@'ip' (using password: YES)”错误通常与MySQL用户权限配置或网络设置有关。通过检查并正确配置用户名和密码、用户权限、MySQL配置文件及防火墙设置,可以有效解决此问题。希望本文能帮助您成功连接MySQL数据库。
36 4
|
1月前
|
安全 关系型数据库 MySQL
【赵渝强老师】MySQL的连接方式
本文介绍了MySQL数据库服务器启动后的三种连接方式:本地连接、远程连接和安全连接。详细步骤包括使用root用户登录、修改密码、创建新用户、授权及配置SSL等。并附有视频讲解,帮助读者更好地理解和操作。
131 1
|
2月前
|
SQL Java 关系型数据库
java连接mysql查询数据(基础版,无框架)
【10月更文挑战第12天】该示例展示了如何使用Java通过JDBC连接MySQL数据库并查询数据。首先在项目中引入`mysql-connector-java`依赖,然后通过`JdbcUtil`类中的`main`方法实现数据库连接、执行SQL查询及结果处理,最后关闭相关资源。
155 6
|
2月前
|
SQL JavaScript 关系型数据库
node博客小项目:接口开发、连接mysql数据库
【10月更文挑战第14天】node博客小项目:接口开发、连接mysql数据库
|
2月前
|
Java 关系型数据库 MySQL
【编程基础知识】Eclipse连接MySQL 8.0时的JDK版本和驱动问题全解析
本文详细解析了在使用Eclipse连接MySQL 8.0时常见的JDK版本不兼容、驱动类错误和时区设置问题,并提供了清晰的解决方案。通过正确配置JDK版本、选择合适的驱动类和设置时区,确保Java应用能够顺利连接MySQL 8.0。
255 1
|
2月前
|
Java 关系型数据库 MySQL
springboot学习五:springboot整合Mybatis 连接 mysql数据库
这篇文章是关于如何使用Spring Boot整合MyBatis来连接MySQL数据库,并进行基本的增删改查操作的教程。
216 0
springboot学习五:springboot整合Mybatis 连接 mysql数据库
|
2月前
|
SQL Oracle 关系型数据库
Python连接Oracle
Python连接Oracle
22 0
|
2月前
|
SQL JavaScript 关系型数据库
Node.js 连接 MySQL
10月更文挑战第9天
32 0
|
2月前
|
关系型数据库 MySQL Linux
Navicat 连接 Windows、Linux系统下的MySQL 各种错误,修改密码。
使用Navicat连接Windows和Linux系统下的MySQL时可能遇到的四种错误及其解决方法,包括错误代码2003、1045和2013,以及如何修改MySQL密码。
280 0
|
2月前
|
Oracle 关系型数据库 MySQL
shell获取多个oracle库mysql库所有的表
请注意,此脚本假设你有足够的权限访问所有提到的数据库。在实际部署前,请确保对脚本中的数据库凭据、主机名和端口进行适当的修改和验证。此外,处理数据库操作时,务必谨慎操作,避免因错误的脚本执行造成数据损坏或服务中断。
44 0

推荐镜像

更多