oracle管理、備份與恢復
數據庫
管理數據庫的用戶是sys、system,能夠將sys比做董事長,system比做總經理,二者有以下區別session
一、最重要的區別,存儲的數據的重要性不同oracle
sys:全部oracle的數據字典的基表和視圖都存放在sys用戶中,這些基表和視圖對於oracle的運行app
是相當重要的,由數據庫本身維護,任何用戶都不能手動更改。sys用戶擁有dba、sysdba、sysoperide
角色或權限,是oracle權限最高的用戶工具
system:用於存放次一級的內部數據,如oracle的一些特性或工具的管理信息。system用戶擁有post
dba、sysdba角色或系統權限spa
二、其次的區別,權限的不一樣rest
sys用戶必須以as sysdba或as sysoper形式登陸,不能以正常的方式登陸數據庫日誌
system若是正常登陸,它其實就是一個普通dba用戶,可是若是as sysdba登陸,其結果其實是做爲sys用戶登陸的
從登陸信息裏面能夠看出來
sysdba和sysoper的相同與不一樣,none爲沒有,也就是不一樣
startup數據庫 startup
shutdown數據庫 shutdown
alter database open/mount /backup alter database open/mount /backup
改變字符集 none
create database(建立數據庫) none
drop database(刪除數據庫) none
create spfile create spfile
alter database archivelog(歸檔日誌) alter database archivelog(歸檔日誌)
alter database recovery恢復數據庫 只能徹底恢復,不能執行不徹底恢復
擁有restricted session會話限制 擁有restricted session會話限制
可讓用戶做爲sys用戶鏈接 能夠進行一些基本的操做,但不能查看用戶數據
登陸以後用戶是sys 登陸以後用戶是public
dba權限的用戶
dba用戶是指具備dba角色的數據庫用戶,特權用戶能夠執行啓動實例,關閉實例等特殊操做
而dba用戶只能在啓動數據庫後才能執行各類管理操做
管理初始化參數
顯示初始化參數
一、show parameter 顯示系統參數
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數據庫的備份與恢復
邏輯備份是指使用工具export將數據對象的結構和數據導出文件的過程(數據導出到磁盤),邏輯恢復
是指當數據庫對象被誤操做而損壞後使用工具import利用備份的文件把數據對象導入到數據庫的過程
物理備份便可在數據庫open的狀態下進行也可在關閉數據庫後進行,可是邏輯備份和恢復只能在open的狀態下進行
導出:
導出具體分爲:導出用戶某張表,導出各用戶schema,導出整個數據庫三種方式
導出使用exp命令來完成,下面是常見的選項
userid:用於指定執行導出操做的用戶名,口令,鏈接字符串
tables:用於指定導出操做的表
owner:用於指定執行導出操做的方案
full=y:用於指定執行導出操做的數據庫
inctype:用於指定執行導出操做的增量類型
rows:用於指定執行導出操做是否要導出表中的數據
file:用於指定導出文件名
導出表:
一、導出本身的表(表的數據)
[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. 這裏出現警告信息,查出是字符集的問題
EXP-00091: Exporting questionable statistics.
Export terminated successfully with warnings.
字符集不匹配致使的,首先查詢數據庫的字符集
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"
而後再導出多張表的數據
[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.
發現警告消失了,成功解決警告信息
上述執行的是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.
只須要修改相應的表信息就行
導出表的結構,只導出結構
三、導出表的結構
exp userid=scott/redhat@orcl11g tables=emp file=/oracle/test/emp_table.dmp rows=n
四、使用直接導出方式
exp userid=scott/redhat@ocrl11g tables=emp file=/oracle/test/emp_direct.dmp direct=y
這種方式比默認的常規方式速度要快,當數據量大時,能夠考慮使用此種方法
這種方法須要數據庫的字符集和客戶端的字符集徹底一致,不然會報錯
導出方案
導出方案是指使用export工具導出一個方案或是多個方案中的全部對象(表、視圖、索引等等)和數據,並將它存放到文件中
一、導出本身的方案
[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.
二、導出其餘的方案
若是用戶要導出其餘方案,則須要dba的權限或是exp_full_database的權限,例如用system用戶
就能夠導出任何方案
exp userid=system/redhat@orcl11g owner=system,scott file=/oracle/test/system_scott.dmp
示例表示使用system用戶不能導出sys的schema
導出整個數據庫
導出數據庫是指利用export導出全部數據庫中的對象及數據,要求該用戶具備dba的權限
或者是exp_full_database權限
exp userid=system/redhat@orcl11g full=y inctype=comlete file=/oracle/test/full_oracle.dmp
導入表
一、導入表
imp userid=scott/redhat@orcl11g tables=emp file=
模擬刪除一張表,而後進行導出數據?
[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進行導入數據
$ 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
成功導入表數據
二、用system執行導入數據
模擬刪除
SQL> drop table student;
Table dropped
而後導入表數據
$ 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
三、導入表的結構
只導入表的結構而不導入數據
imp userid=scott/redhat@orcl11g tables=emp file= rows=n
四、導入數據
若是表的對象已經存在,那麼就能夠只導入數據,無需導入對象
imp userid=scott/redhat@orcl11g tables=emp file= ignore=y
導入方案
導入方案是指使用import工具將文件中的對象和數據導入到一個或多個方案中
若是要導入其餘用戶的方案,要求用戶具備dba的權限,或者是imp_full_database的權限
一、導入自身的方案
imp userid=scott/redhat@orcl11g file=/oracle/test/schema_scott.dmp
二、導入其餘用戶的方案(利用system用戶進行操做)
imp userid=system/redhat@orcl11g file=/oracle/test/schema_system_scott.dmp fromuser=system touser=scott
導入數據庫
在默認狀況下,當導入數據庫時,會導入全部結構和數據
imp userid=system/redhat full=y file=/oracle/test/database.dmp