oracle管理、備份與恢復
創(chuàng)新互聯(lián)建站是一家專業(yè)提供加查企業(yè)網(wǎng)站建設,專注與成都網(wǎng)站制作、成都網(wǎng)站建設、H5響應式網(wǎng)站、小程序制作等業(yè)務。10年已為加查眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設公司優(yōu)惠進行中。
管理數(shù)據(jù)庫的用戶是sys、system,可以將sys比作董事長,system比作總經(jīng)理,兩者有如下區(qū)別
1、最重要的區(qū)別,存儲的數(shù)據(jù)的重要性不一樣
sys:所有oracle的數(shù)據(jù)字典的基表和視圖都存放在sys用戶中,這些基表和視圖對于oracle的運行
是至關重要的,由數(shù)據(jù)庫自己維護,任何用戶都不能手動更改。sys用戶擁有dba、sysdba、sysoper
角色或權限,是oracle權限最高的用戶
system:用于存放次一級的內(nèi)部數(shù)據(jù),如oracle的一些特性或工具的管理信息。system用戶擁有
dba、sysdba角色或系統(tǒng)權限
2、其次的區(qū)別,權限的不同
sys用戶必須以as sysdba或as sysoper形式登錄,不能以正常的方式登錄數(shù)據(jù)庫
system如果正常登錄,它其實就是一個普通dba用戶,但是如果as sysdba登錄,其結果實際上是作為sys用戶登錄的
從登錄信息里面可以看出來
sysdba和sysoper的相同與不同,none為沒有,也就是不同
startup數(shù)據(jù)庫 startup
shutdown數(shù)據(jù)庫 shutdown
alter database open/mount /backup alter database open/mount /backup
改變字符集 none
create database(創(chuàng)建數(shù)據(jù)庫) none
drop database(刪除數(shù)據(jù)庫) none
create spfile create spfile
alter database archivelog(歸檔日志) alter database archivelog(歸檔日志)
alter database recovery恢復數(shù)據(jù)庫 只能完全恢復,不能執(zhí)行不完全恢復
擁有restricted session會話限制 擁有restricted session會話限制
可以讓用戶作為sys用戶連接 可以進行一些基本的操作,但不能查看用戶數(shù)據(jù)
登錄之后用戶是sys 登錄之后用戶是public
dba權限的用戶
dba用戶是指具有dba角色的數(shù)據(jù)庫用戶,特權用戶可以執(zhí)行啟動實例,關閉實例等特殊操作
而dba用戶只能在啟動數(shù)據(jù)庫后才能執(zhí)行各種管理操作
管理初始化參數(shù)
顯示初始化參數(shù)
1、show parameter 顯示系統(tǒng)參數(shù)
SQL> show parameter;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
O7_DICTIONARY_ACCESSIBILITY boolean FALSE
active_instance_count integer
aq_tm_processes integer 0
archive_lag_target integer 0
asm_diskgroups string
asm_diskstring string
asm_power_limit integer 1
Oracle數(shù)據(jù)庫的備份與恢復
邏輯備份是指使用工具export將數(shù)據(jù)對象的結構和數(shù)據(jù)導出文件的過程(數(shù)據(jù)導出到磁盤),邏輯恢復
是指當數(shù)據(jù)庫對象被誤操作而損壞后使用工具import利用備份的文件把數(shù)據(jù)對象導入到數(shù)據(jù)庫的過程
物理備份即可在數(shù)據(jù)庫open的狀態(tài)下進行也可在關閉數(shù)據(jù)庫后進行,但是邏輯備份和恢復只能在open的狀態(tài)下進行
導出:
導出具體分為:導出用戶某張表,導出各用戶schema,導出整個數(shù)據(jù)庫三種方式
導出使用exp命令來完成,下面是常見的選項
userid:用于指定執(zhí)行導出操作的用戶名,口令,連接字符串
tables:用于指定導出操作的表
owner:用于指定執(zhí)行導出操作的方案
full=y:用于指定執(zhí)行導出操作的數(shù)據(jù)庫
inctype:用于指定執(zhí)行導出操作的增量類型
rows:用于指定執(zhí)行導出操作是否要導出表中的數(shù)據(jù)
file:用于指定導出文件名
導出表:
1、導出自己的表(表的數(shù)據(jù))
[oracle@aliyun_test oracle]$ exp userid=scott/redhat@test tables=emp file=/oracle/test/scott_emp.dmp
Export: Release 11.2.0.1.0 - Production on Mon Sep 26 14:33:45 2016
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
EXP-00056: ORACLE error 12154 encountered
ORA-12154: TNS:could not resolve the connect identifier specified
EXP-00000: Export terminated unsuccessfully
導出報錯,然后進行確定Oracle實例是否是test,查看
[oracle@aliyun_test admin]$ cat tnsnames.ora
# tnsnames.ora Network Configuration File: /u01/app/product/11.2.0/dbhome_1/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
ORCL11G =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = aliyun_test)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl11g.us.oracle.com)
)
)
Oracle實例是ORCL11G,于是重新導出
[oracle@aliyun_test oracle]$ exp userid=scott/redhat@orcl11g tables=emp file=/oracle/test/scott_emp.dmp
Export: Release 11.2.0.1.0 - Production on Mon Sep 26 14:38:11 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 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in US7ASCII character set and UTF8 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)
About to export specified tables via Conventional Path ...
. . exporting table EMP 14 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
Export terminated successfully with warnings.
[oracle@aliyun_test oracle]$ ll /oracle/test/scott_emp.dmp
-rw-r--r-- 1 oracle oinstall 16384 Sep 26 14:38 /oracle/test/scott_emp.dmp
導出多張表
[oracle@aliyun_test oracle]$ exp userid=scott/redhat@orcl11g tables=emp,dept file=/oracle/test/scott_emp_dept.dmp
Export: Release 11.2.0.1.0 - Production on Mon Sep 26 15:10:05 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 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in US7ASCII character set and UTF8 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)
About to export specified tables via Conventional Path ...
. . exporting table EMP 14 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
. . exporting table DEPT 4 rows exported
EXP-00091: Exporting questionable statistics. 這里出現(xiàn)警告信息,查出是字符集的問題
EXP-00091: Exporting questionable statistics.
Export terminated successfully with warnings.
字符集不匹配導致的,首先查詢數(shù)據(jù)庫的字符集
SQL> select sys_context('userenv','language') from dual;
SYS_CONTEXT('USERENV','LANGUAGE')
--------------------------------------------------------------------------------
AMERICAN_AMERICA.AL32UTF8
然后再在設置與之相同的字符集
[oracle@aliyun_test oracle]$ export NLS_LANG="AMERICAN_AMERICA.AL32UTF8"
然后再導出多張表的數(shù)據(jù)
[oracle@aliyun_test oracle]$ exp userid=scott/redhat@orcl11g tables=emp,dept file=/oracle/test/scott_emp_dept.dmp
Export: Release 11.2.0.1.0 - Production on Mon Sep 26 15:11:56 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 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in AL32UTF8 character set and UTF8 NCHAR character set
About to export specified tables via Conventional Path ...
. . exporting table EMP 14 rows exported
. . exporting table DEPT 4 rows exported
Export terminated successfully without warnings.
發(fā)現(xiàn)警告消失了,成功解決警告信息
上述執(zhí)行的是Scott用戶導出自己的表,下面介紹用system用戶來導出scott用戶的表
[oracle@aliyun_test oracle]$ exp userid=system/redhat@orcl11g tables=scott.emp file=/oracle/test/system_emp.dmp
Export: Release 11.2.0.1.0 - Production on Mon Sep 26 15:17:54 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 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in AL32UTF8 character set and UTF8 NCHAR character set
About to export specified tables via Conventional Path ...
Current user changed to SCOTT
. . exporting table EMP 14 rows exported
Export terminated successfully without warnings.
只需要修改相應的表信息就行
導出表的結構,只導出結構
3、導出表的結構
exp userid=scott/redhat@orcl11g tables=emp file=/oracle/test/emp_table.dmp rows=n
4、使用直接導出方式
exp userid=scott/redhat@ocrl11g tables=emp file=/oracle/test/emp_direct.dmp direct=y
這種方式比默認的常規(guī)方式速度要快,當數(shù)據(jù)量大時,可以考慮使用此種方法
這種方法需要數(shù)據(jù)庫的字符集和客戶端的字符集完全一致,否則會報錯
導出方案
導出方案是指使用export工具導出一個方案或是多個方案中的所有對象(表、視圖、索引等等)和數(shù)據(jù),并將它存放到文件中
1、導出自己的方案
[oracle@aliyun_test oracle]$ exp userid=scott/redhat@orcl11g owner=scott file=/oracle/test/schema_scott.dmp
Export: Release 11.2.0.1.0 - Production on Mon Sep 26 15:22:27 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 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in AL32UTF8 character set and UTF8 NCHAR character set
. exporting pre-schema procedural objects and actions
. exporting foreign function library names for user SCOTT
. exporting PUBLIC type synonyms
. exporting private type synonyms
. exporting object type definitions for user SCOTT
About to export SCOTT's objects ...
. exporting database links
. exporting sequence numbers
. exporting cluster definitions
. about to export SCOTT's tables via Conventional Path ...
. . exporting table DEPT 4 rows exported
. . exporting table EMP 14 rows exported
. . exporting table SALGRADE 5 rows exported
. . exporting table STUDENT 0 rows exported
. exporting synonyms
. exporting views
. exporting stored procedures
. exporting operators
. exporting referential integrity constraints
. exporting triggers
. exporting indextypes
. exporting bitmap, functional and extensible indexes
. exporting posttables actions
. exporting materialized views
. exporting snapshot logs
. exporting job queues
. exporting refresh groups and children
. exporting dimensions
. exporting post-schema procedural objects and actions
. exporting statistics
Export terminated successfully without warnings.
2、導出其他的方案
如果用戶要導出其他方案,則需要dba的權限或是exp_full_database的權限,例如用system用戶
就可以導出任何方案
exp userid=system/redhat@orcl11g owner=system,scott file=/oracle/test/system_scott.dmp
示例表示使用system用戶不能導出sys的schema
導出整個數(shù)據(jù)庫
導出數(shù)據(jù)庫是指利用export導出所有數(shù)據(jù)庫中的對象及數(shù)據(jù),要求該用戶具有dba的權限
或者是exp_full_database權限
exp userid=system/redhat@orcl11g full=y inctype=comlete file=/oracle/test/full_oracle.dmp
導入表
1、導入表
imp userid=scott/redhat@orcl11g tables=emp file=
模擬刪除一張表,然后進行導出數(shù)據(jù)?
[oracle@oracle11g ~]$ exp userid=scott/redhat@orcl11g tables=student
file=/oracle/test/student.dmp
SQL> drop table student;
Table dropped
SQL> desc student;
Object student does not exist
然后imp進行導入數(shù)據(jù)
$ imp userid=scott/redhat@orcl11g tables=student file=/oracle/test/student.dmp
最后查看表是否成功導入?
SQL> desc student;
Name Type Nullable Default Comments
-------- ------------ -------- ------- --------
XH NUMBER(4) Y
XM VARCHAR2(20) Y
SEX CHAR(2) Y
SAL NUMBER(7,2) Y
BIRTHDAY DATE Y
成功導入表數(shù)據(jù)
2、用system執(zhí)行導入數(shù)據(jù)
模擬刪除
SQL> drop table student;
Table dropped
然后導入表數(shù)據(jù)
$ imp userid=system/redhat@orcl11g tables=student file=/oracle/test/student.dmp touser=scott
然后查看
SQL> desc student;
Name Type Nullable Default Comments
-------- ------------ -------- ------- --------
XH NUMBER(4) Y
XM VARCHAR2(20) Y
SEX CHAR(2) Y
SAL NUMBER(7,2) Y
BIRTHDAY DATE Y
3、導入表的結構
只導入表的結構而不導入數(shù)據(jù)
imp userid=scott/redhat@orcl11g tables=emp file= rows=n
4、導入數(shù)據(jù)
如果表的對象已經(jīng)存在,那么就可以只導入數(shù)據(jù),無需導入對象
imp userid=scott/redhat@orcl11g tables=emp file= ignore=y
導入方案
導入方案是指使用import工具將文件中的對象和數(shù)據(jù)導入到一個或多個方案中
如果要導入其他用戶的方案,要求用戶具有dba的權限,或者是imp_full_database的權限
1、導入自身的方案
imp userid=scott/redhat@orcl11g file=/oracle/test/schema_scott.dmp
2、導入其他用戶的方案(利用system用戶進行操作)
imp userid=system/redhat@orcl11g file=/oracle/test/schema_system_scott.dmp fromuser=system touser=scott
導入數(shù)據(jù)庫
在默認情況下,當導入數(shù)據(jù)庫時,會導入所有結構和數(shù)據(jù)
imp userid=system/redhat full=y file=/oracle/test/database.dmp