<< 返回文章列表 本特性自 MogDB 5.0.0版本开始引入,支持 Oracle DBLink语法,可以使用@符号访问 Oracle 数据库中的表。
示 例
01
环境准备
MogDB 环境
Oracle 环境
创建测试表:
插入测试数据:
02
验证步骤
① 登录 MogDB 数据库
以数据库postgres,端口号26000为例:
② 创建用户
③ 用户创建用户映射密钥文件
④ 以test用户登录 MogDB 数据库
⑤ 创建 MogDB 数据库到 Oracle 数据库的DBLink连接
⑥ 创建用户映射
⑦ 通过DBLink在 MogDB 数据库中查询 Oracle 表
⑧ 通过DBLink操作数据
⑨ 查看执行计划
MogDB 对 Oracle DBLink兼容性增强
2024年3月27日
许玉晨
63
-
已安装 MogDB 数据库。 -
已安装oracle_fdw插件,具体安装方法参见oracle_fdw安装文档
https://docs.mogdb.io/zh/mogdb/v5.0/1-oracle_fdw
CREATE TABLE scott.EMPLOYEE (
ID INT PRIMARY KEY,
NAME VARCHAR2(50) NOT NULL,
SALARY NUMBER(10,2)
);
INSERT INTO scott.EMPLOYEE (ID, NAME, SALARY) VALUES (1001, 'Mike', 5000);
INSERT INTO scott.EMPLOYEE (ID, NAME, SALARY) VALUES (1002, 'JACK', 6000);
[omm@master01 ~]$ gsql -d postgres -p 26000 -r
gsql ((MogDB 5.0.4 build 070c88a0) compiled at 2023-11-25 12:57:09 commit 0 last mr 1804 )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
MogDB=#
赋予用户sysadmin权限,以用户名test为例:
MogDB=# create user test identified by 'Enmo@123';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
MogDB=# alter user test sysadmin;
ALTER ROLE
[omm@master01**粗体** oracle_file]$ gs_guc generate -S 'xxxx@123' -D $GAUSSHOME/bin -o usermapping -U test
The gs_guc run with the following arguments: [gs_guc -S ******** -D /data/mogdb500/app/bin -o usermapping -U test generate ].
gs_guc generate -S *** -U ***
-S 表示自定义密钥,例如xxxx@123
[omm@master01 ~]$ gsql -d postgres -p 26000 -r -U test -W 'Enmo@123'
gsql ((MogDB 5.0.4 build 070c88a0) compiled at 2023-11-25 12:57:09 commit 0 last mr 1804 )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help
MogDB=>
以IP地址10.211.55.21,端口号1521,数据库名orcl为例:
MogDB=> CREATE SERVER dblink_to_orcl FOREIGN DATA WRAPPER oracle_fdw OPTIONS(dbserver '10.211.55.21:1521/orcl');
CREATE SERVER
为 MogDB 数据库用户test和 Oracle 数据库用户scott建立映射关系:
MogDB=> create user mapping for test server dblink_to_orcl options(user 'scott',password 'xxx123');
CREATE USER MAPPING
MogDB=> SELECT * FROM scott.EMPLOYEE@dblink_to_orcl;
id | name | salary
------+------+---------
1001 | Mike | 5000.00
1002 | JACK | 6000.00
rows)
MogDB=> SELECT * FROM scott.EMPLOYEE@dblink_to_orcl WHERE SALARY > 400;
id | name | salary
------+------+---------
1001 | Mike | 5000.00
1002 | JACK | 6000.00
(2 rows)
在 MogDB 数据库中对 Oracle 数据库表执行更新、插入、删除、查询操作:
MogDB=> UPDATE scott.EMPLOYEE@dblink_to_orcl SET SALARY = 5500 WHERE ID = 1001;
UPDATE 1
MogDB=> insert into scott.EMPLOYEE@dblink_to_orcl values (1003, 'JANE', 7000);
INSERT 0 1
MogDB=> DELETE FROM scott.EMPLOYEE@dblink_to_orcl WHERE SALARY =6000;
DELETE 1
MogDB=> select * from scott.EMPLOYEE@dblink_to_orcl;
id | name | salary
------+------+---------
1003 | JANE | 7000.00
1001 | MIKE | 5500.00
(2 rows)
MogDB=> explain select * from scott.EMPLOYEE@dblink_to_orcl;
QUERY PLAN
-----------------------------------------------------------------------------------------------
-------------------
Foreign Scan on "scott.employee@dblink_to_orcl" employee (cost=10000.00..20000.00 r
ows=1000 width=78)
Oracle query: SELECT /*66abc20a4a7895b75898e391381f9de8*/ r1."ID",r1."NAME",r1."SALARY" FROM
scott.employee r1
(2 rows)