在oracle数据库访问mysql数据库数据
环境
Oracle 数据库: 192.168.22.6 端口1521 操作系统:RHEL 7.6
MYSQL 数据库: 192.168.22.9 端口3306 操作系统:RHEL 7.6 用户:lanmc,密码:lanmc123 ,数据库 lanmc
ORACLE 数据库配置
- 1、oracle服务端安装mysql odbc
mysql odbc下载地址:https://dev.mysql.com/downloads/connector/odbc/
rpm -ivh mysql-connector-odbc-8.0.31-1.el7.x86_64.rpm
warning: mysql-connector-odbc-8.0.31-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-connector-odbc-8.0.31-1.el7################################# [100%]
[root@t12c ~]# rpm -ql mysql-connector-odbc-8.0.31-1.el7.x86_64
/usr/bin/myodbc-installer
/usr/lib64/libmyodbc8a.so
/usr/lib64/libmyodbc8w.so
/usr/share/doc/mysql-connector-odbc-8.0.31
/usr/share/doc/mysql-connector-odbc-8.0.31/ChangeLog
/usr/share/doc/mysql-connector-odbc-8.0.31/INFO_BIN
/usr/share/doc/mysql-connector-odbc-8.0.31/INFO_SRC
/usr/share/doc/mysql-connector-odbc-8.0.31/LICENSE.txt
/usr/share/doc/mysql-connector-odbc-8.0.31/README.txt
- 2、安装最新的unixODBC
下载地址:http://www.unixodbc.org/
1. copy the unixODBC-2.3.11.tar.gz file somewhere you can create files and directories
2. gunzip unixODBC*.tar.gz
3. tar xvf unixODBC*.tar
./configure ## 指定路径安装 ./configure --prefix=/usr/local/unixODBC
make
make install
[root@t12c ~]# odbcinst -j
unixODBC 2.3.11
DRIVERS............: /usr/local/etc/odbcinst.ini
SYSTEM DATA SOURCES: /usr/local/etc/odbc.ini
FILE DATA SOURCES..: /usr/local/etc/ODBCDataSources
USER DATA SOURCES..: /root/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
检查路径是否为配置文件路径;如不是
1.直接在 /usr/local/etc/ 目录下配置odbcinst.ini 和 odbc.ini.
2.在其他目录配置,之后使用odbcinst 命令加载2个配置。
odbcinst -i -d -f /etc/odbc.ini
odbcinst -i -s -l -f /etc/odbc.ini
- 3、在/etc目录下创建odbc.ini文件
[root@t12c ~]# vi /etc/odbc.ini
[MYSQL]
Driver = /usr/lib64/libmyodbc8w.so ## mysql odbc的lib
Description = Connector ODBC 8
SERVER = 192.168.22.9
PORT = 3306
USER = lanmc
Password = lanmc123
Database = lanmc
OPTION = 0
TRACE = OFF
CHARSET = UTF8
## 测试连接
[root@t12c ~]# isql MYSQL
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL> select * from t1;
+-----------+-----------+
| id1 | id2 |
+-----------+-----------+
| 3333 | 6412 |
+-----------+-----------+
SQLRowCount returns 1
1 rows fetched
SQL> quit()
- 4、配置透明网关
[oracle@t12c admin]$ vi initMYSQL.ora
HS_FDS_CONNECT_INFO = MYSQL
HS_FDS_TRACE_LEVEL = OFF
HS_FDS_SHAREABLE_NAME = /usr/lib64/libodbc.so ## 该参数值应该为unixodbc的lib包,如果配置为mysql odbc的lib包,查询时会报错ORA-28500
HS_NLS_NCHAR = UCS2
HS_LANGUAGE=AMERICAN_AMERICA.AL32UTF8 ## 两边数据库的字符集都为utf8
set ODBCINI=/etc/odbc.ini
#HS_LANGUAGE = AMERICAN_AMERICA.WE8ISO8859P15
set LD_LIBRARY_PATH=/usr/lib64/
- 5、编辑监听文件
[oracle@t12c admin]$ vi listener.ora
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME =STU_DGMGRL)
(ORACLE_HOME = /oracle/app/product/12.2.0/db)
(SID_NAME =stu12c)
)
(SID_DESC =
(GLOBAL_DBNAME =stu12c)
(ORACLE_HOME = /oracle/app/product/12.2.0/db)
(SID_NAME =stu12c)
)
(SID_DESC =
(SID_NAME =MYSQL)
(ORACLE_HOME = /oracle/app/product/12.2.0/db)
(PROGRAM=dg4odbc)
(ENVS=LD_LIBRARY_PATH="/usr/lib64: /oracle/app/product/12.2.0/db/lib")
)
)
- 6、编辑$ORACLE_HOME/network/admin/tnsnames.ora
[oracle@t12c admin]$ vi tnsnames.ora
MYSQL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.22.6)(PORT = 1521))
(CONNECT_DATA =
(SID = MYSQL)
)
(HS = OK)
)
[oracle@t12c admin]$ lsnrctl reload
LSNRCTL for Linux: Version 12.2.0.1.0 - Production on 20-DEC-2022 19:55:19
Copyright (c) 1991, 2016, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.22.6)(PORT=1521)))
The command completed successfully
[oracle@t12c admin]$ lsnrctl status
LSNRCTL for Linux: Version 12.2.0.1.0 - Production on 20-DEC-2022 19:55:23
Copyright (c) 1991, 2016, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.22.6)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 12.2.0.1.0 - Production
Start Date 20-DEC-2022 16:13:28
Uptime 0 days 3 hr. 41 min. 54 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /oracle/app/product/12.2.0/db/network/admin/listener.ora
Listener Log File /oracle/app/diag/tnslsnr/t12c/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.22.6)(PORT=1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "MYSQL" has 1 instance(s).
Instance "MYSQL", status UNKNOWN, has 1 handler(s) for this service...
Service "PG" has 1 instance(s).
Instance "PG", status UNKNOWN, has 1 handler(s) for this service...
Service "STU_DGMGRL" has 1 instance(s).
Instance "stu12c", status UNKNOWN, has 1 handler(s) for this service...
Service "destdb" has 1 instance(s).
Instance "stu12c", status READY, has 1 handler(s) for this service...
Service "df66fa250c1347e7e0530616a8c0b8a4" has 1 instance(s).
Instance "stu12c", status READY, has 1 handler(s) for this service...
Service "e7fa5abedc356516e0530616a8c06294" has 1 instance(s).
Instance "stu12c", status READY, has 1 handler(s) for this service...
Service "stat_test" has 1 instance(s).
Instance "stu12c", status READY, has 1 handler(s) for this service...
Service "stu12c" has 2 instance(s).
Instance "stu12c", status UNKNOWN, has 1 handler(s) for this service...
Instance "stu12c", status READY, has 1 handler(s) for this service...
Service "stu12cXDB" has 1 instance(s).
Instance "stu12c", status READY, has 1 handler(s) for this service...
Service "stu12c_CFG" has 1 instance(s).
Instance "stu12c", status READY, has 1 handler(s) for this service...
Service "stu12c_DGB" has 1 instance(s).
Instance "stu12c", status READY, has 1 handler(s) for this service...
The command completed successfully
[oracle@t12c admin]$ tnsping MYSQL
TNS Ping Utility for Linux: Version 12.2.0.1.0 - Production on 20-DEC-2022 19:55:45
Copyright (c) 1997, 2016, Oracle. All rights reserved.
Used parameter files:
/oracle/app/product/12.2.0/db/network/admin/sqlnet.ora
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.22.6)(PORT = 1521)) (CONNECT_DATA = (SID = MYSQL)) (HS = OK))
OK (0 msec)
SQL> create public database link mysql_link connect to "lanmc" identified by "lanmc123" using 'mysql';
Database link created.
SQL> select * from "t1"@mysql;
id1 id2
---------- ----------
3333 6412
标签:stu12c,...,odbc,dblink,usr,mysql,oracle
From: https://blog.51cto.com/u_13482808/7969742