今天业务需要,要跨库查询Oracle数据,于是想到了dblink, 下面一一细说,Oracle DBLink。
首先Oracle DBLink 分成 如下三种:
Private 创建database link的user拥有该database link 在本地数据库的特定的schema下建立的database link。只有建立该database link的schema的session能使用这个database link来访问远程的数据库。同时也只有Owner能删除它自己的private database link。Public Owner是PUBLIC. Public的database link是数据库级的,本地数据库中所有的拥有数据库访问权限的用户或pl/sql程序都能使用此database link来访问相应的远程数据库
Global Owner是PUBLIC. Global的database link是网络级的,When an Oracle network uses a directory server, the directory server automatically create and manages global database links (as net service names) for every Oracle Database in the network. Users and PL/SQL subprograms in any database can use a global link to access objects in the corresponding remote database.
Note: In earlier releases of Oracle Database, a global database link referred to a database link that was registered with an Oracle Names server. The use of an Oracle Names server has been deprecated. In this document, global database links refer to the use of net service names from the directory server.
创建dblink:
一:创建 public database link 的实例:
create public database link todb151
connect to hezi identified by manager
using '(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST =192.168.4.151)(PORT = 18000))
)
(CONNECT_DATA =
(SERVICE_NAME = newzqdb)
)
)';
应该注意的是:
1,上面的hezi 这个用户,是你要连接的远程数据库中的用户(也就是 4.151上的)。
2,SQL> select * from pd_zh_cn.tb_goods@todb151; 通过todb151来跨库查询4.151,会拥有hezi的查询权限。只要本地的用户拥有数据库访问权限即可,所以尽量不要建public数据库连接,或者不要用大权限用户hezi来创建,以免让本地小权限的用户,去远程4.151查看到了不该看到的信息。
3.using 后面的引号中间的内容实际上就有 本地tnsname.ora 中的相应数据库连接信息。所以这里也可以直接改写成:
create public database link todb151
connect to hezi identified by manager
using 'db151' ;
前提是本地tnsname.ora 文件中有:
db151 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.4.151)(PORT = 18000))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = newzqdb)
)
)
其中SERVICE_NAME 一般是下面红字部分,
SQL> show parameter service_names
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
service_names string newzqdb
还要注意要和监听的服务名一致,因为这个远程是要借助监听的。这里明确的告诉大家,配置静态监听注册时,需要输入的全局数据库名(GLOBAL_DBNAME)输入什么都可以,只要保证listerner.ora中的GLOBAL_DBNAME和tnsnames.ora中的SERVICE_NAME保持一致,所以如果是静态监听,那么在创建dblink时应该也应该符合,service_name =listerner.ora中的GLOBAL_DBNAME ,(创建dblink时不一定非得一样)
[oracle@rac1 ~]$ lsnrctl status
LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 01-JUL-2015 21:39:53
Copyright (c) 1991, 2009, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=newzq.com.cn)(PORT=18000)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 11.2.0.1.0 - Production
Start Date 01-JUL-2015 20:18:41
Uptime 0 days 1 hr. 21 min. 12 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /u01/oracle/products/database/11.2.0.1/network/admin/listener.ora
Listener Log File /u01/oracle/products/diag/tnslsnr/newzq/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=newzq.com.cn)(PORT=18000)))
Services Summary...
Service "newzqdb" has 1 instance(s).
Instance "newzqdb", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
二:创建 private dblink 实例:注意没有private字眼。
create database link dblink
connect to liuwenhe identified by liuwenhe
using '(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.4.151)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = newzqdb)
)
)';
管理dblink:
在当前数据库下查看所有的DBLINK的方法:
1. select * from dba_db_links;
删除当前数据库下的一个指定的DBLINK的方法:
1. 删除public link
drop public database link dblink;
2. 删除private link
drop database link dblink;
ORA-02085 解决办法 :