1、连接本地scott用户查看拥有的表
[oracle@ORADG ~]$ sqlplus scott/tiger
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
BONUS TABLE
DEPT TABLE
EMP TABLE
MLOG$_EMP TABLE
RUPD$_EMP TABLE
SALGRADE TABLE
TEST TABLE
2、连接远程计算机orcl实例的scott用户,查看用户表,测试可以连通
[oracle@ORADG ~]$ sqlplus scott/tiger@192.168.56.30:1521/orcl
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
BONUS TABLE
DEPT TABLE
EMP TABLE
SALGRADE TABLE
USER_DATA TABLE
3、修改host记录,可以解析rac-scan的计算机名
[root@ORADG ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.56.20 ORADG
192.168.56.30 rac-scan
4、修改tnsnames.ora文件,添加如下一行,让本机可以用rac连通远程服务器
[oracle@ORADG ~]$ cd /u01/app/oracle/product/11.2.0/db_1/network/admin/
[oracle@ORADG admin]$ vi tnsnames.ora
RAC =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.56.30)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
5、修改完毕后进行tnsping,检车能否tnsping通
[oracle@ORADG admin]$ tnsping RAC
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.56.30)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = orcl)))
OK (10 msec)
6、查看test用户下拥有的表
[oracle@ORADG admin]$ sqlplus test/test
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
EMPF TABLE
EMPF1 TABLE
EMPU1 TABLE
EMPU2 TABLE
USLOG$_EMPU1 TABLE
USLOG$_EMPU2 TABLE
7、授予test用户有创建dblink权限
SQL> conn / as sysdba
SQL> grant create database link to test;
8、用test用户创建dblink,连接scott用户,语法如下
SQL> conn test/test
SQL> create database link RAC connect to scott identified by tiger using 'RAC';
9、检查以RAC命名的dblink是否有效,检查可以访问远程的scott用户下的emp表
SQL> desc emp@RAC
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
10、通过dblink更改scott用户的emp表,然后在远程的scott用户下的emp表数据发生了变化
SQL> select sal from emp@rac where deptno=10;
SAL
----------
2450
5000
1300
SQL> /
SAL
----------
3450
6000
2300
11、授予test用户有创建同义词的权限
SQL> conn / as sysdba
SQL> grant create synonym to test;
12、为test用户创建同义词
SQL> conn test/test
SQL> create synonym emp for emp@rac;
SQL> create synonym dept for dept@rac;
SQL> create synonym salgrade fro salgrade@rac;
13、可以看到多了几张表,其实为同义词
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
DEPT SYNONYM
EMP SYNONYM
EMPF TABLE
EMPF1 TABLE
EMPU1 TABLE
EMPU2 TABLE
SALGRADE SYNONYM
USLOG$_EMPU1 TABLE
USLOG$_EMPU2 TABLE
14、使用同义词可以进行对应的查询,该查询是通过dblink远程访问rac的scott用户
SQL> select * from emp where deptno=10;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7782 CLARK MANAGER 7839 09-JUN-81 2500 10
7839 KING PRESIDENT 17-NOV-81 5000 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10
15、同样也可以进行数据更改
SQL> update emp set sal=sal-1 where deptno=10;
SQL> commit;
SQL> select * from emp where deptno=10;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7782 CLARK MANAGER 7839 09-JUN-81 2449 10
7839 KING PRESIDENT 17-NOV-81 4999 10
7934 MILLER CLERK 7782 23-JAN-82 1299 10
16、创建word用户并授权
SQL> conn / as sysdba
SQL> create user word identified by oracle default tablespace users;
SQL> grant connect,resource to word;
17、用word用户可以将远程的数据库用户下的表直接拷贝过来
SQL> conn word/oracle
SQL> copy from scott/tiger@rac-scan:1521/orcl to word/oracle@orcl_dg replace emp using select * from emp;
Array fetch/bind size is 15. (arraysize is 15)
Will commit when done. (copycommit is 0)
Maximum long size is 80. (long is 80)
Table EMP created.
14 rows selected from scott@rac-scan:1521/orcl.
14 rows inserted into EMP.
14 rows committed into EMP at word@orcl_dg.
SQL> select sal from emp where deptno=10;
SAL
----------
2449
4999
1299
18、可以修改arraysize的参数,使拷贝速度加快
SQL> set arraysize 999
SQL> copy from scott/tiger@rac to word/oracle@orcl_dg create dept using select * from emp;
Array fetch/bind size is 999. (arraysize is 999)
Will commit when done. (copycommit is 0)
Maximum long size is 80. (long is 80)
Table EMP created.
14 rows selected from scott@rac.
14 rows inserted into EMP.
14 rows committed into EMP at word@orcl_dg.