????????达梦数据库的外部链接是一种数据库的实体对象,记录了远程数据库的连接信息,用于建立与远程数据的联系。用户可以通过数据库的外部链接对象透明地操作远程数据库的数据,对远程数据库的表进行查询和增删改操作,也可以调用远程的存储过程。数据库的外部链接对象可以是公用的(数据库中所有用户使用),也可以是私有的(特定用户使用)
一、同构数据库链接DM8 到 DM8
A库:192.168.0.153 ?(被连接服务器)
B库:192.168.0.144 (连接服务器)
两台服务器都安装DM8数据库,实例名不能一致,数据库字符集必须一致,两台服务器配置dm数据库mal系统
修改dm.ini 文件开启mal组件,并编辑mal配置文件(两台服务器配置一致)
[root@ecs-301110 ~]# cat /dm8/data/DAMENG/dmmal.ini
[MAL_INST1]
MAL_INST_NAME = PROD #实例名,和 dm.ini 中的 INSTANCE_NAME 一致
MAL_HOST = 192.168.0.153 #MAL 系统监听 TCP 连接的 IP 地址
MAL_PORT = 61141 #MAL 系统监听 TCP 连接的端口
MAL_INST_HOST = 192.168.0.153 #实例的对外服务 IP 地址
MAL_INST_PORT = 5237 #实例的对外服务端口,和 dm.ini 中的 PORT_NUM 一致
[MAL_INST2]
MAL_INST_NAME = PROD02
MAL_HOST = 192.168.0.144
MAL_PORT = 61142
MAL_INST_HOST = 192.168.0.144
MAL_INST_PORT = 5237
#注:两个数据库配置文件必须一致,实例名不能一样
?编辑好配置文件重启两台数据库
[root@ecs-141585 DAMENG]# systemctl restart DmServicePROD
在B库创建公有dblink连接操作A库
示例:create public link 外部连接名 connect with A主机登录用户 identified by A主机登录用户密码 using 'A主机ip/a主机系统监听TCP连接端口'
#公有dblink(所有用户可使用)
CREATE PUBLIC LINK iplink1 CONNECT WITH "SYSDBA" IDENTIFIED BY "Dameng123" USING '192.168.0.153/61141';
#私有dblink(谁创建谁使用)
CREATE LINK iplink1 CONNECT WITH "SYSDBA" IDENTIFIED BY "Dameng123" USING '192.168.0.144/61142';
#可以使用下面的语句查看dblink
select * from dba_db_links;
在A库创建一个测试表,并添加一条数据
?
?测试在B库是否可以insert 及select A库的test表
在A库查看是否有数据?
?二、Oracle到DM8
? ? ? ? Oracle需要使用ODBC连接DM8,所以要先安装ODBC。
????????ODBC为异构数据库访问提供统一接口,允许应用程序以SQL为数据存取标准,存取不同DBMS管理的数据;使应用程序直接操纵DB中的数据,免除随DB的改变而改变。
#检查是否安装gcc
rpm -ap |grep gcc
#解压安装包
tar -xzvf unixODBC-2.3.0.tar.gz
#配置ODBC
./configure --enable-gui=no
#编译安装ODBC
make && make install
? ? ? ? ODBC默认安装在/usr/local/etc/,进入/usr/local/etc/目录,可以看到odbc.ini和odbcinst.ini两个文件,有特殊情况可能会安装在/etc目录下,可以输入odbcinst -j进行确认配置文件
? ? ? ? 查看 odbc 配置文件路径 ? ? ? ? ? ? ? ? odbcinst -j
????????查看 odbc 版本 ????????????????odbc_config --version
?
?
配置 odbc.ini 和 odbcinst.ini
[dmdba@ecs-301110 bin]$ cd /usr/local/etc/
[dmdba@ecs-301110 etc]$ cat odbc.ini
[dm8]
Description = DM ODBS DSND
Driver = DM8 ODBC DRIVER #需要和odbcinst.ini 文件中标题一样
SERVER = 127.0.0.1 #数据库地址
UID = TEST #数据库用户
PWD = Dameng123 #数据库TEST用户密码
TCP_PORT = 5237 #数据库端口号
[dmdba@ecs-301110 etc]$ cat odbcinst.ini
[DM8 ODBC DRIVER] #需要和odbc.ini文件中 Driver参数一样
Description = ODBC DRIVER FOR DM8
DRIVER = /dm8/bin/libdodbc.so
验证ODBC
[dmdba@ecs-301110 etc]$ isql dm8
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL>
注意事项:
? ? ? ? 1、在oracle服务器如果没有安装达梦客户端,可以安装一个或者直接从安装好的达梦里面直接拷贝bin目录上传。
? ? ? ? 2、需要注意libdodbc.so文件,oracle需要有权限去访问这个文件。可以在oracle的/home/oracle/.bash_profile中增加达梦bin目录的路径,例如:
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib:$ORACLE_HOME/dmbin
编辑透明网关文件
[root@ecs-301110 ~]# cd /u01/app/oracle/product/11.2.0/hs/admin/
[root@ecs-301110 admin]# cat initdm8.ora
#HS_FDS_CONNECT_INFO=dm8
#HS_FDS_TRACE_LEVEL = debug
#HS_FDS_SHAREABLE_NAME=/usr/local/lib/libodbc.so
#HS_FDS_SUPPORT_STATISTICS=FALSE
#HS_LANGUAGE="simplified chinese_china.al32utf8"
#HS_NLS_NCHAR=UCS2
#set ODBCINI=/usr/local/etc/odbc.ini
HS_FDS_CONNECT_INFO = dm8
HS_FDS_TRACE_LEVEL = debug
HS_FDS_SUPPORT_STATISTICS=FALSE
HS_FDS_TRACE_LEVEL = 255
set ODBCINI=/usr/local/etc/odbc.ini
HS_FDS_SHAREABLE_NAME =/usr/local/lib/libodbc.so
HS_LANGUAGE =American_America.ZHS16GBK
[root@ecs-301110 admin]#
配置静态监听listener.ora和 tnsnames.ora(如果文件不存在则新建)
[root@ecs-301110 listener]# cat /u01/app/oracle/product/11.2.0/network/admin/tnsnames.ora
# tnsnames.ora Network Configuration File: /u01/app/oracle/product/11.2.0/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
dm8 =
(DESCRIPTION=
(ADDRESS=
(PROTOCOL=TCP) (HOST=localhost) (PORT=1521)
)
(CONNECT_DATA=
(SERVICE_NAME = dm8)
(SID=dm8)
)
(HS=OK)
)
[root@ecs-301110 listener]# cat /u01/app/oracle/product/11.2.0/network/admin/listener.ora
# listener.ora Network Configuration File: /u01/app/oracle/product/11.2.0/network/admin/listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER=
(SID_LIST=
(SID_DESC=
(SID_NAME=dm8)
(ORACLE_HOME=/u01/app/oracle/product/11.2.0)
(PROGRAM=dg4odbc)
(ENVS=LD_LIBRARY_PATH=/u01/app/oracle/product/11.2.0/lib:/lib:/usr/lib:/dm8/bin)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
)
ADR_BASE_LISTENER = /u01/app/oracle
[root@ecs-301110 listener]#
?重启监听并测试是否可以识别
[oracle@ecs-301110 ~]$ lsnrctl stop
LSNRCTL for Linux: Version 11.2.0.3.0 - Production on 30-AUG-2022 14:50:26
Copyright (c) 1991, 2011, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))
The command completed successfully
[oracle@ecs-301110 ~]$
[oracle@ecs-301110 ~]$ lsnrctl start
LSNRCTL for Linux: Version 11.2.0.3.0 - Production on 30-AUG-2022 14:50:37
Copyright (c) 1991, 2011, Oracle. All rights reserved.
Starting /u01/app/oracle/product/11.2.0/bin/tnslsnr: please wait...
TNSLSNR for Linux: Version 11.2.0.3.0 - Production
System parameter file is /u01/app/oracle/product/11.2.0/network/admin/listener.ora
Log messages written to /u01/app/oracle/diag/tnslsnr/ecs-301110/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=ecs-301110)(PORT=1521)))
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 11.2.0.3.0 - Production
Start Date 30-AUG-2022 14:50:37
Uptime 0 days 0 hr. 0 min. 0 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /u01/app/oracle/product/11.2.0/network/admin/listener.ora
Listener Log File /u01/app/oracle/diag/tnslsnr/ecs-301110/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=ecs-301110)(PORT=1521)))
Services Summary...
Service "dm8" has 1 instance(s).
Instance "dm8", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
[oracle@ecs-301110 ~]$
[oracle@ecs-301110 ~]$ lsnrctl start
LSNRCTL for Linux: Version 11.2.0.3.0 - Production on 30-AUG-2022 14:51:14
Copyright (c) 1991, 2011, Oracle. All rights reserved.
TNS-01106: Listener using listener name LISTENER has already been started
创建dblink并验证是否可用
报错排查:
如果把initsid.ora里的HS_FDS_TRACE_LEVEL设置为off,一旦出现错误,不好排查问题。配置中遇到了问题:
ORA-28500: connection from ORACLE to a non-Oracle system returned this message:
ORA-02063: preceding line from <dblinkname>
看不出具体报错信息,而且后面测试发现很多情况都会这样报错,比如:init.ora文件名中的sid写错了、配置文件中的字符集有问题、动态库文件找不到,都会报这样的错误。 配置成debug后,在目录中可以看到详细的报错原因,方便排查问题所在。
[oracle@ecs-301110 ~]$ cd /u01/app/oracle/product/11.2.0/hs/log/
[oracle@ecs-301110 log]$ ls
dm8_agt_10056.trc dm8_agt_13821.trc dm8_agt_6709.trc
[oracle@ecs-301110 log]$
例如:测试中写错libdodbc.so文件名(写成libodbc.so)
[oracle@ecs-301110 ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on Fri Jul 30 15:48:14 2021
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SYS@orcl> select * from v$version@dmlink;
select * from v$version@dmlink1
*
ERROR at line 1:
ORA-28500: connection from ORACLE to a non-Oracle system returned this message:
ORA-02063: preceding line from DMLINK1
报错信息是看不出来问题的。 查看日志可以看到:
Oracle Corporation --- FRIDAY JUL 30 2021 13:57:22.023
Version 11.2.0.1.0
Entered hgogprd
HOSGIP for "HS_FDS_TRACE_LEVEL" returned "debug"
Entered hgosdip
setting HS_OPEN_CURSORS to default of 50
.......
Exiting hgosdip, rc=0
ORACLE_SID is "dm8"
Product-Info:
Port Rls/Upd:1/0 PrdStat:0
Agent:Oracle Database Gateway for ODBC
......
Failed to load ODBC library symbol: /usr/lib/libodbc.so(SQLAllocHandle)
......
?
?
|