[Oracle-> MySQL] Oracle通過dblink連接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位:


[oracle@lx16 ~]$ file $ORACLE_HOME/bin/dg4odbc  
  1. /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)

 

$ wget http://sourceforge.net/projects/unixodbc/files/unixODBC/2.2.14/unixODBC-2.2.14-linux-x86-64.tar.gz/download  

解壓縮:


 

$ tar -zxvf unixODBC-2.2.14-linux-x86-64.tar.gz  

解壓縮后會在當前目錄下自動創建usr的目錄,我們創建一個目錄(~/app/unixodbc-2.2.14)用於放置unixodbc,然后把usr 遷移到該目錄下:


 

$ mkdir ~/app/unixodbc-2.2.14  
  1. $ 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版本:

 

$ 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/  
  1. $ tar -zxvf mysql-connector-odbc-5.2.5-linux-glibc2.5-x86-64bit.tar.gz  

解壓縮成功后是一個文件夾,把該文件夾遷移至~/app目錄下,並給它創建一個軟鏈接:

 

$ mv mysql-connector-odbc-5.2.5-linux-glibc2.5-x86-64bit ~/app  
  1. $ cd ~/app  
  2. $ ln -s mysql-connector-odbc-5.2.5-linux-glibc2.5-x86-64bit myodbc-5.2.5  

4)配置ODBC Driver


在~/etc目錄下創建odbc.ini如下:

 

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

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


5)驗證ODBC連接

 

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

上面顯示連接成功。


6)配置tnsnames.ora

 

myodbc5 =  
  1.   (DESCRIPTION=  
  2.     (ADDRESS=  
  3.         (PROTOCOL=TCP) (HOST=localhost) (PORT=1521)  
  4.     )  
  5.     (CONNECT_DATA=  
  6.       (SID=myodbc5)  
  7.     )  
  8.     (HS=OK)  
  9. )  

7)配置listener.ora


 

SID_LIST_LISTENER=  
  1.   (SID_LIST=  
  2.     (SID_DESC=  
  3.       (SID_NAME=myodbc5)  
  4.       (ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/dbhome_1)  
  5.       (PROGRAM=dg4odbc)  
  6.       (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)  
  7.     )   
  8.   )  

如上所示,為了避免和其它已存在的ODBC Driver Manager沖突,強烈設置LD_LIBRARY_PATH在listener.ora

8)創建 init.ora文件
創建文件$ORACLE_HOME/hs/admin/initmyodbc5.ora,內容如下:

 

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

9)使上述配置文件生效


 

$ lsnrctl reload  
  1. $ lsnrctl status  
  2. Service  "myodbc5"  has 1 instance(s).  
  3.   Instance  "myodbc5" , status UNKNOWN, has 1 handler(s)  for  this service...、  

10)驗證配置是否正確

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


11)創建dblink

 

SQL>  create   public   database  link mysqltest  connect   to   "mysql_user"  identified  by   "mysql_pwd"  using  'myodbc5' ;  
  1. SQL>  select   count (*)  from  trans_expert_map@mysqltest;  
  2.   
  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>

 

問題依舊,等待后續進一步解決。

http://blog.itpub.net/26736162/viewspace-2144661/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM