1.什么是dblink

dblink在一个数据库中访问另一个数据库建立起来的通道。

dbLink是简称,全称是databaselink,翻译过来就是数据库链接的意思。它主要是用来做跨库访问的。比如说我现在有一个A数据库,还有一个B数据库,那我我现在想在A库中访问B数据库中的数据,就可以在A库中创建一个dblink来链接到B数据库。这样我们在A数据库中就可以直接对B中的数据进行查询,访问,修改。

2.语法:

CREATE PUBLIC DATABASE LINK db_link_name CONNECT TO username IDENTIFIED BY password USING '(DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST =visist_IP)(PORT =visit_port ))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = db_name)
    )
  )';
  --参数说明
  --username 需要访问数据用户名, password  密码, 
  visit_ip  需要访问数据库的ip,visit_port 窗口, 
  db_name数据库名称

3.例子:

241的dsdta和dsdemo用户可以访问196的linkdb的所有表

241执行
create public database link  to_196orcl connect to linkdb identified by linkdb using 'orcl';

orcl =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.196)(PORT = 1577))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  )

196 linkd 可以访问241 dsdta和dsdemo的ooeg_t,ooefl_t,faah_t,faaj_t,inag_t,imaal_t的表查询权限

241执行
create user linkdb identified by "linkdb!241";
grant connect,resource to linkdb;
grant select on DSDEMO.FAAH_T to linkdb;
grant select on DSDEMO.FAAJ_T to linkdb;
grant select on DSDEMO.IMAAL_T to linkdb;
grant select on DSDEMO.INAG_T to linkdb;
grant select on DSDEMO.OOEFL_T to linkdb;
grant select on DSDEMO.OOEG_T to linkdb;
grant select on DSDATA.FAAH_T to linkdb;
grant select on DSDATA.FAAJ_T to linkdb;
grant select on DSDATA.IMAAL_T to linkdb;
grant select on DSDATA.INAG_T to linkdb;
grant select on DSDATA.OOEFL_T to linkdb;
grant select on DSDATA.OOEG_T to linkdb;
196执行
create public database link  to_241 connect to linkdb identified by "linkdb!241" using 'topprd';
topprd =
 (DESCRIPTION =
   (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.242)(PORT = 1521))
   (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.244)(PORT = 1521))
   (CONNECT_DATA =
    (SERVER = DEDICATED)
    (SERVICE_NAME = topprd)
    )
  )

如果想要赋予更多的权限,可以

grant insert,update,delete on 表名 to 用户名

通过赋予连接的用户权限来实现对表的一系列操作,这样更加的安全。

Related Posts