客户需求oracle访问mysql数据,客户是linux7.3 11.2.0.4单实例,字符集GBK,mysql是5.7.31,字符集UTF8,下面结合网上的文档和自己的实践,配置过程如下
1.安装oracle透明网关
首先在oracle服务器上面安装oracle透明网关,在oracle的安装包的第5个包:
p13390677_112040_Linux-x86-64_5of7.zip
安装很简单,下一步就行了,在安装选项那里勾选for odbc即可。
2.在oracle服务器上安装odbc和mysql驱动
这里挂载镜像iso,然后使用yum进行安装,也比较简单
yum install unixODBC* mysql* mariadb*
主要是安装下面这些包:
安装mariadb是为了可以使用mysql命令行来连接测试。
3.配置mysql DSN连接
配置一下驱动文件的软连接:
cd /usr/lib64/
ln -s libodbcinst.so.2.0.0 libodbcinst.so.1
ln -s libodbc.so.2.0.0 libodbc.so.1
查看odbc配置文件
[oracle@db66 oradata]$ odbcinst -j
unixODBC 2.3.1
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /home/oracle/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
查看一下mysql的驱动是否都安装了:
[oracle@db66 oradata]$ 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/psqlodbcw.so
Setup = /usr/lib/libodbcpsqlS.so
Driver64 = /usr/lib64/psqlodbcw.so
Setup64 = /usr/lib64/libodbcpsqlS.so
FileUsage = 1
# Driver from the mysql-connector-odbc package
# Setup from the unixODBC package
#####这里下面的64位驱动是否都有,正常安装了mysql-connect-odbc之后就都有了
[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
修改odbc.ini配置文件,这一步是关键点1,配置连接mysql的odbc DSN:
[oracle@db66 oradata]$ cat /etc/odbc.ini
[vte]
Driver=MySQL #这里是上面odbcinst.ini里面的驱动名称,下面其它的都是很简单mysql的连接信息
SERVER=192.168.50.104
PORT=3306
USER=vte1
PASSWORD=611048sl
DATABASE=vtedb
OPATION=0
TRACE=OFF
配置完成之后使用isql测试连接是否正常:
[oracle@db66 oradata]$ isql vte
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL> select version();
+-----------+
| version() |
+-----------+
| 5.7.31-log|
+-----------+
SQLRowCount returns 1
1 rows fetched
这里表示连接正常。
4.配置oracle透明网关配置文件
cd $ORACLE_HOME/hs/admin
vi initodbcvte.ora
这里注意这个文件的名称叫initodbcvte.ora,这里的odbcvte是第二个关键点,需要和后面监听里面的保持一致
[oracle@db66 admin]$ pwd
/u01/app/oracle/product/11.2.0/dbhome_1/hs/admin
[oracle@db66 admin]$ cat initodbcvte.ora
# This is a sample agent init file that contains the HS parameters that are
# needed for the Database Gateway for ODBC
#
# HS init parameters
#
HS_FDS_CONNECT_INFO = vte ##这里是odbc.ini里面的DSN名称
HS_FDS_TRACE_LEVEL = ON
HS_FDS_SHAREABLE_NAME = /usr/lib64/libodbc.so
HS_LANGUAGE=AMERICAN_AMERICA.UTF8 ##这两段如果不配会出现乱码
HS_NLS_NCHAR=UCS2
#
# ODBC specific environment variables
#
set ODBCINI= /etc/odbc.ini
#
# Environment variables required for the non-Oracle system
#
#set =
5.配置监听和tnsnames
配置listener.ora文件,这里为了不和现在的监听冲突,我选择新建一个端口号为1522的listener2,这里的SID需要和配置文件的文件名保持一致
[oracle@db66 admin]$ cat listener.ora
# listener.ora Network Configuration File: /u01/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora
# Generated by Oracle configuration tools.
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = db66)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
ADR_BASE_LISTENER = /u01/app/oracle
LISTENER2 =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.50.66)(PORT = 1522))
)
)
SID_LIST_LISTENER2=
(SID_LIST=
(SID_DESC=
(SID_NAME=odbcvte)
(ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1)
(PROGRAM=dg4odbc)
)
)
配置tnsnames.ora连接此listener的服务命名:
odbcvte=
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.50.66)(PORT = 1522))
(CONNECT_DATA =
(SID = odbcvte)
)
(HS = OK)
)
配置完成之后启动listener2,并测试
lsnrctl start listener2
再用tnsping odbcvte测试:
[oracle@db66 admin]$ tnsping odbcvte
TNS Ping Utility for Linux: Version 11.2.0.4.0 - Production on 25-JUN-2023 16:16:14
Copyright (c) 1997, 2013, Oracle. All rights reserved.
Used parameter files:
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.50.66)(PORT = 1522)) (CONNECT_DATA = (SID = odbcvte)) (HS = OK))
OK (0 msec)
6. 在数据库创建dblink
SQL> create public database link to_vte connect to "root" identified by "123456" using 'odbcvte';
这里注意,mysql是区分大小写的,所以这里的用户名和密码需要加双引号
7.验证dblink
8.遇到的问题
在oracle中连接到mysql之后数据都是乱码。需要在initodbcvte.ora中配置语言:
HS_LANGUAGE=AMERICAN_AMERICA.UTF8
HS_NLS_NCHAR=UCS2
实测,只要配置第二个就不会出现中文乱码