03 Files

本章提要
-----------------------------------------------
组成 oracle 的 8 种主要文件(包括 instance 和 database)
instance: parameter file, trace file, alert file
database: data file, temp file, control file, redo log file, password file
简单概述:
parameter file: instance 参数初始化, 哪里可以找到control file.
trace file: diagnostic files created by a server process
alert file: dba 检查问题的中心
data file: 存储例如 table, index 等
temp file: 临时存储 和 disk-based sort
control file: 哪里可以找到 datafile, redolog file, temp file, 一些其他的元数据, backup info.
redo log file: transaction logs
password file: are used to authenticate users performing administrative activities over the network.

-- 10g 以后新增加的两个文件, 为了更好的backup 和 recovery
change-tracking file: 更好的 incremental backup or oracle data
flashback log file: store "before images" of database blocks

-- 其他类型文件 --
dump files: Export 产生的文件
Data Pump files: Data Pump Export process 产生的文件
Flat files: 可以用text editor 打开的文件, 一般用作 loading data

-- 最重要的文件是 data file, redo log file.
-----------------------------------------------

1. parameter file
    很多种类, 例如 tnsnames.ora, listener.ora 等, 不过这里重点讨论的是, init<sid>.ora 和 spfile

SID : 概念再补充: the sid is a site identifier, it and ORACLE_HOME(where the oracle software is
    installed) are hashed together in UNIX to create a unique key name for creating or attaching
    a Shared Global Area(SGA) memory region. If your ORACLE_SID or ORACLE_HOME is not set correctly,
    you’ll get the ORACLE NOT AVAILABLE error.

    select value from v$parameter where name = 'db_block_size' -- 查询 parameter 方法
    show parameter db_block_s        -- 查询 parameter 方法, 可以使用模糊查询, 所以比较好
    还有一些非正式声明的参数, 它们是以下划线开头, 例如 _trace_file_public, 一般我们不用关心这些非正式
    声明的参数, 大师一般只是设置一个 _trace_file_public=true, 作用是设置 trace_file read only by all.
    因为有很多常用工具需要读取tracefile, 比如 SQL_TRACE, TIMED_STATISTICS, TKPROF
    但是在生产库, 不需要设置这个参数.(生产库不设置任何 undocumented
    spfile 修改参数方法:
    Alter system set parameter=value <comment='text'> <deferred>
                    <scope=memory|spfile|both> <sid='sid|*'>
    如果我们想删除我们之前设置的参数, 而是用系统默认的参数, 那么, 我们需要使用如下命令:(reset)
    alter system reset sort_area_size scope=spfile;
    可以通过使用 startup pfile=filename, 这样, instance启动时会参考Pfile, 而不是spfile.
    之前说过parameter file的优先级.

    如果spfile损坏了怎么办, 你有很多办法, 首先如果你在unix系统下, 因为spfile很小, 可以使用strings命令
    查看spfile中的内容, 进而生成pfile(可以使用editor text编辑), 然后再create spfile from pfile 就可以.
    在windows系统, 可以使用 write.exe(WordPad)打开这个文件, 进而生成pfile, 然后同样命令创建spfile.

    如果spfile丢了, 那么你可以借助 log file来恢复spifle, 在log file中有instance启动的信息, 里边有参数
    设置的情况, copy出来, 制作pfile, 进而生成spfile.
    
2. Trace files
    Trace files are a source of debugging information.(是数据库自己debug的information)
    数据库编写人员, 会将数据库出现的问题, 写日志到trace file中.
    oracle 对于 debug 而提供的工具有:
    (我们不需要直接读tracefile, 我们读不懂, tracefile是为oracle support 人员准备的, 那么我们可以借助下面工具
     来进行一些查看.)
    1) v$ views: Most V$ views contain “debug” information.v$waitstat, v$session_event 等.
    2) audit: This command allows you to specify what events the database should record for later analysis
    3) resource manager: (DBMS_RESOURCE_MANAGER)(CPU, I/O)
    4) oracle events: ask Oracle to produce trace or diagnostic information as needed
    5) dbms_trace: within the PL/SQL engine exhaustively records the call tree of stored procedures 等.
    6) database event trigger: These triggers, such as ON SERVERERROR.
    7) SQL_TRACE/DBMS_MONITOR: This is used to view the exact SQL, wait events and other performance.
    
    Trace files 包含两种:
    1) 我们比较关心的, contain diagnostic information about your session and will help you tune your
        application to optimize its performance and diagnose any bottlenecks it is experiencing.
    2) 我们一般不关心, 一般是数据库原厂支持人员才能看懂的诊断内容, 比如 ORA-00600 "Internal Error"

    Requested Trace Files (请求相关的 trace file)
    DBMS_MONITOR( alter session set sql_trace=true)
    or using the extended trace facility via the 10046 event, might be as follows:
    alter session set events '10046 trace name context forever, level 12'
    These trace files contain diagnostic and performance related information(这种trace file最有用, 会经常使用)
    Location: user_dump_dest(dedicated server), background_dump_dest(shared server)
    show parameter dump_dest, 会显示 3 个 location, 其中:
    background_dump_dest: "server" process, oracle background process
    core_dump_dest: 进程的更加详尽的诊断文件
    user_dump_dest: (dedicated server)
   

with home
as
(select value home
   from v$diag_info
  where name = 'ADR Home'
)
select name,
        case when value <> home.home
            then replace(value, home.home, '$home$')
        else value
        end value
  from v$diag_info, home
