首页  ·  知识 ·  生产制造
dblink配置
网友   http://hi.baidu.com/leexiaodong/blog    编辑:德仔   图片来源:网络
blog_text cnt假设有2台oracle数据库,一台叫test,一台叫prod,都是9204。 其中tes
假设有2台oracle数据库,一台叫test,一台叫prod,都是9204。
其中test在window xp平台,prod在RHEL AS3。

test:
db_name=dba
db_domain=world
glabal_name=dba.world


prod:
db_name=ora9i
db_domain=oracle.com
global_name=ora9i.oracle.com


db link指向是: test -> prod

假设test和prod数据库上都有scott和mhung用户。
test上指向prod的网络连接串是as3。

db link 有3种类型,我这里只讨论其中两种,connected user和fixed user。
connected user,简单来说,test数据库以connected的用户来连接远程数据库(prod)。
fixed user,简单来说,test数据库以fixed(指定的)用户来连接远程数据库(prod)。

看看创建db link语法,你对这两种类型就比较清楚了。

创建fixed user的db link语法:
create databas link foo connect to scott identified by tiger using 'as3' ;

创建fixed user的db link语法:
create databas link foo using 'as3' ;

db link 的命名和global_names有关,
如果global_names=true(test),那么db link的命名要和远程数据库(prod)的global_name相同;
如果global_names=false(test),那么你可以随便命名db link。
请注意,是test数据库的global_names,与prod数据库的global_names无关。

global_name是数据库全局名称,global_name在你所管理的数据库中要保证唯一。
数据库名称是db_name。数据库名称一般都取得比较短,我的习惯一般取长度4个字符,重名概率高。
比如你有2个数据库的db_name都是prod,重名啦。
所以,oracle模仿域名搞出一个global_name,global_name=db_name+db_domain。
有了global_name,就可以实现数据库命名的全局唯一。

4种情况如下表。

global_names(test数据库) db link type
-----------------          -------------
1. false                      connected user
2. false                      fixed user
3. true                      connected user
4. true                      fixed user


1. global_names=false, link type=connected user

scott@DBA.WORLD> create database link foo using 'as3' ;

Database link created.

Elapsed: 00:00:00.00
scott@DBA.WORLD> select * from global_name ;

GLOBAL_NAME
--------------------------------------------------------

DBA.WORLD

Elapsed: 00:00:00.00
scott@DBA.WORLD> select * from global_name@foo ;

GLOBAL_NAME
--------------------------------------------------------

ORA9I.ORACLE.COM

Elapsed: 00:00:00.04
scott@DBA.WORLD>

2. global_names=false, link type=fixed user

scott@DBA.WORLD> create database link woo connect to mhung identified by huang using 'as3' ;

Database link created.

Elapsed: 00:00:00.00
scott@DBA.WORLD> select * from global_name@woo ;

GLOBAL_NAME
----------------------------------------------------------------------------------------------

ORA9I.ORACLE.COM

Elapsed: 00:00:00.04
scott@DBA.WORLD> select *from global_name ;

GLOBAL_NAME
----------------------------------------------------------------------------------------------

DBA.WORLD

Elapsed: 00:00:00.00
scott@DBA.WORLD>

以下测试global_names=true的情况,
         
3. global_name=true, link type=connected user

sys@DBA.WORLD> show parameter global

NAME                               TYPE        VALUE
------------------------------------ ----------- ----------------
global_context_pool_size          string
global_names                      boolean     FALSE
sys@DBA.WORLD> alter system set global_names=true ;

System altered.

Elapsed: 00:00:00.01
sys@DBA.WORLD>

scott@DBA.WORLD> select * from global_name@foo ;
select * from global_name@foo
                         *
ERROR at line 1:
ORA-02085: database link FOO.WORLD connects to ORA9I.ORACLE.COM


Elapsed: 00:00:00.00
scott@DBA.WORLD>

看看,global_names=true后,刚才好好的foo,马上失效了。

