Oracle之管理以及exp、imp的使用

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

相关文章
相关标签/搜索