需求:oracle生产环境12.2.0.1,测试环境11.2.0.4,操作系统均为centos6.x
由于开发需要在测试环境搭测试库,库名和生产相同,但由于条件限制,只能在11.2.0.4上建库。
考虑到数据量太大问题,需要库表结构一致,对于数据:大部分表需有1000条记录即可,个别表需要全量数据。
处理思路:
1.建测试库,调整表空间等
2.expdp导数据,scp/sftp到测试机
3.测试机impdp导入对象和数据(先导全库对象,然后再导全量表replace方式),编译失效对象
操作步骤:
1.测试环境建库,一条命令搞定
测试库存放路径空间检查足够,例如:/oracle/oradata
注意字符集–源端提前检查,任选一种sql查询即可
select userenv('language') from dual;
select * from props$;
select * from database_properties;
例如
characterset zhs16gbk
nationalcharacterset al16utf16
memory_target根据物理内存实际大小分配:比如32g,分配3g,可设置memorypercentage 10即可,后期可调整
su - oracle
cd $oracle_home/assistants/dbca/templates
dbca -silent -createdatabase -templatename general_purpose.dbc -gdbname oradb -sid oradb -syspassword xxxxxx -systempassword xxxxxx -responsefile no_value -datafiledestination /oracle/oradata -redologfilesize 200 -recoveryareadestination no_value -storagetype fs -characterset zhs16gbk -nationalcharacterset al16utf16 -sampleschema false -memorypercentage 10 -databasetype oltp -emconfiguration none
防止密码过期锁定:
alter profile default limit failed_login_attempts unlimited;
alter profile default limit password_life_time unlimited;
源端检查表空间,根据表空间名称和大小,在测试库提前建好
set line 132
set wrap off
select t.*
from (select d.tablespace_name,
space "sum_space(m)",
blocks sum_blocks,
space - nvl(free_space, 0) "used_space(m)",
round((1 - nvl(free_space, 0) / space) * 100, 2) "used_rate(%)",
free_space "free_space(m)"
from (select tablespace_name,
round(sum(bytes) / (1024 * 1024), 2) space,
sum(blocks) blocks
from dba_data_files
group by tablespace_name) d,
(select tablespace_name,
round(sum(bytes) / (1024 * 1024), 2) free_space
from dba_free_space
group by tablespace_name) f
where d.tablespace_name = f.tablespace_name( )
union all --if have tempfile
select d.tablespace_name,
space "sum_space(m)",
blocks sum_blocks,
used_space "used_space(m)",
round(nvl(used_space, 0) / space * 100, 2) "used_rate(%)",
space - used_space "free_space(m)"
from (select tablespace_name,
round(sum(bytes) / (1024 * 1024), 2) space,
sum(blocks) blocks
from dba_temp_files
group by tablespace_name) d,
(select tablespace,
round(sum(blocks * 8192) / (1024 * 1024), 2) used_space
from v$sort_usage
group by tablespace) f
where d.tablespace_name = f.tablespace( )) t
order by "used_rate(%)" desc;
2.生产库导出
测试和生产都可提前准备dmp目录,例如
create directory dmp as '/oracle/dmp';
grant read,write on directory dmp to public;
生成需要导出的用户脚本,方便expdp调用。
select listagg(username,',') within group (order by username) from dba_users where account_status='open' and username not in('system','sys','oggadm','wksys','olapsys','dbsnmp','mgmt_view','sysman');
例如:aaa,bbb,ccc
编辑t.par文件(无需转义字符):注意并行度parallel的选择,建议1/4的cpu数,确保cpu idle大于70%(sar 1 10可参考),对系统影响较小,排除多个内容,并列写exclude即可,对于高版本到低版本需要注意加version=11.2,否则无法导入。
version=11.2
schemas=aaa,bbb,ccc
directory=dmp
dumpfile=oradb_%u.dmp
logfile=oradb_20220119.log
parallel=4
cluster=n
exclude=statistics
query="where rownum<=1000"
exclude=table:"like' 1%'"
exclude=table:"in ('xxx_log','xxx_log_20090901','tmp0901_1','xxx_log161206','de_whole_route_log161010')"
导出直接调用(用sysdba好处是无需密码,同时可导出public的同义词,dblink等对象
第1次导出不超过1000条的符合要求的库表对象:
expdp \'/ as sysdba\' parfile=t.par
如果不使用parfile,那么需要添加转义字符\,例如:
exclude=table:\"like\' 1%\'\" exclude=table:\"in \(\'xxx_log\',\'xxx_log_20090901\',\'tmp0901_1\',\'xxx_log161206\',\'de_whole_route_log161010\'\)\"
注意字母要大写,同时不要加用户名.形式,只需要大写表名。对于windows不要用powershell窗口操作(命令会报错),而应用cmd的dos窗口。
第2次导出指定全量数据的表:(换一种形式写)
expdp \'/ as sysdba\' tables=aaa.t1,bbb.t2,ccc.t3 dumpfile=oradb_tab.dmp logfile=oradb_tab_20220119.log directory=dmp version=11.2 exclude=statistics
生成的dmp赋权,并压缩,目的是减少网络传输时间
chmod 777 /oracle/dmp/oradb*.dmp
tar zcvf oradb_20220119.tar.gz oradb*.dmp
传输到测试机:
scp oracle@生产库ip:/oracle/dmp/oradb_20220119.tar.gz /oradata/dmp/
3.测试库导入
解压tar zxvf oradb_20220119.tar.gz
对于涉及dblink的库,建议导入之前先排除掉(加快速度的关键),避免导入过程中如果有视图,存储过程,包体调用到的话,可能卡半天(每个编译涉及link连接需要等待超时以后才会继续导入)
导入的几种方式参考:
第1次导入所有对象:
最简单的:
impdp \'/ as sysdba\' directory=dmp full=y dumpfile=oradb_%u.dmp parallel=4 cluster=n logfile=zydb_20220118-ok.log exclude=db_link
其次是参考expdp导出时的参数:
impdp \'/ as sysdba\' directory=dmp schemas=aaa,bbb,ccc dumpfile=oradb_%u.dmp parallel=4 cluster=n logfile=oradb_20220118-1.log exclude=db_link
第2次导入指定全量表:注意 table_exists_action=replace或truncate使用,对于有外键情况可能需要考虑先禁用外键,根据实际情况处理。
impdp \'/ as sysdba\' directory=dmp full=y cluster=n dumpfile=oradb_tab.dmp parallel=1 logfile=oradb_tab_20220119-imp.log table_exists_action=replace
同样也可以指定表写法:
impdp \'/ as sysdba\' tables=aaa.t1,bbb.t2,ccc.t3 dumpfile=oradb_tab.dmp logfile=oradb_tab_20220119-imp.log directory=dmp table_exists_action=replace
最后可考虑是否导入dblink,注意parallel=1 content=metadata_only include=db_link参数
impdp \'/ as sysdba\' directory=dmp full=y cluster=n dumpfile=oradb_tab.dmp cluster=n logfile=oradb_20220118-1.log parallel=1 content=metadata_only include=db_link
排除系统默认用户的命令参考:
expdp \'/ as sysdba\' directory=dpump_dir1 dumpfile=full_meta.dmp logfile=full_meta.log full=y content=metadata_only parallel=1 cluster=n exclude=table,index,sequence,statistics exclude=schema:\"in \(\'sys\',\'system\',\'owbsys_audit\',\'owbsys\',\'apex_public_user\',\'apex_030200\',\'flows_files\',\'spatial_csw_admin_usr\',\'spatial_wfs_admin_usr\',\'mddata\',\'olapsys\',\'orddata\',\'ordplugins\',\'si_informtn_schema\',\'mdsys\',\'ordsys\',\'xdb\',\'anonymous\',\'ctxsys\',\'exfsys\',\'wmsys\',\'appqossys\',\'dbsnmp\',\'oracle_ocm\',\'dip\',\'outln\',\'sysman\'\)\"
编译失效对象
conn /as sysdba
@?/rdbms/admin/utlrp.sql
小结:
上述参数方法在日常迁移中比较常用,不限于此,如果熟悉了expdp/impdp的灵活用法,对于迁移库来说便可事半功倍!
最后修改时间:2023-12-14 11:03:28
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【米乐app官网下载的版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。