数据迁移的方法有多种,根据不同的需求制定不通的迁移方案。
1.exp/imp最传统,也是最保险的迁移方式。时间较长。
2.Expdp/impdp数据泵,速度比较快。
3.Rman的数据迁移。其原理只是备份和恢复。很容易理解。
1.exp/imp
查看数据库的表空间及大小。select tablespace_name from dba_tablespaces;
查看数据库字符集 select userenv('language') from dual;
查看数据库用户及其权限。
exp system/oracle file=full.dmp log=full.log buffer=10000000 full=y
imp system/oracle file=full.dmp log=full.log buffer=100000000 full=y ignore=y
这种方法比较简单只是时间会比较长。导出和导入的时候需要注意字符集。
需要新库和老库的表空间名称一致。用户名称一致,最好密码也一致。因为如果表空间 的名称不一致的话,原来的用户导入到新库没有找到对应的表空间,则会吧数据导入到 system表空间里去。指定full=y。可以把所有的对象都导入的。包括权限,索引,试图等等。只是新库需要重新编译一下。
2.expdp/impdp
Expdp是oracle的数据泵技术。其优点是速度快。但是不太稳定,有时候会激发出oracle的内容BUG。比如上次在周市人民医院用数据泵做迁移的时候就激发出了BUG。他们的数据库是10.2.0.1.0的最老版本,虽说支持数据泵。但是容易激发出BUG。最后还是用exp/imp才搞定。
使用数据泵迁移的步骤。
查看数据库的表空间及大小。select tablespace_name from dba_tablespaces;
查看数据库字符集 select userenv('language') from dual;
查看数据库用户及其权限。
在新库建立对应的表空间,用户。
Create tablespace datafile ' /opt/oradata/SID/' size 100M;这里的大小要根据老库的数据文件 的大小定。如果老库的数据文件已经有1G大小了。那不能只创建100M的数据文件。即使是自动扩展的。
在两边的服务器上建立相同的directory。
SQL>Create directory my_dir as '/opt/my_dir'; 需要保证路径存在,不存在是不好报错的。但是后面就出现错误了。
SQL>Grant read,write on directory my_dir to system;给system用户对该目录的读写权限。
Expdp system/oracle
SQL> select * from dba_directories where directory_name='MY_DIR';
OWNER DIRECTORY_NAME DIRECTORY_PATH
-------------------- -------------------- ------------------------------
SYS MY_DIR /opt/my_dir/expdp_impdp
确认创建成功。
expdp system/oracle as sysdb directory=my_dir dumpfile=exp_ora10g_full.dmp full=y
把导出文件拷贝到新库的directory目录下。
impdp system/oracle as sysdb directory=my_dir dumpfile=imp_ora10g_full.dmp full=y
结束注意查看日志文件。
3.rman迁移
Rman迁移的思想就是在老库上面执行备份。然后在新库上面执行恢复。
原理:源数据库(A) rman备份。新数据库rman(B)恢复。
(A)
1.新建RMAN恢复目录数据库(实例=rman).
第一步,在目录数据库中创建恢复目录所用表空间:
create tablespace rman_tar datafile '/oracle/oradata/rman/rman_tar.dbf' size 20M;
第二步,在目录数据库中创建RMAN 用户并授权:
create user rman identified by rman default tablespace rman_tar;
grant recovery_catalog_owner to rman;
grant connect to rman;
grant resource to rman;
第三步,在目录数据库中创建恢复目录
$rman catalog rman/rman
如果未安装恢复目录 (执行)
create catalog tablespace rman_tar;
2.注册目标数据库
[oracle@SZSC ~]$ rman catalog rman/rman@ora10g
RMAN> connect target /
RMAN> register database;
3.对目标数据库进行备份
run{
configure controlfile autobackup on;
configure controlfile autobackup format for device type disk to '/opt/backup/%F';
allocate channel d1 type disk format '/opt/backup/full_%U';
backup database tag 'full_ora10g_data' filesperset 10 plus archivelog filesperset 20 delete all input;
release channel d1;
}
4.将备份的backup目录传输到目标服务器对应的backup目录。并且将密码文件及pfile文件传到对应的目录。
Scp -r /opt/backup/* 192.168.124.3:/opt/backup
Scp -r /opt/oracle/dbs/* 192.168.124.3:/opt/oracle/dbs
(B)
5.到B数据库,进行恢复。
先用传输过来的pfile启动数据库。
Sqlplus / as sysdba
Startup nomount pfile='/opt/oracle/dbs/initora10g.ora
6.连接rman
Rman target /
Set dbid=XXXXX(DBID到控制文件自动备份里找)
恢复控制文件。
Restore controlfile from '/opt/backup/c-4106385437-20120723-01'
Alter database mount
恢复数据文件
Restore databae from tag='full_ora10g_data';
Recover database;
Alter databae open;