[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
- [oracle@lx16 ~]$ file $ORACLE_HOME/bin/dg4odbc
- /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
- $ 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
- $ tar -zxvf unixODBC-2.2.14-linux-x86-64.tar.gz
解压缩后会在当前目录下自动创建usr的目录,我们创建一个目录(~/app/unixodbc-2.2.14)用于放置unixodbc,然后把usr 迁移到该目录下:
[plain] view plain copy
- $ mkdir ~/app/unixodbc-2.2.14
- $ 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
- $ 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/
- $ tar -zxvf mysql-connector-odbc-5.2.5-linux-glibc2.5-x86-64bit.tar.gz
解压缩成功后是一个文件夹,把该文件夹迁移至~/app目录下,并给它创建一个软链接:
[plain] view plain copy
- $ mv mysql-connector-odbc-5.2.5-linux-glibc2.5-x86-64bit ~/app
- $ cd ~/app
- $ 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
- [myodbc5]
- Driver = /home/oracle/app/myodbc-5.2.5/lib/libmyodbc5w.so
- Description = Connector/ODBC 5.2 Driver DSN
- SERVER = 192.168.1.15
- PORT = 3306
- USER = mysql_user
- PASSWORD = mysql_pwd
- DATABASE = mysql_db
- OPTION = 0
- TRACE = OFF
其中,Driver指向第3步上按照的ODBC Driver,这里要特别注意:MySQL的Datbase是大小写敏感的。
5)验证ODBC连接
[plain] view plain copy
- $ export ODBCINI=/home/oracle/etc/odbc.ini
- $ export LD_LIBRARY_PATH=/home/oracle/app/unixodbc-2.2.14/usr/local/lib:$LD_LIBRARY_PATH
- $ cd ~/app/unixodbc-2.2.14/usr/local/bin
- $ ./isql myodbc5 -v
- +---------------------------------------+
- | Connected! |
- | |
- | sql-statement |
- | help [tablename] |
- | quit |
- | |
- +---------------------------------------+
上面显示连接成功。
6)配置tnsnames.ora
[plain] view plain copy
- myodbc5 =
- (DESCRIPTION=
- (ADDRESS=
- (PROTOCOL=TCP) (HOST=localhost) (PORT=1521)
- )
- (CONNECT_DATA=
- (SID=myodbc5)
- )
- (HS=OK)
- )
7)配置listener.ora
[plain] view plain copy
- SID_LIST_LISTENER=
- (SID_LIST=
- (SID_DESC=
- (SID_NAME=myodbc5)
- (ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/dbhome_1)
- (PROGRAM=dg4odbc)
- (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)
- )
- )
如上所示,为了避免和其它已存在的ODBC Driver Manager冲突,强烈设置LD_LIBRARY_PATH在listener.ora
8)创建init.ora文件
创建文件$ORACLE_HOME/hs/admin/initmyodbc5.ora,内容如下:
[sql] view plain copy
- HS_FDS_CONNECT_INFO=myodbc5 # Data source name in odbc.ini
- HS_FDS_SHAREABLE_NAME=/home/oracle/app/unixodbc-2.2.14/usr/local/lib/libodbc.so
- HS_FDS_SUPPORT_STATISTICS=FALSE
- HS_LANGUAGE=AMERICAN_AMERICA.WE8ISO8859P15
- # ODBC env variables
- set ODBCINI=/home/oracle/etc/odbc.ini
9)使上述配置文件生效
[sql] view plain copy
- $ lsnrctl reload
- $ lsnrctl status
- Service "myodbc5" has 1 instance(s).
- Instance "myodbc5", status UNKNOWN, has 1 handler(s) for this service...
10)验证配置是否正确
[sql] view plain copy
- $ tnsping myodbc5
- TNS Ping Utility for Linux: Version 11.2.0.1.0 - Production on 29-AUG-2013 10:54:46
- Copyright (c) 1997, 2009, Oracle. All rights reserved.
- Used parameter files:
- /home/oracle/app/oracle/product/11.2.0/dbhome_1/network/admin/sqlnet.ora
- 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 (0 msec)
11)创建dblink
[sql] view plain copy
- SQL> create public database link mysqltest connect to "mysql_user" identified by "mysql_pwd" using 'myodbc5';
- SQL> select count(*) from trans_expert_map@mysqltest;
- COUNT(*)
- ----------
- 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 -
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 -
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 小麦苗的微店
.............................................................................................................................................