2023/01/22
: redo logfile 에 있는 내용을 추출해서 SQL문장으로 변환해주는 유틸리티
현재 세션에서만 가능하다.
최소 supplemental 로그 데이터를 사용하는지 여부
select supplemental_log_data_min from v$database;
alter database add supplemental log data;
select supplemental_log_data_min from v$database;
---
SUPPLEME
--------
YES
---
create table hr.test_20240122(id number, name varchar2(30));
insert into hr.test_20240122(id,name) values(1,'oracle');
insert into hr.test_20240122(id,name) values(2,'overwatch');
commit;
update hr.test_20240122
set name ='itwill' where id=1;
delete from hr.test_20240122 where id=2;
commit;
select a.group#,b.thread#,b.sequence#, member, b.bytes/1024/1024 mb, b.archived, b.status,
b.first_change#, b.next_change#, b.next_time
from v$logfile a, v$log b
where a.group# = b.group#
order by 1,2;
begin
dbms_logmnr.add_logfile(logfilename=>'/u01/app/oracle/oradata/ora11g/redo01.log',
options=> dbms_logmnr.new); -- 처음꺼만 new
dbms_logmnr.add_logfile(logfilename=>'/u01/app/oracle/oradata/ora11g/redo02.log',
options=> dbms_logmnr.addfile); -- 이후부터는 addfile
end;
/
select db_name, filename from v$logmnr_logs;
BEGIN
dbms_logmnr.start_logmnr(options=>dbms_logmnr.dict_from_online_catalog);
END;
/
select to_char(timestamp,'yyyy/mm/dd hh24:mi:ss'),operation,
sql_redo, sql_undo
from v$logmnr_contents
where seg_name ='TEST_20240122';
---
2024/01/13 23:15:24 DDL create table hr.test_20240122(id number, name varchar2(30));
2024/01/13 23:15:25 INSERT insert into "HR"."TEST_20240122"("ID","NAME") values ('1','oracle');
2024/01/13 23:15:26 INSERT insert into "HR"."TEST_20240122"("ID","NAME") values ('2','overwatch');
2024/01/13 23:15:29 UPDATE update "HR"."TEST_20240122" set "NAME" = 'itwill' where "NAME" = 'oracle' and ROWID = 'AAAVrkAAEAAAAKcAAA';
2024/01/13 23:15:29 DELETE delete from "HR"."TEST_20240122" where "ID" = '2' and "NAME" = 'overwatch' and ROWID = 'AAAVrkAAEAAAAKcAAB';
exec dbms_logmnr.end_logmnr
select to_char(timestamp,'yyyy/mm/dd hh24:mi:ss'),operation,
sql_redo, sql_undo
from v$logmnr_contents
where seg_name ='TEST_20240122';
: 운영 데이터파일, 리두로그 파일을 이용해서 데이터베이스를 생성
● 1. 복제 db에 datafile, redologfile 복제
: sysaux 와 undo 테이블스페이스는 필수이다.
[oracle@oracle ~]$ mkdir clone
[oracle@oracle ~]$ cd clone/
[oracle@oracle clone]$ cp -av /home/oracle/backup/arch/cold_20240116/*.dbf .
‘/home/oracle/backup/arch/cold_20240116/example01.dbf’ -> ‘./example01.dbf’
‘/home/oracle/backup/arch/cold_20240116/sysaux01.dbf’ -> ‘./sysaux01.dbf’
‘/home/oracle/backup/arch/cold_20240116/system01.dbf’ -> ‘./system01.dbf’
‘/home/oracle/backup/arch/cold_20240116/temp01.dbf’ -> ‘./temp01.dbf’
‘/home/oracle/backup/arch/cold_20240116/undotbs01.dbf’ -> ‘./undotbs01.dbf’
‘/home/oracle/backup/arch/cold_20240116/users01.dbf’ -> ‘./users01.dbf’
[oracle@oracle clone]$ cp -av /home/oracle/backup/arch/cold_20240116/*.log .
‘/home/oracle/backup/arch/cold_20240116/redo01.log’ -> ‘./redo01.log’
‘/home/oracle/backup/arch/cold_20240116/redo02.log’ -> ‘./redo02.log’
‘/home/oracle/backup/arch/cold_20240116/redo03.log’ -> ‘./redo03.log’
[oracle@oracle clone]$ ls
example01.dbf redo02.log sysaux01.dbf temp01.dbf users01.dbf
redo01.log redo03.log system01.dbf undotbs01.dbf
● 2. pfile 생성
select * from v$version;
[oracle@oracle ~]$ vi $ORACLE_HOME/dbs/initclone.ora
---
compatible='11.2.0.4.0'
*.control_files='/home/oracle/clone/control01.ctl'
*.db_name='clone'
*.log_archive_dest_1='location=/home/oracle/clone mandatory'
*.log_archive_format='arch_%t_%s_%r.arc'
*.undo_tablespace='undotbs'
---
● 3. clone 접속
[oracle@oracle dbs]$ echo $ORACLE_HOME
/u01/app/oracle/product/11.2.0.4/db_1
. oraenv
Oracle 사용자가 여러 데이터베이스 간에 전환할때 사용
[oracle@oracle ~]$ . oraenv
ORACLE_SID = [ora11g] ? clone
ORACLE_HOME = [/home/oracle] ? /u01/app/oracle/product/11.2.0.4/db_1
The Oracle base remains unchanged with value /u01/app/oracle
[oracle@oracle clone]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Sun Jan 14 03:10:43 2024
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to an idle instance
SQL> startup nomount
ORACLE instance started.
Total System Global Area 167407616 bytes
Fixed Size 1363300 bytes
Variable Size 109052572 bytes
Database Buffers 50331648 bytes
Redo Buffers 6660096 bytes
● 4. control file 생성
SQL> alter database backup controlfile to trace as '/home/oracle/new_control.sql';
Database altered.
SQL> CREATE CONTROLFILE SET DATABASE "CLONE" RESETLOGS ARCHIVELOG
MAXLOGFILES 16
MAXLOGMEMBERS 3
MAXDATAFILES 100
MAXINSTANCES 8
MAXLOGHISTORY 292
LOGFILE
GROUP 1 '/home/oracle/clone/redo01.log' SIZE 50M BLOCKSIZE 512,
GROUP 2 '/home/oracle/clone/redo02.log' SIZE 50M BLOCKSIZE 512,
GROUP 3 '/home/oracle/clone/redo03.log' SIZE 50M BLOCKSIZE 512
DATAFILE
'/home/oracle/clone/system01.dbf',
'/home/oracle/clone/sysaux01.dbf',
'/home/oracle/clone/users01.dbf',
'/home/oracle/clone/example01.dbf',
'/home/oracle/clone/undotbs01.dbf'
CHARACTER SET AL32UTF8
;
Control file created.
- 변경한것
- REUSE -> SET
- NORESETLOGS -> RESETLOGS
- DB 이름 변경
- /home/oracle/clone/ 경로변경
- 공백줄 없는지 확인
SQL> select status from v$instance;
STATUS
------------
MOUNTED
● 5. cancel base recovery
SQL> recover database until cancel using backup controlfile
ORA-00279: change 1588754 generated at 01/12/2024 17:26:30 needed for thread 1
ORA-00289: suggestion : /home/oracle/clone/arch_1_1_1158057974.arc
ORA-00280: change 1588754 for thread 1 is in sequence #1
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
cancel
Media recovery cancelled.
SQL> alter database open resetlogs;
Database altered.
SQL> select name from v$database;
NAME
---------
CLONE
SQL> select count(*) from hr.employees;
COUNT(*)
----------
107
SQL> select * from v$tempfile;
no rows selected
SQL> ! ls /home/oracle/clone
arch_1_1_1158057974.arc redo01.log sysaux01.dbf undotbs01.dbf
control01.ctl redo02.log system01.dbf users01.dbf
example01.dbf redo03.log temp01.dbf
SQL> alter tablespace temp add tempfile '/home/oracle/clone/temp01.dbf' reuse;
[oracle@oracle ~]$ rm -r clone
[oracle@oracle ~]$ cd $ORACLE_HOME/dbs
[oracle@oracle dbs]$ ls
hc_clone.dat initclone.ora initora11g.ora lkORA11G spfileora11g.ora
hc_ora11g.dat init.ora lkCLONE orapwora11g
[oracle@oracle dbs]$ rm initclone.ora
SQL> select name from v$database;
NAME
---------
CLONE
SQL> shu abort
ORACLE instance shut down.
. oraenv
이용해서 sid와 다시지정[oracle@oracle dbs]$ sqlplus / as sysdba
bash: sqlplus: command not found...
[oracle@oracle dbs]$ . oraenv
ORACLE_SID = [clone] ? ora11g
The Oracle base has been set to /u01/app/oracle
[oracle@oracle dbs]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Sun Jan 14 00:48:10 2024
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> select name from v$database;
NAME
---------
ORA11G
● 1. 작업
create table hr.insa_emp tablespace users as select * from hr.employees;
select count(*) from hr.insa_emp;
- tip
: ctas 로 테이블을 만들면서 not null 제약조건만 걸린다.
select constraint_name, constraint_type, search_condition,
status,index_name
from dba_constraints
where table_name = 'INSA_EMP';
---
SYS_C0011533 C "JOB_ID" IS NOT NULL ENABLED
SYS_C0011532 C "HIRE_DATE" IS NOT NULL ENABLED
SYS_C0011531 C "EMAIL" IS NOT NULL ENABLED
SYS_C0011530 C "LAST_NAME" IS NOT NULL ENABLED
alter table hr.insa_emp add constraint insa_emp_id_pk primary key(employee_id);
alter system switch logfile;
select a.group#,b.thread#,b.sequence#, member, b.bytes/1024/1024 mb, b.archived, b.status,
b.first_change#, b.next_change#, b.next_time
from v$logfile a, v$log b
where a.group# = b.group#
order by 1,2;
● 2. 실수 : 테이블 삭제
drop table hr.insa_emp purge;
● 3. 로그마이너로 삭제한 시간 확인
select a.group#,b.thread#,b.sequence#, member, b.bytes/1024/1024 mb, b.archived, b.status,
b.first_change#, b.next_change#, b.next_time
from v$logfile a, v$log b
where a.group# = b.group#
order by 1,2;
- 로그마이너 (redologfile, 아카이브파일)
: add_logfile로 추가한 것과 add_logfile로 추가한 아카이브 파일이 같은것이면 에러가뜨므로 다른것을 넣어준다.
SQL> ! ls /home/oracle/arch1
SQL> select a.group#,b.thread#,b.sequence#, member, b.bytes/1024/1024 mb, b.archived, b.status,
b.first_change#, b.next_change#, b.next_time
from v$logfile a, v$log b
where a.group# = b.group#
order by 1,2;
begin
dbms_logmnr.add_logfile(logfilename=>'/u01/app/oracle/oradata/ora11g/redo02.log',
options=> dbms_logmnr.new); -- current한 리두로그그룹
dbms_logmnr.add_logfile(logfilename=>'/u01/app/oracle/oradata/ora11g/redo01.log',
options=> dbms_logmnr.addfile);
dbms_logmnr.add_logfile(logfilename=>'/u01/app/oracle/oradata/ora11g/redo03.log',
options=> dbms_logmnr.addfile);
dbms_logmnr.add_logfile(logfilename=>'/home/oracle/arch1/arch_1_3_1158057974.arc',
options=> dbms_logmnr.addfile); -- 아카이브 파일경로
dbms_logmnr.add_logfile(logfilename=>'/home/oracle/arch1/arch_1_4_1158057974.arc',
options=> dbms_logmnr.addfile); -- 아카이브 파일경로
end;
/
select db_name, filename from v$logmnr_logs;
---
ORA11G /home/oracle/arch1/arch_1_1_1158057974.arc
ORA11G /home/oracle/arch1/arch_1_2_1158057974.arc
ORA11G /u01/app/oracle/oradata/ora11g/redo03.log
ORA11G /u01/app/oracle/oradata/ora11g/redo01.log
ORA11G /u01/app/oracle/oradata/ora11g/redo02.log
BEGIN
dbms_logmnr.start_logmnr(options=>dbms_logmnr.dict_from_online_catalog);
END;
/
select to_char(timestamp,'yyyy/mm/dd hh24:mi:ss'),operation,
sql_redo, sql_undo
from v$logmnr_contents
where seg_name ='INSA_EMP';
---
2024/01/14 01:06:24 DDL create table hr.insa_emp tablespace users as select * from hr.employees;
2024/01/14 01:21:26 DDL alter table hr.insa_emp add constraint insa_emp_id_pk primary key(employee_id);
2024/01/14 02:58:20 DDL drop table hr.insa_emp purge;
exec dbms_logmnr.end_logmnr
● 4. 복제 데이터베이스 생성시 필요한 데이터파일, 리두로그파일, 아카이브 파일 복사
: sysaux, system, undo 필수
[oracle@oracle cold_20240116]$ pwd
/home/oracle/backup/arch/cold_20240116
[oracle@oracle cold_20240116]$ cp -av system01.dbf /home/oracle/clone
‘system01.dbf’ -> ‘/home/oracle/clone/system01.dbf’
[oracle@oracle cold_20240116]$ cp -av sysaux01.dbf /home/oracle/clone
‘sysaux01.dbf’ -> ‘/home/oracle/clone/sysaux01.dbf’
[oracle@oracle cold_20240116]$ cp -av undotbs01.dbf /home/oracle/clone
‘undotbs01.dbf’ -> ‘/home/oracle/clone/undotbs01.dbf’
[oracle@oracle cold_20240116]$ cp -av users01.dbf /home/oracle/clone
‘users01.dbf’ -> ‘/home/oracle/clone/users01.dbf’
[oracle@oracle cold_20240116]$ cp -av temp01.dbf /home/oracle/clone
‘temp01.dbf’ -> ‘/home/oracle/clone/temp01.dbf’
[oracle@oracle cold_20240116]$ cp -av *.log /home/oracle/clone
‘redo01.log’ -> ‘/home/oracle/clone/redo01.log’
‘redo02.log’ -> ‘/home/oracle/clone/redo02.log’
‘redo03.log’ -> ‘/home/oracle/clone/redo03.log’
[oracle@oracle cold_20240116]$ cd /home/oracle/arch1
[oracle@oracle arch1]$ cp -av *.* /home/oracle/clone/
‘arch_1_1_1158057974.arc’ -> ‘/home/oracle/clone/arch_1_1_1158057974.arc’
‘arch_1_2_1158057974.arc’ -> ‘/home/oracle/clone/arch_1_2_1158057974.arc’
‘arch_1_3_1158057974.arc’ -> ‘/home/oracle/clone/arch_1_3_1158057974.arc’
‘arch_1_4_1158057974.arc’ -> ‘/home/oracle/clone/arch_1_4_1158057974.arc’
[oracle@oracle arch1]$ cd /home/oracle/clone
[oracle@oracle clone]$ ls
arch_1_1_1158057974.arc arch_1_4_1158057974.arc redo03.log temp01.dbf
arch_1_2_1158057974.arc redo01.log sysaux01.dbf undotbs01.dbf
arch_1_3_1158057974.arc redo02.log system01.dbf users01.dbf
● 5. pfile 생성
[oracle@oracle clone]$ vi $ORACLE_HOME/dbs/initclone.ora
---
compatible='11.2.0.4.0'
*.control_files='/home/oracle/clone/control01.ctl'
*.db_name='clone'
*.log_archive_dest_1='location=/home/oracle/clone mandatory'
*.log_archive_format='arch_%t_%s_%r.arc'
*.undo_tablespace='undotbs'
---
● 6. clone db 접속
[oracle@oracle clone]$ . oraenv
ORACLE_SID = [ora11g] ? clone
ORACLE_HOME = [/home/oracle] ? /u01/app/oracle/product/11.2.0.4/db_1
The Oracle base remains unchanged with value /u01/app/oracle
[oracle@oracle clone]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Sun Jan 14 03:10:43 2024
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to an idle instance.
SQL> startup nomount
ORACLE instance started.
Total System Global Area 167407616 bytes
Fixed Size 1363300 bytes
Variable Size 109052572 bytes
Database Buffers 50331648 bytes
Redo Buffers 6660096 bytes
● 7. control file 생성
SQL>
CREATE CONTROLFILE SET DATABASE "CLONE" RESETLOGS ARCHIVELOG
MAXLOGFILES 16
MAXLOGMEMBERS 3
MAXDATAFILES 100
MAXINSTANCES 8
MAXLOGHISTORY 292
LOGFILE
GROUP 1 '/home/oracle/clone/redo01.log' SIZE 50M BLOCKSIZE 512,
GROUP 2 '/home/oracle/clone/redo02.log' SIZE 50M BLOCKSIZE 512,
GROUP 3 '/home/oracle/clone/redo03.log' SIZE 50M BLOCKSIZE 512
DATAFILE
'/home/oracle/clone/system01.dbf',
'/home/oracle/clone/sysaux01.dbf',
'/home/oracle/clone/users01.dbf',
'/home/oracle/clone/undotbs01.dbf'
CHARACTER SET AL32UTF8
;
Control file created.
: temp는 cp안했으므로 삭제했다.
SQL> select status from v$instance;
STATUS
------------
MOUNTED
SQL> select name, status from v$datafile;
NAME
--------------------------------------------------------------------------------
STATUS
-------
/home/oracle/clone/system01.dbf
SYSTEM
/home/oracle/clone/sysaux01.dbf
RECOVER
/home/oracle/clone/users01.dbf
RECOVER
NAME
--------------------------------------------------------------------------------
STATUS
-------
/home/oracle/clone/undotbs01.dbf
RECOVER
SQL> alter session set nls_date_format ='yyyy/mm/dd hh24:mi:ss';
Session altered.
● 8. drop한 시간 전으로 time base recovery
SQL> recover database until time '2024/01/14 02:57:2' using backup controlfile
ORA-00279: change 1588754 generated at 01/12/2024 17:26:30 needed for thread 1
ORA-00289: suggestion : /home/oracle/clone/arch_1_1_1158057974.arc
ORA-00280: change 1588754 for thread 1 is in sequence #1
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
auto
ORA-00279: change 1593539 generated at 01/13/2024 23:15:05 needed for thread 1
ORA-00289: suggestion : /home/oracle/clone/arch_1_2_1158057974.arc
ORA-00280: change 1593539 for thread 1 is in sequence #2
ORA-00278: log file '/home/oracle/clone/arch_1_1_1158057974.arc' no longer
needed for this recovery
ORA-00279: change 1593542 generated at 01/13/2024 23:15:06 needed for thread 1
ORA-00289: suggestion : /home/oracle/clone/arch_1_3_1158057974.arc
ORA-00280: change 1593542 for thread 1 is in sequence #3
ORA-00278: log file '/home/oracle/clone/arch_1_2_1158057974.arc' no longer
needed for this recovery
ORA-00279: change 1601683 generated at 01/14/2024 01:22:06 needed for thread 1
ORA-00289: suggestion : /home/oracle/clone/arch_1_4_1158057974.arc
ORA-00280: change 1601683 for thread 1 is in sequence #4
ORA-00278: log file '/home/oracle/clone/arch_1_3_1158057974.arc' no longer
needed for this recovery
ORA-00279: change 1601737 generated at 01/14/2024 01:23:49 needed for thread 1
ORA-00289: suggestion : /home/oracle/clone/arch_1_5_1158057974.arc
ORA-00280: change 1601737 for thread 1 is in sequence #5
ORA-00278: log file '/home/oracle/clone/arch_1_4_1158057974.arc' no longer
needed for this recovery
ORA-00308: cannot open archived log
'/home/oracle/clone/arch_1_5_1158057974.arc'
ORA-27037: unable to obtain file status
Linux Error: 2: No such file or directory
Additional information: 3
SQL> recover database until time '2024/01/14 02:57:2' using backup controlfile
ORA-00279: change 1601737 generated at 01/14/2024 01:23:49 needed for thread 1
ORA-00289: suggestion : /home/oracle/clone/arch_1_5_1158057974.arc
ORA-00280: change 1601737 for thread 1 is in sequence #5
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
cancel
Media recovery cancelled.
SQL> alter database open resetlogs;
Database altered.
● 9. 확인
SQL> select count(*) from hr.insa_emp;
COUNT(*)
----------
107
● 10. 클론db에서 export, 원래 db에서 import
- 요약
클론 db에서 살린 data를 export해서
원래 db에 import하면 된다.
exp hr/hr file=insa_emp.dmp tables=hr.insa_emp statistics=none
[oracle@oracle ~]$ exp hr/hr file=insa_emp.dmp tables=hr.insa_emp statistics=none
-- 통계정보는 none
Export: Release 11.2.0.4.0 - Production on Sun Jan 14 03:33:53 2024
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)
About to export specified tables via Conventional Path ...
. . exporting table INSA_EMP 107 rows exported
Export terminated successfully without warnings.
[oracle@oracle ~]$ ls insa_emp.dmp
insa_emp.dmp
[oracle@oracle ~]$ . oraenv
ORACLE_SID = [ora11g] ? ora11g
The Oracle base remains unchanged with value /u01/app/oracle
[oracle@oracle ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Sun Jan 14 03:29:46 2024
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> select name from v$database;
NAME
---------
ORA11G
imp hr/hr file=insa_emp.dmp tables=insa_emp
[oracle@oracle ~]$ imp hr/hr file=insa_emp.dmp tables=insa_emp
Import: Release 11.2.0.4.0 - Production on Sun Jan 14 03:35:43 2024
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export file created by EXPORT:V11.02.00 via conventional path
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses AL32UTF8 character set (possible charset conversion)
. importing HR's objects into HR
. importing HR's objects into HR
. . importing table "INSA_EMP" 107 rows imported
Import terminated successfully without warnings.
[oracle@oracle ~]$ exit
exit
● 11. 원래 db (ora11g) 확인
SQL> select instance_name, status from v$instance;
INSTANCE_NAME STATUS
---------------- ------------
ora11g OPEN
SQL> select count(*) from hr.insa_emp;
COUNT(*)
----------
107
export는 오라클 db가 서로 다른 하드웨어 및 소프트웨어 구성을 사용하는 플랫폼에서 데이터베이스 간 데이터 객체를 이동할 수 있는 방법을 제공한다.
import는 export 덤프 파일에서 db에 로드하는 기능을 제공한다.
export & import 할떄
system 계정에서하는것은 필수이다.
● 1. 작업
create table hr.emp as select * from hr.employees;
select count(*) from hr.emp;
select *
from dba_constraints
[oracle@oracle ~]$ exp userid=system/oracle tables=hr.emp file=hr_emp.dmp statistics=none
Export: Release 11.2.0.4.0 - Production on Sun Jan 14 05:08:28 2024
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)
About to export specified tables via Conventional Path ...
Current user changed to HR
. . exporting table EMP 107 rows exported
Export terminated successfully without warnings.
[oracle@oracle ~]$ ls -lh hr_emp.dmp
-rw-r--r--. 1 oracle oinstall 24K Jan 14 05:08 hr_emp.dmp
drop table hr.emp cascade constraint purge;
[oracle@oracle ~]$ imp userid=system/oracle file=hr_emp.dmp fromuser=hr
Import: Release 11.2.0.4.0 - Production on Sun Jan 14 05:13:44 2024
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export file created by EXPORT:V11.02.00 via conventional path
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses AL32UTF8 character set (possible charset conversion)
. importing SYSTEM's objects into SYSTEM
. importing HR's objects into HR
. . importing table "EMP" 107 rows imported
Import terminated successfully without warnings.
[oracle@oracle ~]$ exit
exit
SQL> select count(*) from hr.emp;
COUNT(*)
----------
107
SQL> truncate table hr.emp;
Table truncated.
SQL> select count(*) from hr.emp;
COUNT(*)
----------
0
SQL> imp system/oracle file=hr_emp.dmp fromuser=hr tables=emp data_only=y
SQL> select count(*) from hr.emp;
COUNT(*)
----------
127
create user michael identified by oracle default tablespace users quota 10m on users;
grant create session to michael;
imp userid=system/oracle file=hr_emp.dmp fromuser=hr touser=michael
SQL> select count(*) from michael.emp;
COUNT(*)
----------
107
● 1. user 레벨 export
[oracle@oracle ~]$ exp system/oracle file=michael_owner.dmp owner=michael
[oracle@oracle ~]$ exp system/oracle file=hr_owner.dmp owner=hr
● 2. 유저삭제
SQL> drop user michael cascade;
User dropped.
● 3. import하기 위해 유저만들기
SQL> create user michael identified by oracle default tablespace users quota 10m on users;
User created.
SQL> grant create session to michael;
Grant succeeded.
● 4. user 레벨에서의 import
[oracle@oracle ~]$ imp system/oracle fromuser=michael file=michael_owner.dmp
● 5. 확인
SQL> conn michael/oracle
Connected.
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
EMP TABLE
2023/01/23
create tablespace insa_tbs datafile
'/u01/app/oracle/oradata/ora11g/insa_tbs01.dbf' size 10m;
-- ctas 로 테이블스페이스 지정할때는 이렇게해야한다.
CREATE TABLE hr.emp
TABLESPACE insa_tbs
AS
SELECT * FROM hr.employees;
DROP TABLESPACE insa_tbs INCLUDING CONTENTS AND DATAFILES;
: 고속 데이터/메타 데이터 이동(export, import)을 위한 유틸리티
데이터: 데이터 내용
메타 데이터: 테이블구조
dbms_datapump
expdp, impdp
- 논리적인 디렉토리생성
SQL> create directory pump_dir as '/home/oracle/data_pump'; Directory created.
- 디렉토리 확인
SQL> select * from dba_directories where directory_name ='PUMP_DIR';
- 논리적 디렉토를 읽고 쓰는 권한 부여
SQL> grant read, write on directory pump_dir to hr; Grant succeeded.
- 논리적인 디렉토리 삭제
SQL> drop directory pump_dir; Directory dropped.
● 1. hr유저의 특정테이블 export
: hr유저의 3개의 테이블을 덤프파일로 export
[oracle@oracle ~]$ expdp system/oracle directory=pump_dir dumpfile=hr_emp_dept_job.dmp tables=hr.employees, hr.departments, hr.job_history
------------------------------
Export: Release 11.2.0.4.0 - Production on Sun Jan 14 08:12:43 2024
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Starting "SYSTEM"."SYS_EXPORT_TABLE_01": system/******** directory=pump_dir dumpfile=hr_emp_dept_job.dmp tables=hr.employees, hr.departments, hr.job_history
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 192 KB
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type TABLE_EXPORT/TABLE/COMMENT
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/TRIGGER
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
. . exported "HR"."DEPARTMENTS" 7.007 KB 27 rows
. . exported "HR"."EMPLOYEES" 16.80 KB 107 rows
. . exported "HR"."JOB_HISTORY" 7.054 KB 10 rows
Master table "SYSTEM"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SYSTEM.SYS_EXPORT_TABLE_01 is:
/home/oracle/data_pump/hr_emp_dept_job.dmp
Job "SYSTEM"."SYS_EXPORT_TABLE_01" successfully completed at Sun Jan 14 08:12:47 2024 elapsed 0 00:00:04
[oracle@oracle ~]$ ls /home/oracle/data_pump/
expdat.dmp export.log hr_emp_dept_job.dmp
● 2. 테이블 지우기
SQL> select count(*) from hr.job_history;
COUNT(*)
----------
10
SQL> drop table hr.job_history purge;
Table dropped.
SQL> select count(*) from hr.job_history;
select count(*) from hr.job_history
*
ERROR at line 1:
ORA-00942: table or view does not exist
● 3. import로 hr.job_history 테이블만 가져오기
expdp=유저/비밀번호
directory=논리적이름
dumpfile=덤프받을파일이름
schemas=유저이름
SQL> !
[oracle@oracle ~]$ impdp system/oracle directory=pump_dir dumpfile=hr_emp_dept_job.dmp tables=hr.job_history
-------------------
Import: Release 11.2.0.4.0 - Production on Sun Jan 14 08:16:04 2024
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_TABLE_01": system/******** directory=pump_dir dumpfile=hr_emp_dept_job.dmp tables=hr.job_history
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "HR"."JOB_HISTORY" 7.054 KB 10 rows
Processing object type TABLE_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type TABLE_EXPORT/TABLE/COMMENT
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Job "SYSTEM"."SYS_IMPORT_TABLE_01" successfully completed at Sun Jan 14 08:16:06 2024 elapsed 0 00:00:01
● 4. 확인
[oracle@oracle ~]$ exit
exit
SQL> select count(*) from hr.job_history;
COUNT(*)
----------
10
● 1. truncate 테이블
SQL> select count(*) from hr.job_history;
COUNT(*)
----------
10
SQL> grant read, write on directory pump_dir to hr;
Grant succeeded.
SQL> truncate table hr.job_history;
Table truncated.
SQL> select count(*) from hr.job_history;
COUNT(*)
----------
0
● 2. truncate 했으므로 테이블구조는 있다.
: 따라서 데이터만 가져오면 된다
-> content=data_only
SQL>
SQL> !
[oracle@oracle ~]$ impdp system/oracle directory=pump_dir dumpfile=hr_emp_dept_job.dmp tables=hr.job_history content=data_only
-----------------------------------
Import: Release 11.2.0.4.0 - Production on Sun Jan 14 08:18:21 2024
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_TABLE_01": system/******** directory=pump_dir dumpfile=hr_emp_dept_job.dmp tables=hr.job_history content=data_only
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "HR"."JOB_HISTORY" 7.054 KB 10 rows
Job "SYSTEM"."SYS_IMPORT_TABLE_01" successfully completed at Sun Jan 14 08:18:23 2024 elapsed 0 00:00:01
● 3. 확인
[oracle@oracle ~]$ exit
exit
SQL> select count(*) from hr.job_history;
COUNT(*)
----------
10
● 1. 테이블 삭제
SQL> drop table hr.job_history purge;
Table dropped.
● 2. 테이블 구조만 가져오겠다
-> content=metadata_only
SQL> !
[oracle@oracle ~]$ impdp system/oracle directory=pump_dir dumpfile=hr_emp_dept_job.dmp tables=hr.job_history content=metadata_only
--------------------
Import: Release 11.2.0.4.0 - Production on Sun Jan 14 08:39:07 2024
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_TABLE_01": system/******** directory=pump_dir dumpfile=hr_emp_dept_job.dmp tables=hr.job_history content=metadata_only
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type TABLE_EXPORT/TABLE/COMMENT
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Job "SYSTEM"."SYS_IMPORT_TABLE_01" successfully completed at Sun Jan 14 08:39:09 2024 elapsed 0 00:00:01
● 3. 확인
[oracle@oracle ~]$ exit
exit
SQL> select * from hr.job_history;
no rows selected
SQL> desc hr.job_history;
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPLOYEE_ID NOT NULL NUMBER(6)
START_DATE NOT NULL DATE
END_DATE NOT NULL DATE
JOB_ID NOT NULL VARCHAR2(10)
DEPARTMENT_ID NUMBER(4)
SQL> purge recyclebin;
Recyclebin purged.
SQL> select * from tab;
- hr의 모든 스키마 export pump
( exclude 쓰면 어떤것을 제외하고 export )
: 리눅스에서는 문자앞에 \ 을 써야한다.[oracle@oracle ~]$ expdp system/oracle directory=pump_dir dumpfile=hr_schema.dmp schemas=hr exclude=table:\"=\'EXCEPTIONS\'\"
- hr의 모든 스키마 export pump
( exclude 쓰면 어떤것을 제외하고 export
in 을 써서 여러개 )[oracle@oracle ~]$ expdp system/oracle directory=pump_dir dumpfile=hr_e_j_schema.dmp schemas=hr exclude=table:\"in \(\'EXCEPTIONS\', \'JOBS\'\)\"
- hr의 테이블만 export pump
[oracle@oracle ~]$ expdp system/oracle directory=pump_dir dumpfile=hr_table.dmp schemas=hr include=table
● 1. 유저 생성
create user henry identified by oracle default tablespace users quota 10m on users;
grant create session to henry;
SQL> select table_name, tablespace_name from dba_tables where owner ='HR';
TABLE_NAME TABLESPACE_NAME
------------------------------ ------------------------------
JOB_HISTORY EXAMPLE
EMPLOYEES EXAMPLE
JOBS EXAMPLE
DEPARTMENTS EXAMPLE
LOCATIONS EXAMPLE
REGIONS EXAMPLE
DEPT USERS
TEST1 USERS
TEST2 USERS
TEST_20240122 USERS
AUDIT_EMP USERS
TABLE_NAME TABLESPACE_NAME
------------------------------ ------------------------------
FGA_LOG USERS
TEST USERS
EMP_BEFORE_2005 USERS
EXCEPTIONS USERS
EMP_SAL USERS
EMP2005 USERS
EMP_NEW USERS
COUNTRIES
● 2. import
hr의 스키마로 export받은것을
hr을 henry로 사용자 변경
EXAMPLE 테이블스페이스를 USERS로 변경
impdp system/oracle directory=pump_dir dumpfile=hr_table.dmp remap_schema='hr':'henry' remap_tablespace='EXAMPLE':'USERS'
● 3. 확인
SQL> select table_name, tablespace_name from dba_tables where owner ='HENRY';
TABLE_NAME TABLESPACE_NAME
------------------------------ ------------------------------
EMPLOYEES USERS
JOBS USERS
DEPARTMENTS USERS
LOCATIONS USERS
REGIONS USERS
JOB_HISTORY USERS
DEPT USERS
AUDIT_EMP USERS
FGA_LOG USERS
EXCEPTIONS USERS
EMP_SAL USERS
TABLE_NAME TABLESPACE_NAME
------------------------------ ------------------------------
TEST USERS
EMP_BEFORE_2005 USERS
EMP2005 USERS
EMP_NEW USERS
TEST1 USERS
TEST2 USERS
TEST_20240122 USERS
COUNTRIES
19 rows selected.
● 1. expdp : 특정 row만 (where절)
[oracle@oracle ~]$ expdp system/oracle directory=pump_dir dumpfile=hr_50_st_man.dmp tables=hr.employees query=hr.employees:\"where department_id \= 50 and job_id\=\'ST_MAN\'\"
● 2. 테이블 생성
create table hr.emp_50_stman as select * from hr.employees where 1=2;
● 3. impdp
[oracle@oracle ~]$ impdp system/oracle directory=pump_dir dumpfile=hr_50_st_man.dmp remap_table=employees:emp_50_stman content=data_only
● 1. sqlplus XE DB로 접속
C:\Users\itwill>sqlplus system/oracle@XE
SQL*Plus: Release 11.2.0.2.0 Production on 화 1월 23 14:04:23 2024
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
SQL> show user
USER is "SYSTEM"
SQL> select name from v$database;
NAME
------------------
XE
● 2. DB링크 생성
- public DB링크 생성
SQL> create public database link ora11g_link connect to system identified by oracle using 'ora11g'; Database link created.
- public DB링크 삭제
drop public database link ORA11G_LINL;
SQL> select * from dba_db_links;
SQL> select * from dba_directories;
---
SQL> select * from dba_directories
2 where directory_name = 'DATA_PUMP_DIR';
OWNER
------------------------------------------------------------
DIRECTORY_NAME
------------------------------------------------------------
DIRECTORY_PATH
--------------------------------------------------------------------------------
SYS
DATA_PUMP_DIR
C:\oraclexe\app\oracle/admin/xe/dpdump/
SQL> host
Microsoft Windows [Version 10.0.22621.3007]
(c) Microsoft Corporation. All rights reserved.
● 3. expdp : db링크를 이용해서
C:\Users\itwill>expdp system/oracle directory=data_pump_dir dumpfile=hr_.dmp schemas=hr network_link=ora11g_link
---------
Export: Release 11.2.0.2.0 - Production on 화 1월 23 14:13:56 2024
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
FLASHBACK automatically enabled to preserve database integrity.
- tnsnames.ora 파일 확인하기
: 없으면 추가해주기 (db링크학습)XE = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = DESKTOP-6FUPLFQ)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE) ) ) ora11g = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.56.104)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = ora11g) ) )
● 4. import
C:\Users\itwill>sqlplus hr/hr
SQL*Plus: Release 11.2.0.2.0 Production on 화 1월 23 14:52:20 2024
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
SQL> show user
USER is "HR"
SQL> select * from tab;
SQL> select table_name, tablespace_name from user_tables;
SQL> drop table hr.job_history purge;
Table dropped.
SQL> select * from hr.job_history;
select * from hr.job_history
*
ERROR at line 1:
ORA-00942: table or view does not exist
C:\Users\itwill>impdp system/oracle directory=data_pump_dir dumpfile=hr.dmp tables=hr.job_history remap_tablespace=example:users exclude=grant
SQL> select * from hr.job_history;
EMPLOYEE_ID START_DA END_DATE JOB_ID DEPARTMENT_ID
----------- -------- -------- -------------------- -------------
102 01/01/13 06/07/24 IT_PROG 60
101 97/09/21 01/10/27 AC_ACCOUNT 110
101 01/10/28 05/03/15 AC_MGR 110
201 04/02/17 07/12/19 MK_REP 20
114 06/03/24 07/12/31 ST_CLERK 50
122 07/01/01 07/12/31 ST_CLERK 50
200 95/09/17 01/06/17 AD_ASST 90
176 06/03/24 06/12/31 SA_REP 80
176 07/01/01 07/12/31 SA_MAN 80
200 02/07/01 06/12/31 AC_ACCOUNT 90
10 rows selected.
-> remap_schema
● 1. 작업
SQL> select name from v$database;
NAME
------------------
XE
SQL> create user james identified by oracle default tablespace users quota unlimited on users;
User created.
SQL> grant create session to james;
Grant succeeded.
SQL> host
Microsoft Windows [Version 10.0.22621.3007]
(c) Microsoft Corporation. All rights reserved.
● 2. import
: remap_schema = hr:james
hr에서 james로 변경해준다.
C:\oraclexe\app\oracle>impdp system/oracle directory=data_pump_dir dumpfile=hr.dmp remap_schema=hr:james remap_tablespace=example:users exclude=grant
● 3. 확인
SQL> conn james/oracle
Connected.
SQL> select * from tab;
db링크를 이용하면 export 없이 import 가능하다.
● 1. 가져올 data의 유저명과 같게 유저 생성
SQL> select name from v$database;
NAME
------------------
XE
SQL> create user henry identified by oracle default tablespace users quota unlimited on users;
User created.
SQL> grant create session to henry;
Grant succeeded.
SQL> host
Microsoft Windows [Version 10.0.22621.3007]
(c) Microsoft Corporation. All rights reserved.
● 2. import
: 가져올 이름이 다르면 remap_schema 로 변경해주면 된다
C:\oraclexe\app\oracle>impdp system/oracle network_link=ora11g_link schemas=henry exclude=grant,trigger
SQLPLUS 켰을때 무슨 DB로 접속했는지 알려주는것
SET sqlprompt "_USER'@'_CONNECT_IDENTIFIER> "
위치 :
/u01/app/oracle/product/11.2.0.4/db_1/sqlplus/admin/glogin.sql
C:\oraclexe\app\oracle\product\11.2.0\server\sqlplus\admin