一 当有数据文件被误删除时如何恢复 RMAN规划 host target库 catalog库 ocm01 ocmdb ocm02 ocmdb02 模拟ocmdb库system01.dbf 数据文件丢失,利用catalogue库omcdb02进行RMAN恢复。 构造场景 shutdown immediate rm -f /u01/oracle/oradata/ocmdb/disk1/syste
一 当有数据文件被误删除时如何恢复
RMAN规划
host |
target库 |
catalog库 |
ocm01 |
ocmdb |
|
ocm02 |
|
ocmdb02 |
模拟ocmdb库system01.dbf 数据文件丢失,利用catalogue库omcdb02进行RMAN恢复。
构造场景
shutdown immediate
rm -f /u01/oracle/oradata/ocmdb/disk1/system01.dbf
sqlplus / as sysdba
startup
ORA-01157: cannot identify/lock data file 1 - see DBWR trace file
ORA-01110: data file 1: '/u01/oracle/oradata/ocmdb/disk1/system01.dbf'
alert日志中同样记录了报错信息(现在数据库处于异常关闭状态),从告警日志中就可以知道是system文件没有了 --alert日志位置:/u01/oracle/admin/ocmdb/bdump/alert_ocmdb.log
将数据库启动到mount状态,才能restore and recover 恢复
shutdown abort
startup mount
exit
登录RMAN
rman target sys/sys@ocmdb1521 catalog catalog_admin/catalog_admin@ocmdb021521
用原来的RMAN备份来还原和恢复
restore datafile 1;
recover datafile 1;
alter database open;
select file_name,tablespace_name,status from dba_data_files where tablespace_name='SYSTEM';
二 物化视图
物化视图规划
host |
基表&日志 |
物化视图 |
dblink |
ocm01 |
|
mv_t |
dblink_to_ocm02 |
ocm02 |
t_mv_ocm02,MLOG$_T_MV_OCM02 |
|
|
物化视图:执行sql并保留结果,直接放在数据文件中,不放在内存中方便重用【空间换时间】
场合:同步数据,过滤数据并两次利用
1.要求创建跨库物化视图并可手动更新
官方文档:PL/SQL Packages and Types Reference -> 61 DBMS_MVIEW
基表和物化视图不在同一个库上,基表和日志在ocm02上,物化视图在ocm01上,在ocm01上可手动刷新物化视图
ocm02
create user mv_ocm02 identified by mv_ocm02 ;
grant connect,resource to mv_ocm02 ;
conn mv_ocm02 /mv_ocm02
create table t_mv_ocm02(a varchar2(10));
insert into t values ('test01');
commit;
select * from t_mv_ocm02;
创建物化视图日志,基表在哪个库,日志就在哪个库,用于记录基表所有变化,实时刷新物化视图,完全刷新不需要物化视图日志,快速刷新需要物化视图日志
create materialized view log on t with rowid; 基于rowid变化记录到日志中 --创建MLOG$_T_MV_OCM02对象
ocm01
创建dblink
sqlplus / as sysdba@ocmdb
create public database link dblink_to_ocm02 connect to mv_ocm02 identified by mv_ocm02 using 'ocmdb021521';
注:global_names参数为FALSE 才允许DB link名字与我远程连接实例名不一致
create user mv_ocm01 identified by mv_ocm01 ;
grant dba to mv_ocm01 ;
conn mv_ocm01 /mv_ocm01
创建跨库可刷新物化视图
create materialized view mv_t refresh fast with rowid as select * from t_mv_ocm02@dblink_to_ocm02;
sqlplus mv_ocm02 /mv_ocm02 @ocmdb021521
insert into t values ('test02');
commit;
select * from t_mv_ocm02;
sqlplus mv_ocm01 /mv_ocm01 @ocmdb
select * from mv_t;
快速刷新方法:增量刷新 或 完全刷新方法:全表刷新
exec dbms_mview.refresh('mv_t','f'); 手动刷新物化视图
exec dbms_mview.refresh('mv_t','c');
select * from mv_t;
2.要求创建物化视图具有自动快速刷新功能
sqlplus mv_ocm01 /mv_ocm01 @ocmdb
注:grant connect,resource,create materialized view to mv_ocm01 ; 如果是普通用户需要授予创建物化视图权限
drop table t1 purge;
create table t1 (x int,y int,z int);
insert into t1 values (1,2,3);
insert into t1 values (4,5,6);
commit;
select * from t1;
create materialized view log on t1 with sequence,rowid (x,y,z) including new values;
我们只看x和y列信息
drop materialized view mv_t1;
create materialized view mv_t1 build immediate refresh with rowid fast on commit enable query rewrite as select x,y from t1;
参数说明:
build immediate:创建物化视图时,立即刷新基表
fast on commit:支持基于commit动作的自动刷新
enable query rewrite:启动查询重写功能
refresh with rowid:物化视图默认是基于主键PK方式来刷新的,由于基表没有主键,因此我们基于ROWID刷新
select * from mv_t1;
验证物化视图是否随记录增加而更新
insert into t1 values (7,8,9);
select * from t1;
select * from mv_t1; mv_t1随记录增加而木有刷新,必须commit之后才触发物化视图刷新
commit;
select * from mv_t1;
delete from t1 where x=1;
select * from t1;
commit;
select * from mv_t1;
三 外部表
官方文档:Administrator’s Guide -> 15 Managing Tables -> Managing External Tables
Utilities->Part III External Tables –> 14 The ORACLE_DATAPUMP Access Driver
外部表:把数据保存在操作系统层面上,并不保存在表空间上,即可以把数据写入外部表又可以读取外部表到数据库(只读到内存里)。外部表就是操作系统上的一个二进制文件不是纯文本文件。可用strings查看
场合:导出的数据可用于二次开发。数据迁移
要求使用oracle数据泵导出外部表并跨库加载
过程:ocm01卸载数据,传输到ocm02加载数据
外部表实验规划
步骤 |
ocm01 |
ocm02 |
说明 |
1 |
创建dir_dmp |
|
位置:ocm01:/home/oracle |
2 |
导出t2_part1.dat,t2_part2.dat到dir_dmp |
|
|
3 |
|
创建dir_dmp |
位置:ocm02:/home/oracle |
4 |
|
拷贝t2_part1.dat,t2_part2.dat到dir_dmp |
scp把文件从ocm01拷贝到ocm02 |
5 |
|
加载到数据库 |
|
创建directory
sqlplus / as sysdba@ocmdb
create user ext identified by ext;
grant dba to ext;
conn ext/ext
create directory dir_dmp as '/home/oracle';
grant read,write on directory dir_dmp to public; 授予读/写权限给目录对象,把目录对象给所有用户
创建外部表,使用数据泵工具卸载数据,使用2个cup并行卸载,加快速度
create table t2
organization external
(type oracle_datapump
default directory dir_dmp
location ('t2_part1.dat','t2_part2.dat')
)
parallel 2
as
select owner,object_id,object_name from dba_objects where owner='SYSTEM';
strings命令可以读取卸载得到的二进制文件内容
strings t2_part1.dat
在ocm02创建外部表t3读取t2_part1.dat和t2_part2.dat
scp t2_part1.dat t2_part2.dat ocm02:/home/oracle
sqlplus / as sysdba@ocmdb02
create user ext identified by ext;
grant dba to ext;
conn ext/ext
create directory dir_dmp as '/home/oracle';
grant read,write on directory dir_dmp to public;
加载数据到t3
create table t3 (owner varchar2(100),object_id varchar2(100),object_name varchar2(100))
organization external
(type oracle_datapump
default directory dir_dmp
location ('t2_part1.dat','t2_part2.dat'));
select count(*) from t3;
到此我们的外部表方式数据迁移完成。