概念:
1.用於備份恢復和數據遷移,可以實施全庫級、用戶級、表級的數據備份和恢復。
2.對於數據量在G級或者G級以內的,強調高可用性,可以容忍少量數據丟失的數據庫系統,Exp/Imp是可以嘗試的邏輯備份方式
3.處於客戶端程序,但都可以在客戶端和服務器使用。
export:從數據庫中導出數據到dump文件中
import:從dump文件中導入數據到數據庫中
dump文件:二進制格式的轉儲文件,不可手工編輯,可以跨OS/跨版本使用
使用場景:
數據庫之間傳送數據
數據庫的備份和恢復
三種模式
1:表方式,將指定表的數據exp/imp。
導出:
導出一張或幾張表:
$ exp user/pwd file=/dir/xxx.dmp log=xxx.log tables=table1,table2
導出某張表的部分數據
$ exp user/pwd file=/dir/xxx.dmp log=xxx.log tables=table1
query="where col1=\’ …\’
and col2 \<…\”
導入:
導入一張或幾張表
$ imp user/pwd file=/dir/xxx.dmp log=xxx.log tables=table1,table2fromuser=dbuser
touser=dbuser2 commit=y ignore=y
詳細過程:
創建一個表空間
SYS@orcl>createtablespace test_tbs
2 datafile '/home/oracle/test/test_data02.dbf'
3 size5m
4 autoextend on maxsize 512M;
創建兩個用戶
SYS@orcl>createuser testuser identified by testuser default tablespace test_tbs;
User created.
SYS@orcl>createuser testuser2 identified by testuser2 default tablespace test_tbs;
User created.
給新用戶授權
SYS@orcl>grant CREATE SESSION,CREATE TABLE to testuser;
Grant succeeded.
SYS@orcl>grant CREATE SESSION,CREATE TABLE to testuser2;
Grant succeeded.
賦予用戶在表空間中能夠使用的配額
SYS@orcl>alteruser testuser1 quota 50M on test_tbs;
User altered.
SYS@orcl>alteruser testuser2 quota 50M on test_tbs;
User altered.
登陸用戶testuser
SYS@orcl>conntestuser/testuser;
Connected.
創建表,插入數據
TESTUSER@orcl>create table testuser.exptest( a int);
Table created.
TESTUSER@orcl>insertinto testuser.exptest values (1);
1 row created.
TESTUSER@orcl>commit;
Commit complete.
TESTUSER@orcl>select* from testuser.exptest;
A
----------
1
登陸用戶testuser2,確定沒有exptest表
TESTUSER2@orcl>select* from testuser2.exptest;
select * fromtestuser2.exptest
*
ERROR at line 1:
ORA-00942: tableor view does not exist
切換到Linux終端界面,登陸oracle用戶
[root@11g_s ~]#su – oracle
[oracle@11g_s~]$ exp testuser/testuser@orcl tables=exptest file ~/test/exp/tb_exp.dmplog=~/test/exp/testuserExp.log
Export: Release11.2.0.1.0 - Production on Tue Feb 23 18:53:25 2016
Copyright (c)1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to:Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With thePartitioning, OLAP, Data Mining and Real Application Testing options
Export done inUS7ASCII character set and UTF8 NCHAR character set
server usesWE8MSWIN1252 character set (possible charset conversion)
About to exportspecified tables via Conventional Path ...
. . exportingtable EXPTEST 1 rows exported
EXP-00011:TESTUSER.FILE does not exist
EXP-00009: noprivilege to export /HOME/ORACLE/TEST/EXP/TB_EXP's table DMP
Exportterminated successfully with warnings.
[oracle@11g_s~]$ exp testuser/testuser@orcl tables=exptest file=~/test/exp/tb_exp.dmplog=~/test/exp/testuserExp.log
Export: Release11.2.0.1.0 - Production on Tue Feb 23 18:54:20 2016
Copyright (c)1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to:Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With thePartitioning, OLAP, Data Mining and Real Application Testing options
Export done inUS7ASCII character set and UTF8 NCHAR character set
server usesWE8MSWIN1252 character set (possible charset conversion)
About to exportspecified tables via Conventional Path ...
. . exportingtable EXPTEST 1 rows exported
Export terminated successfully without warnings.
[oracle@11g_s~]$ imp testuser2/testuser2@orcl fromuser=testuser touser=testuser2file=~/test/exp/tb_exp.dmp log=~/test/exp/tb_imp.log
Import: Release11.2.0.1.0 - Production on Tue Feb 23 18:58:36 2016
Copyright (c)1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to:Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With thePartitioning, OLAP, Data Mining and Real Application Testing options
Export filecreated by EXPORT:V11.02.00 via conventional path
Warning: theobjects were exported by TESTUSER, not by you
import done inUS7ASCII character set and UTF8 NCHAR character set
import serveruses WE8MSWIN1252 character set (possible charset conversion)
. importingTESTUSER's objects into TESTUSER2
. . importingtable "EXPTEST" 1rows imported
Import terminated successfully without warnings.
切換到sqlplus界面,登陸testuser2用戶查看是否導入成功
TESTUSER2@orcl>select* from testuser2.exptest;
A
----------
1
不同用戶之間表的Exp/Imp成功
2:用戶方式,將指定用戶的所有對象及數據導出/導入
導出:
$exp user/pwd file=/dir/xxx.dmp log=xxx.log owner=(xx,yy) # 要導出數據的數據庫帳號
只導出數據對象,不導出數據(rows=n )
$exp user/pwd file=/dir/xxx.dmp log=xxx.log owner=user rows=n #是否導出行數據,默認y,n則否
導入:
$imp user/pwd file=/dir/xxx.dmp log=xxx.log fromuser=dbuser touser=dbuser2
commit=yignore=y
詳細過程:
[oracle@11g_s~]$ exp bankuser/bankuser file=~/bank/exp/bank_user.dmp log=~/bank/exp/
bank_user.logowner=bankuser
Export: Release11.2.0.1.0 - Production on Wed Feb 24 04:29:34 2016
Copyright (c)1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to:Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With thePartitioning, OLAP, Data Mining and Real Application Testing options
Export done inWE8MSWIN1252 character set and UTF8 NCHAR character set
. exportingpre-schema procedural objects and actions
. exportingforeign function library names for user BANKUSER
. exportingPUBLIC type synonyms
. exportingprivate type synonyms
. exportingobject type definitions for user BANKUSER
About to exportBANKUSER's objects ...
. exportingdatabase links
. exportingsequence numbers
. exportingcluster definitions
. about toexport BANKUSER's tables via Conventional Path ...
. . exportingtable EXPTEST 2 rows exported
. exportingsynonyms
. exportingviews
. exportingstored procedures
. exportingoperators
. exportingreferential integrity constraints
. exportingtriggers
. exportingindextypes
. exportingbitmap, functional and extensible indexes
. exportingposttables actions
. exportingmaterialized views
. exportingsnapshot logs
. exporting jobqueues
. exportingrefresh groups and children
. exportingdimensions
. exportingpost-schema procedural objects and actions
. exportingstatistics
Export terminated successfully without warnings.
[oracle@11g_s~]$ imp bankuser2/bankuser2 file=~/bank/exp/bank_user.dmp log=~/bank/exp/
bank_user.logfromuser=bankuser touser=bankuser2 commit=y ignore=y
Import: Release11.2.0.1.0 - Production on Wed Feb 24 04:32:34 2016
Copyright (c)1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to:Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With thePartitioning, OLAP, Data Mining and Real Application Testing options
Export filecreated by EXPORT:V11.02.00 via conventional path
Warning: theobjects were exported by BANKUSER, not by you
import done inWE8MSWIN1252 character set and UTF8 NCHAR character set
. . importingtable "EXPTEST" 2rows imported
Import terminated successfully without warnings.
3.全庫方式,將數據庫中的所有對象導出/導入
導出:
expuserid=sys/sys@orcl BUFFER=8192 FILE=x:\*.dmp LOG=x:\*.log full=y grants=y
實際工作中用的最多的導入命令
impuserid=sys/sys@orcl BUFFER=81920 FILE=x:\*.dmp LOG=x:\*.log full=y grants=y ignore=y
ignore解釋:
當要導入數據庫中已經存在了某個表(test),如果該表沒有唯一性約束,那麼在導入時加參數ignore=y,則會把數據完全導入到表中,而且不報錯。
當表已經存在了唯一性約束,特別是主鍵的約束,那麼在導入時,只導入主鍵中不存在的記錄. 導入過程中會有警告.
操作與其它模式差不多