ORACLE 数据库在LINUX 访问 MYSQL 数据库通过 DBLINK + ODBC方式。
数据库版本是ORACLE 11.2.0.4.0 LINUX :ORACLE LINUX 6.9
前提条件配置好YUM源
[root@jxhistdb etc]# yum install -y unixODBC mysql-connector-odbc
已加载插件:security, ulninfo
设置安装进程
OracleLinux65cd-local | 3.7 kB 00:00 ...
解决依赖关系
--> 执行事务检查
---> Package mysql-connector-odbc.x86_64 0:5.1.5r1144-7.el6 will be 安装
---> Package unixODBC.x86_64 0:2.2.14-14.el6 will be 安装
--> 完成依赖关系计算
依赖关系解决
================================================================================================================================
软件包 架构 版本 仓库 大小
================================================================================================================================
正在安装:
mysql-connector-odbc x86_64 5.1.5r1144-7.el6 OracleLinux65cd-local 114 k
unixODBC x86_64 2.2.14-14.el6 OracleLinux65cd-local 377 k
事务概要
================================================================================================================================
Install 2 Package(s)
总下载量:491 k
Installed size: 1.5 M
下载软件包:
总计 4.5 MB/s | 491 kB 00:00
运行 rpm_check_debug
执行事务测试
事务测试成功
执行事务
Warning: RPMDB altered outside of yum.
正在安装 : unixODBC-2.2.14-14.el6.x86_64 1/2
正在安装 : mysql-connector-odbc-5.1.5r1144-7.el6.x86_64 2/2
Verifying : unixODBC-2.2.14-14.el6.x86_64 1/2
Verifying : mysql-connector-odbc-5.1.5r1144-7.el6.x86_64 2/2
已安装:
mysql-connector-odbc.x86_64 0:5.1.5r1144-7.el6 unixODBC.x86_64 0:2.2.14-14.el6
完毕!
检查对应的配置文件是否存在 /etc
[root@jxhistdb etc]# ll odbc.
-rw-r--r-- 1 root root 0 8月 17 2014 odbc.ini
-rw-r--r-- 1 root root 575 8月 17 2014 odbcinst.ini
检查对应的SO文件是否存在 /usr/lib64
[root@jxhistdb lib64]# ls odbc.*
libmyodbc5-5.1.5.so libodbcdrvcfg2S.so.2 libodbcminiS.so.2.0.0 libodbcpsqlS.so libodbctxtS.so.2
libmyodbc5.so libodbcdrvcfg2S.so.2.0.0 libodbcmyS.so libodbcpsqlS.so.2 libodbctxtS.so.2.0.0
libodbccr.so.2 libodbcinst.so libodbcmyS.so.2 libodbcpsqlS.so.2.0.0 liboplodbcS.so.2
libodbccr.so.2.0.0 libodbcinst.so.2 libodbcmyS.so.2.0.0 libodbc.so liboplodbcS.so.2.0.0
libodbcdrvcfg1S.so.2 libodbcinst.so.2.0.0 libodbcnnS.so.2 libodbc.so.2 liboraodbcS.so.2
libodbcdrvcfg1S.so.2.0.0 libodbcminiS.so.2 libodbcnnS.so.2.0.0 libodbc.so.2.0.0 liboraodbcS.so.2.0.0
检查驱动配置文件
[oracle@jxhistdb admin]cat /etc/odbcinst.ini
Example driver definitions
Driver from the postgresql-odbc package
Setup from the unixODBC package
[PostgreSQL]
Description = ODBC for PostgreSQL
Driver = /usr/lib/psqlodbc.so
Setup = /usr/lib/libodbcpsqlS.so
Driver64 = /usr/lib64/psqlodbc.so
Setup64 = /usr/lib64/libodbcpsqlS.so
FileUsage = 1
Driver from the mysql-connector-odbc package
Setup from the unixODBC package
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/libmyodbc5.so
Setup = /usr/lib/libodbcmyS.so
Driver64 = /usr/lib64/libmyodbc5.so
Setup64 = /usr/lib64/libodbcmyS.so
FileUsage = 1
编辑MYSQL连接文件
[root@jxhistdb etc]# vim odbc.ini
[mysqllink]
Driver = MySQL # 驱动名 在驱动参数文件
Description = Connector/ODBC 5.3 Driver DSN #说明无所谓了
SERVER = 10.12.10.11 #服务IP地址
PORT = 3306 # MYSQL端口
USER = root #MYSQL 登录用户
PASSWORD = 123456 #登录密码
DATABASE = sharkdb #登录的数据库
OPTION = 0 #未知
TRACE = ON #跟踪
CHARSET = utf8 #MYSQL 数据库字符集
TraceFile = stderr #跟踪文件
# 检查下配置是否正确 可否登录到MYSQL
[root@jxhistdb etc]# isql mysqllink -v
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL>
SQL> select count(*) from t_fw_main; #统计MYSQL 某个表的记录
+---------------------+
| count(*) |
+---------------------+
| 85720 |
+---------------------+
SQLRowCount returns 1
1 rows fetched
# 以上说明ODBC配置没问题
[root@jxhistdb etc]# su - oracle
[oracle@jxhistdb ~]cd $TNS_ADMIN
[oracle@jxhistdb admin]vim listener.ora
配置监听:
独立模式
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME=mysqllink)
(ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1)
(PROGRAM=dg4odbc)
)
)
追加模式:
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = jxhistsv)
(ORACLE_HOME = /u01/ora_soft/oracle/product/11.2.0/db_1)
(SID_NAME = jxhistid)
)
(SID_DESC =
(SID_NAME=mysqllink)
(ORACLE_HOME=/u01/ora_soft/oracle/product/11.2.0/db_1)
(PROGRAM=dg4odbc)
)
)
重启监听
[oracle@jxhistdb admin]lsnrctl reload
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 22-1月 -2019 16:34:47
Copyright (c) 1991, 2013, Oracle. All rights reserved.
正在连接到 (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=jxhistdb)(PORT=1521)))
命令执行成功
[oracle@jxhistdb admin]lsnrctl status
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 22-1月 -2019 16:34:52
Copyright (c) 1991, 2013, Oracle. All rights reserved.
正在连接到 (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=jxhistdb)(PORT=1521)))
LISTENER 的 STATUS
别名 LISTENER
版本 TNSLSNR for Linux: Version 11.2.0.4.0 - Production
启动日期 14-1月 -2019 11:07:19
正常运行时间 8 天 5 小时 27 分 33 秒
跟踪级别 off
安全性 ON: Local OS Authentication
SNMP OFF
监听程序参数文件 /u01/ora_soft/oracle/product/11.2.0/db_1/network/admin/listener.ora
监听程序日志文件 /u01/ora_soft/oracle/diag/tnslsnr/jxhistdb/listener/alert/log.xml
监听端点概要...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=127.0.0.1)(PORT=1521)))
服务摘要..
服务 "jxhistsv" 包含 1 个实例。
实例 "jxhistid", 状态 UNKNOWN, 包含此服务的 1 个处理程序...
服务 "mysqllink" 包含 1 个实例。
实例 "mysqllink", 状态 UNKNOWN, 包含此服务的 1 个处理程序...
命令执行成功
配置网络名
MYSQL_BI =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = mysqllink)
)
(HS = OK)
)
检查网络名称
[oracle@jxhistdb admin]tnsping MYSQL_BI
TNS Ping Utility for Linux: Version 11.2.0.4.0 - Production on 22-1月 -2019 16:37:02
Copyright (c) 1997, 2013, Oracle. All rights reserved.
已使用的参数文件:
已使用 TNSNAMES 适配器来解析别名
尝试连接 (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = mysqllink)) (HS = OK))
OK (0 毫秒)
配置DG4ODBC参数文件
[oracle@jxhistdb admin]pwd
/u01/ora_soft/oracle/product/11.2.0/db_1/hs/admin
#复制参数文件,并改名成服务名称
[oracle@jxhistdb admin]cp initdg4odbc.ora initmysqllink.ora
#删除原来的内容并添加如下内容
HS_FDS_CONNECT_INFO = mysqllink # 配置文件名字
HS_FDS_TRACE_LEVEL = OFF # 关闭跟踪
HS_LANGUAGE = AMERICAN_AMERICA.ZHS16GBK # ORACLE 数据库字符集
HS_FDS_SHAREABLE_NAME =/usr/lib64/libodbc.so # 共享文件
set ODBCINI=/etc/odbc.ini # ODBC配置文件
HS_NLS_NCHAR = UCS2 #MYSQL UTF8 则UCS2
HS_LANGUAGE = AMERICAN_AMERICA.UTF8 #换成UTF8 如果MYSQL是UTF8
创建DBLINK 测试访问效果
[oracle@jxhistdb admin]sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on 星期二 1月 22 16:38:51 2019
Copyright (c) 1982, 2013, Oracle. All rights reserved.
连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> create public database link MYSQL_BI connect to "root" identified by "123456" using 'MYSQL_BI';
数据库链接已创建。
SQL> select count(*) from t_fw_main@MYSQL_BI;
COUNT(*)
85720
排除:
一 创建DBLINKE:注意别用PL/SQLDEVE 工具 会让用户名大写,MYSQL 用户名大小写敏感
create public database link MYSQL_BI connect to "SON_ROOT" identified by "son_root" using 'MYSQL_BI';
二 注意环境变量
export ODBCINI=/data/dblink/etc/odbc.ini
export ODBCSYSINI=/data/dblink/etc
export ODBCINSTINI=/data/dblink/etc/odbc.ini
一般情况下一个干净的系统无需要设置这些环境变量的。设置这些主要是用人采用TAR.GZ 源码安装,然后存放在特定目录。
echo $PATH
检查 ORACLE用户下PATH 有哪些路径。
find / -name isql
在ROOT用户下查找 该执行程序,看下有哪些目录有它的存在。
三 UNIXODB2.24 与MYSQL 8.0连接不匹配,
会报/TMP/MYSQL.SOCK 找不到
四 DG4ODBC配置文件
MYSQLLINK.ORA 不能有空行
五开启跟踪
HS_FDS_TRACE_LEVEL = 255
HS_FDS_TRACE_FILE_NAME = myodbc5.trc
会在$ORACLE_HOME/HS/LOG 下面出现该文件 会显示详细的过程,
SQLPLUS:
SQL> select count(*) from t_fw_main@MYSQL_BI;
select count(*) from t_fw_main@MYSQL_BI
ERROR at line 1:
ORA-28500: connection from ORACLE to a non-Oracle system returned this message:
[unixODBC][Driver Manager]Data source name not found, and no default driver
specified {IM002}
ORA-02063: preceding 2 lines from TEST_MYSQL_BI
ORA-00604: error occurred at recursive SQL level 1
ORA-16000: database open for read-only access
cat myodbc5.trc:
Exiting hgocip, rc=0 at 2019/01/23-11:13:13
##>Connect Parameters (len=39)
DSN=mysqllink;
#! UID=SON_ROOT;
#! PWD=*
Exiting hgogenconstr, rc=0 at 2019/01/23-11:13:13
Entered hgopoer at 2019/01/23-11:13:13
hgopoer, line 231: got native error 0 and sqlstate IM002; message follows...
[unixODBC][Driver Manager]Data source name not found, and no default driver specified {IM002}
Exiting hgopoer, rc=0 at 2019/01/23-11:13:13
这里显示DG4ODBC 找不到ODBC的配置文件 MYSQLLINK
通过执行odbcinst -j命令可以查看当前配置信息情况,以确认相关配置信息的路径是否正确。
[root@rac_dg bin]# odbcinst -j
unixODBC 2.2.14
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /root/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8