现在我把global_names改为false再看看。
sys@DBA.WORLD> alter system set global_names=false ;

System altered.

Elapsed: 00:00:00.01
sys@DBA.WORLD> show parameter global_names

NAME                               TYPE        VALUE
------------------------------------ ----------- ----------
global_names                      boolean     FALSE
sys@DBA.WORLD>

scott@DBA.WORLD> select * from global_name@foo ;

GLOBAL_NAME
----------------------------------------------------

ORA9I.ORACLE.COM

Elapsed: 00:00:00.00
scott@DBA.WORLD>

你看,foo又好了。

废话一堆,现在开始第3种情况测试,首先把global_names改为true。

sys@DBA.WORLD> alter system set global_names=true ;

System altered.

Elapsed: 00:00:00.01
sys@DBA.WORLD> show parameter global_names

NAME                               TYPE        VALUE
------------------------------------ ----------- ------
global_names                      boolean     TRUE
sys@DBA.WORLD>

开始创建db link。
scott@DBA.WORLD> create database link ora9i.oracle.com using 'as3' ;

Database link created.

Elapsed: 00:00:00.00
scott@DBA.WORLD> select * from global_name@ora9i.oracle.com ;

GLOBAL_NAME
--------------------------------------------------------------------

ORA9I.ORACLE.COM

Elapsed: 00:00:00.05
scott@DBA.WORLD>

4. global_name=true, link type=fixed user
db link是单向的。

在 test 上创建 test->prod 的 db link:

D:\oraclass\sql>sqlplus system/manager ### 以system登录

SQL*Plus: Release 9.2.0.4.0 - Production on Fri Sep 2 10:24:44 2005

Copyright (c) 1982, 2002, Oracle Corporation.   All rights reserved.


Connected to:
Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.4.0 - Production

system@DBA.WORLD> show parameter global_names

NAME                               TYPE        VALUE
------------------------------------ ----------- ------------------------------
global_names                      boolean     TRUE

system@DBA.WORLD> select * from global_name ;

GLOBAL_NAME
-------------------------------------------------------------------------------

DBA.WORLD

Elapsed: 00:00:00.00
### 请注意global_names和global_name的区别,呵呵。

system@DBA.WORLD> create public database link ora9i.oracle.com using 'as3' ;

Database link created.

Elapsed: 00:00:00.01
### 创建db link,as3是test->prod的网络连接串

system@DBA.WORLD> @c mhung/huang 以mhung登录
mhung@DBA.WORLD>
mhung@DBA.WORLD> select * from tab@ora9i.oracle.com ;### 远程数据库

TNAME                          TABTYPE   CLUSTERID
------------------------------ ------- ----------
DEPT                         TABLE
EMP                         TABLE
T                            TABLE
T_A                         SYNONYM

Elapsed: 00:00:10.04
mhung@DBA.WORLD> select * from tab ;### 本地数据库

TNAME                          TABTYPE   CLUSTERID
------------------------------ ------- ----------
EMP30                          TABLE
INT_EMP30                   TABLE
T                            TABLE

Elapsed: 00:00:00.00
mhung@DBA.WORLD> select * from t ;  ### 请注意本地表t的内容

      A B
---------- ----------
      1 燚
      2 燚燚已以
      3 1燚燚已以
      4 数据库

Elapsed: 00:00:00.00
mhung@DBA.WORLD>

在 prod 上创建 prod->test 的 db link:

[oracle@O9i sql]$ sqlplus system/manager  ### 以system登录

SQL*Plus: Release 9.2.0.4.0 - Production on Fri Sep 2 10:28:30 2005

Copyright (c) 1982, 2002, Oracle Corporation.   All rights reserved.


Connected to:
Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.4.0 - Production

system@ORA9I.ORACLE.COM> show parameter global_names  

NAME                               TYPE        VALUE
------------------------------------ ----------- ------------------------------
global_names                      boolean     TRUE
system@ORA9I.ORACLE.COM> select * from global_name ;