/
View Code


    01 trace_file_location.sql
    在查询出来的结果中, 两个参数比较重要: ( 11g 之后使用 )
    Diag Trace: trace地址, 包括background_trace file 和 user dump trace file.
    Default Trace File: your current session's trace file.
    
    trace name : oraclesid_ora_pid.trc
    v$PROCESS: 可以查看到pid
    V$SESSION: 可以查看到session信息
    V$INSTANCE: 可以查看到 oracle sid.
    所以, 可以通过另外的方法, 查看到当前的 trace file
   

column trace new_val T
select c.value || '/' || d.instance_name || '_ora_' ||
       a.spid || '.trc' ||
       case when e.value is not null then
            '_' || e.value 
       end trace
  from v$process a, v$session b, v$parameter c, v$instance d, v$parameter e
 where a.addr = b.paddr
   and b.audsid = userenv('sessionid')
   and c.name = 'user_dump_dest'
   and e.name = 'tracefile_identifier'
/
View Code


    02 trace_file_now.sql
     开通当前 trace file 的方法: exec dbms_monitor.session_trace_enable
    你可以为 trace file 加上标签, 这样你就更容易找到当前是哪个 tracefile, 办法是:
    alter session set tracefile_identifier = 'LooK_LEON'; -- 在当前的 session 下
    exec dbms_monitor.session_trace_enable;
    这样就会在相应目录下看到带 'Look_LEON'名称的trace file, 那么很显然这就是当前的tracefile.
    这样的话, 文件名是 <ORACLE_SID>_ora_<PROCESS_ID>_Look_LEON.trc

    Trace Files Generated in Reponse to Internal Errors
    例如: ORA-00600: internal error code, 类似这种错误, 是 oracle的bug, 我们只能请求oracle的服务.
    所以, 找到对应的 tracefile 文件是十分重要的, 因为可以将它上传, 让 oracle support人员查看.

3. Alert File
    alert log file 很重要, 是数据库日志文件.
    alert log file 是文本文档文件, 可以看懂.

4. Data file    
    一般创建数据库时最少包括3个datafile:
    system tablespace 对应 datafile.
    user tablespace 对应 datafile.
    sysaux tablespace 对应 datafile (10g 以后版本)
    
    4种datafile 结构 (物理结构)
    1) operating system(os)file system: 一般操作系统文件, 我用的就是这种
    2) 未安装任何os的磁盘, 这个磁盘对Oracle来说是个大文件 (这种很少使用)
    3) Automatic Storage management(ASM): 做了一个抽象ASM系统, 即ASM是在OS与database中间
    4) Clustered file system: 为了 RAC, 看起来像第一种file system
    个人感觉, (1), (3)比较常用

    database 分层结构
    tablespace: 组成了database
    segments: 实际存储对象, 比如 table, index 等, Every object that consumes storage is ultimately stored in a
        single segment. There are undo segments, temporary segments,cluster segments, index segments and so on.
    extents: An extent is a logically contiguous allocation of space in a file.
    blocks: A block is the smallest unit of space allocation in Oracle.    一般分为 2k,4k,8k,16k
   
    <03_01.jpg>
    注意: 这个图我们尽量从逻辑角度看, 所以看到extent是连续的, 但是其实不是这样.
    extent 之间不连续, extent 内部连续。
    <03_02.jpg>

   
    block structure
5. Temp Files
    Temp files never have REDO generated for them, although they can have UNDO generated. Thus,
    there will be REDO generated working with temporary tables since UNDO is always protected by REDO.
6. Control files
    the control files tell the instance where the database and online redo log files are.
    developer 基本上用不到 control file.
7. Redo Log Files
8. password Files
    本地组内部的User, 都可以不用密码登陆数据库, 所以不一定非的是oracle这个用户
    而如果在网络上连接数据库, 操作系统认证就不行了, password file 只可以通过网络认证为 sysdba的用户
    首先启动数据库, 设置参数 remote_login_passwordfile=EXCLUSIVE, 如果是shared表示多个数据库可以共用密码文件
    一般这个参数, 默认就是 EXCLUSIVE
    然后使用, orapwd file=orapw$ORACLE_SID password=aaa entries=20(最大连接数), 这时我们可以使用 sys 用户远程
    连接上来, 但是我们自己随便建立用户, 不能连接上来, 因为我们随便建立的用户不是SYSDBA权限, 所以, 我们要先授权
    我们新建用户的权限, grant sysdba to leon; 然后就可以从远端用 leon 用户连接上来了. 另外注意, orapwd是linux命令
    另外, 远程连接时, 如果server端采用动态注册的话, 那么必须要开启instance时, 才能动态注册, 所以, 这样就没有办法
    使用这种远程连接到没有开启instance的数据库, 所以, 这时需要使用静态注册, 才可以远端来接来sysdba,远端开启数据库
9. dump files
    dmp 文件是独立于平台(os), 可以用于重建表, 模式, 甚至整个数据库, 导入工具的作用就是读取dmp文件, 执行ddl语句,
    并加载它找到的所有数据. 现在这个已经有被 data pump 取代的形式.
10. data pump files
    IMPDP 和 EXPDP 使用这种文件格式, 使用方法与 IMP 和 EXP 一样.    

posted @ 2014-07-23 18:06  神之一招  阅读(235)  评论(0编辑  收藏  举报