我们在做某些Schema的迁移的时候,由于用到Public的db link,然而由于不知道db link中目标端账号的密码,因此无法在新环境重新创建DB link。

本次实验的思路是将视图dba_db_links的基表link$迁移到出来并替换到新环境。因为用户的password密文保存在link$基表中。


--------------------- 

源端操作:

首先还在源端创建一个public 的db link 名为test。


Asherdb1 >create public database link test connect to scott identified by tiger using 'ginna';
Database link created.
seven >select * from global_name@test;

GLOBAL_NAME
---------------------
GINNA

Asherdb1 >select * from dba_db_links;

OWNER DB_LINK USERNAME HOST CREATED
------------------------------ -----------------------------
PUBLIC TEST SCOTT



1

Asherdb1 >create public database link test connect to scott identified by tiger using 'ginna';

2

Database link created.

3

seven >select * from global_name@test;

4

5

GLOBAL_NAME

6

---------------------

7

GINNA

8

9

Asherdb1 >select * from dba_db_links;

10

11

OWNER DB_LINK USERNAME HOST CREATED

12

------------------------------ -----------------------------

13

PUBLIC TEST SCOTT

创建中间表trans1,CTAS基表sys.link$。


Asherdb1 >create table trans1 as select * from sys.link$;



1

Asherdb1 >create table trans1 as select * from sys.link$;

目标端操作:

在目标端创建名为test2的Public db link。连接源端库

Asherdb2 >create public database link test2 connect to seven identified by oracle using 'seven';
Database link created.

Asherdb2 >select * from global_name@test2;



1

Asherdb2 >create public database link test2 connect to seven identified by oracle using 'seven';

2

Database link created.

3

4

Asherdb2 >select * from global_name@test2;

通过test2的db link将Asher端的trans2表insert link$中。

Asherdb2 >insert into sys.link$ select * from trans1@test2;



1

Asherdb2 >insert into sys.link$ select * from trans1@test2;

现在再查一下dba_db_links就可以发现名为test的db link已经成功迁移在Asher端。


Asherdb2 >select * from dba_db_links;



1

Asherdb2 >select * from dba_db_links;

测试db link 的有效性:


Asherdb2 >select * from global_name@test;



1

Asherdb2 >select * from global_name@test;

主要就是将trans1这张中间表迁移至Asher库就可以了,这有很多方法,可以直接使用exp/imp或者数据泵都可以。条条大道通罗马,方法有很多种,主要就是将seven端的sys.link$基表迁移至Asher端并替换掉Asher端的sys.link$。


注: public dblink的 user# 是1

​​

SQL> SELECT USER#,NAME FROM USER$ WHERE name in ('SYS','PUBLIC');

USER# NAME
---------- ------------------------------
1 PUBLIC
0 SYS


6


1

SQL> SELECT USER#,NAME FROM USER$ WHERE name in ('SYS','PUBLIC');

2

3

USER# NAME

4

---------- ------------------------------

5

1 PUBLIC

6

0 SYS

--------------


另: 可以通过修改基表(link$)让非public dblink变为public