一、用管理员账号给该用户授权(创建dblink的权限)

我们想要创建dblink前提是该用户有创建dblink的权限。如果没有权限是创建不了dblink的。那么我们只需要用管理员账号给该用
户授予相应的权限即可。

grant create public database link to username;

二、创建dblink的语法

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数据库名称

本机测试:

CREATE PUBLIC DATABASE LINK db_link_myLink CONNECT TO username IDENTIFIED BY password USING '(DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST =192.168.124.20)(PORT =1521 ))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = orcl)
    )
  )';

三、查询语法

select t.* from t_user@db_link_myLink t where t.realName like '陈%';

更多推荐