GLOBAL_NAME
----------------------------------------------------------------------------------------------------
ORA9I.ORACLE.COM

Elapsed: 00:00:00.03
system@ORA9I.ORACLE.COM>
system@ORA9I.ORACLE.COM>
system@ORA9I.ORACLE.COM> create public database link dba.world using 'dba' ;

Database link created.

Elapsed: 00:00:00.03
### 创建db link,dba是prod->test的网络连接串

system@ORA9I.ORACLE.COM> @c mhung/huang ### 以mhung登录
mhung@ORA9I.ORACLE.COM>
mhung@ORA9I.ORACLE.COM> select * from tab@dba.world ;  ### 远程数据库

TNAME                          TABTYPE   CLUSTERID
------------------------------ ------- ----------
EMP30                          TABLE
INT_EMP30                   TABLE
T                            TABLE

Elapsed: 00:00:00.15
mhung@ORA9I.ORACLE.COM> select * from tab ; ### 本地数据库

TNAME                          TABTYPE   CLUSTERID
------------------------------ ------- ----------
DEPT                         TABLE
EMP                         TABLE
T                            TABLE

Elapsed: 00:00:00.01
mhung@ORA9I.ORACLE.COM> select * from t ;  ### 请注意本地表t的内容

C
--------------------
福州
begin 北伐军 ; end;
沙蝶

Elapsed: 00:00:00.01
mhung@ORA9I.ORACLE.COM>

几个demo操作:

1. prod上mhung用户创建synonym,指向mhung.t@dba.world

mhung@ORA9I.ORACLE.COM> create synonym t_test for t@dba.world ;

Synonym created.

Elapsed: 00:00:00.10
mhung@ORA9I.ORACLE.COM> select * from t_test ;

      A B
---------- ----------
      1 燚
      2 燚燚已以
      3 1燚燚已以
      4 数据库

Elapsed: 00:00:00.04
mhung@ORA9I.ORACLE.COM>

2. test上mhung用户,插入数据到 t_test@ora9i.oracle.com 中。
mhung@DBA.WORLD> select * from tab@ora9i.oracle.com ;

TNAME                          TABTYPE   CLUSTERID
------------------------------ ------- ----------
DEPT                         TABLE
EMP                         TABLE
T                            TABLE
T_TEST                      SYNONYM

Elapsed: 00:00:00.00

mhung@DBA.WORLD> select * from t_test@ora9i.oracle.com ;

      A B
---------- ----------
      1 燚
      2 燚燚已以
      3 1燚燚已以
      4 数据库

Elapsed: 00:00:00.04
mhung@DBA.WORLD> select *from t ;

      A B
---------- ----------
      1 燚
      2 燚燚已以
      3 1燚燚已以
      4 数据库
     
Elapsed: 00:00:00.00
mhung@DBA.WORLD> insert into t_test@ora9i.oracle.com values(5,'插本地') ;

1 row created.

Elapsed: 00:00:00.00
mhung@DBA.WORLD> commit;

Commit complete.

Elapsed: 00:00:00.00
mhung@DBA.WORLD> select * from t_test@ora9i.oracle.com ;

      A B
---------- ----------
      1 燚
      2 燚燚已以
      3 1燚燚已以
      4 数据库
      5 插本地

Elapsed: 00:00:00.00
mhung@DBA.WORLD> select *from t ;

      A B
---------- ----------
      1 燚
      2 燚燚已以
      3 1燚燚已以
      4 数据库
      5 插本地

Elapsed: 00:00:00.00
mhung@DBA.WORLD>
 

 
本文作者:网友 来源: http://hi.baidu.com/leexiaodong/blog
CIO之家 www.ciozj.com 微信公众号:imciow
    >>频道首页  >>网站首页   纠错  >>投诉
版权声明:CIO之家尊重行业规范,每篇文章都注明有明确的作者和来源;CIO之家的原创文章,请转载时务必注明文章作者和来源;
延伸阅读