Oracle 数据库实例简介

 

 

一:Oracle 数据库实例简介

image

            1:数据库实例的启动顺序:

image

使用数据库其实就是访问内存。即:数据库实例。数据库的启动是顺序是 先 nomount ---->  mount ----->  open.

image

            2:数据库文件为:数据文件、控制文件、日志文件

数据库就是磁盘上的一些物理文件。可以利用 v$datafile 视图进行查看。如“/u01/app/oracle/oradata/orcl/system01.dbf”该路径下的“system01.dbf”被称作数据文件。数据库的数据信息都是存放在类似“*.dbf”的文件下。

复制代码
  1 [oracle@localhost ~]$ sqlplus / as sysdba;
  2 
  3 SQL*Plus: Release 11.2.0.3.0 Production on Tue Apr 17 22:58:17 2018
  4 
  5 Copyright (c) 1982, 2011, Oracle.  All rights reserved.
  6 
  7 
  8 Connected to:
  9 Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
 10 With the Partitioning, OLAP, Data Mining and Real Application Testing options
 11 
 12 SYS@orcl> select name from v$datafile;
 13 
 14 NAME
 15 --------------------------------------------------------------------------------
 16 /u01/app/oracle/oradata/orcl/system01.dbf
 17 /u01/app/oracle/oradata/orcl/sysaux01.dbf
 18 /u01/app/oracle/oradata/orcl/undotbs01.dbf
 19 /u01/app/oracle/oradata/orcl/users01.dbf
 20 /u01/app/oracle/oradata/orcl/example01.dbf
 21 /u01/app/oracle/oradata/orcl/datafile1.dbf
 22 /u01/app/oracle/oradata/orcl/ts1.dbf
 23 /u01/app/oracle/oradata/orcl/datafile3.dbf
 24 /u01/app/oracle/oradata/orcl/datafile4.dbf
 25 /u01/app/oracle/oradata/orcl/datafile5.dbf
 26 /u01/app/oracle/oradata/orcl/datafilebig_1.dfb
 27 
 28 NAME
 29 --------------------------------------------------------------------------------
 30 /u01/app/oracle/oradata/orcl/ts2.dbf
 31 /u01/app/oracle/oradata/orcl/ts3.dbf
 32 /u01/app/oracle/oradata/orcl/tbsp_1.dbf
 33 
 34 14 rows selected.
 35 
 36 SYS@orcl>
复制代码

再者从视图V$CONTROLFILE查看数据库的控制文件。"/u01/app/oracle/oradata/orcl/control01.ctl " 的“orcl”的目录是数据库实例名

复制代码
  1 SYS@orcl> select name from v$controlfile;
  2 
  3 NAME
  4 --------------------------------------------------------------------------------
  5 /u01/app/oracle/oradata/orcl/control01.ctl
  6 /u01/app/oracle/fast_recovery_area/orcl/control02.ctl
  7 /u01/app/oracle/fast_recovery_area/orcl/control03.ctl
  8 
  9 SYS@orcl>
复制代码

查看日志文件信息

复制代码
  1 SYS@orcl> select member from v$logfile;
  2 
  3 MEMBER
  4 --------------------------------------------------------------------------------
  5 /u01/app/oracle/oradata/orcl/redo03.log
  6 /u01/app/oracle/oradata/orcl/redo02.log
  7 /u01/app/oracle/oradata/orcl/redo01.log
  8 /home/oracle/oracle_system_files_back/logfile_bak/logfile_bak_20180228/redo20180
  9 228.log
 10 
 11 /home/oracle/oracle_system_files_back/logfile_bak/logfile_bak_20180228/redo20180
 12 228_1.log
 13 
 14 /home/oracle/oracle_system_files_back/logfile_bak/logfile_bak_20180228/redo04.lo
 15 g
 16 
 17 MEMBER
 18 --------------------------------------------------------------------------------
 19 
 20 
 21 6 rows selected.
 22 
 23 SYS@orcl>
复制代码

 

 

 

1:Oracle 实例结构

image

 

用户 通过访问数据库实例中的数据块来读取数据文件中的数据信息。用户不能直接读取数据文件中的数据信息。

数据库进程:

  • 客户端进程
  • Oracle服务器端进程
  1.    服务器进程
  2.    后台进程

  当有客户端进程链接到Oracle数据库的时候,Oracle服务器会相应的给与分配一个服务器进程来与之匹配。然后通过服务器进程来访问获取数据库实例的数据信息。当服务器进程终止的时候,数据库实例还是可以在运行的。数据库实例是有数据库服务器的内存和数据库后台进程组成的。所以,服务器进程终止了,并不影响数据库实例的。

imageimage

 

2:数据库实例配置

 

image

image

集群的作用:

  1. 保障数据库实例可以正常运行
  2. 确保负载均衡

 

切换数据库实例ID:

image

image

当 $ORACLE_SID 的值为那个 oracle实例名;则通过“sqlplus / as sysdba;”进行访问的数据库实例就是显示的那个。

 

 

image

image

image

 

3:Oracle 系统标识符(SID)

image

image

复制代码
  1 [oracle@localhost dbs]$ echo $ORACLE_SID
  2 orcl
  3 [oracle@localhost dbs]$ export ORACLE_SID=orcl
  4 [oracle@localhost dbs]$ echo $ORACLE_SID
  5 orcl
  6 [oracle@localhost dbs]$ cd $ORACLE_HOME/dbs
  7 [oracle@localhost dbs]$ pwd
  8 /u01/app/oracle/product/11.2.0/db_1/dbs
  9 [oracle@localhost dbs]$ ls
 10 dbsorapwPROD1  hc_orcl.dat  hc_PROD1.dat  init.ora  initorcl.ora  initPROD1.ora  lkORCL  lkPROD1  orapworcl  spfileorcl.ora  spfilePROD1.ora
 11 [oracle@localhost dbs]$
复制代码

 

 

 

二:实例启动和关闭概述

image

1:实例和数据库启动概述

image

        image

image

image

image

image

复制代码
  1 [oracle@localhost dbs]$ sqlplus / as sysdba;
  2 
  3 SQL*Plus: Release 11.2.0.3.0 Production on Wed Apr 18 00:28:18 2018
  4 
  5 Copyright (c) 1982, 2011, Oracle.  All rights reserved.
  6 
  7 
  8 Connected to:
  9 Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
 10 With the Partitioning, OLAP, Data Mining and Real Application Testing options
 11 
 12 SYS@orcl> show user
 13 USER is "SYS"
 14 SYS@orcl>
 15 
复制代码

2:实例是如何启动的?

image

image

     注:创建数据库是指:以命令的方式创建数据库。

3:数据库是如何装载的?

image

       image

注:数据库恢复的操作有:热备恢复、冷备恢复

4:数据库是如何打开的?

image

 

 

 

5:数据文件检查

image

 

6:只读模式

   image

image

 

注:切换数据库为只读模式:

image

复制代码
  1 
  2 SYS@orcl> shutdown immediate;
  3 Database closed.
  4 Database dismounted.
  5 ORACLE instance shut down.
  6 SYS@orcl> startup;
  7 ORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instance
  8 ORACLE instance started.
  9 
 10 Total System Global Area 1221992448 bytes
 11 Fixed Size                  1344596 bytes
 12 Variable Size             939527084 bytes
 13 Database Buffers          268435456 bytes
 14 Redo Buffers               12685312 bytes
 15 Database mounted.
 16 Database opened.
 17 SYS@orcl> select open_mode from v$database ;
 18 
 19 OPEN_MODE
 20 --------------------
 21 READ WRITE
 22 
 23 SYS@orcl> shutdown immediate;
 24 Database closed.
 25 Database dismounted.
 26 ORACLE instance shut down.
 27 SYS@orcl> startup open read only;
 28 ORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instance
 29 ORACLE instance started.
 30 
 31 Total System Global Area 1221992448 bytes
 32 Fixed Size                  1344596 bytes
 33 Variable Size             939527084 bytes
 34 Database Buffers          268435456 bytes
 35 Redo Buffers               12685312 bytes
 36 Database mounted.
 37 Database opened.
 38 
 39 SYS@orcl> select open_mode from v$database ;
 40 
 41 OPEN_MODE
 42 --------------------
 43 READ ONLY
 44 
 45 SYS@orcl> shutdown immediate;
 46 Database closed.
 47 Database dismounted.
 48 ORACLE instance shut down.
 49 SYS@orcl> startup mount;
 50 ORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instance
 51 ORACLE instance started.
 52 
 53 Total System Global Area 1221992448 bytes
 54 Fixed Size                  1344596 bytes
 55 Variable Size             939527084 bytes
 56 Database Buffers          268435456 bytes
 57 Redo Buffers               12685312 bytes
 58 Database mounted.
 59 SYS@orcl> alter database open read only;
 60 
 61 Database altered.
 62 
 63 SYS@orcl> select open_mode from v$database ;
 64 
 65 OPEN_MODE
 66 --------------------
 67 READ ONLY
 68 
 69 SYS@orcl>
复制代码

 

 

 

7:限制模式打开数据库(RESTAICT)

image

  image

image

image

image

 

  1 
  2 SYS@orcl> shutdown immediate;
  3 Database closed.
  4 Database dismounted.
  5 ORACLE instance shut down.
  6 SYS@orcl> startup restrict
  7 ORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instance
  8 ORACLE instance started.
  9 
 10 Total System Global Area 1221992448 bytes
 11 Fixed Size                  1344596 bytes
 12 Variable Size             939527084 bytes
 13 Database Buffers          268435456 bytes
 14 Redo Buffers               12685312 bytes
 15 Database mounted.
 16 Database opened.
 17 SYS@orcl> select instance_name,logins from v$instance;
 18 
 19 INSTANCE_NAME    LOGINS
 20 ---------------- ----------
 21 orcl             RESTRICTED
 22 
 23 
 24 SYS@orcl> create user u1 identified by u1;
 25 
 26 User created.
 27 
 28 SYS@orcl> grant create session to u1;
 29 
 30 Grant succeeded.
 31 
 32 SYS@orcl> quit
 33 Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
 34 With the Partitioning, OLAP, Data Mining and Real Application Testing options
 35 [oracle@localhost dbs]$ sqlplus u1/u1
 36 
 37 SQL*Plus: Release 11.2.0.3.0 Production on Wed Apr 18 23:06:47 2018
 38 
 39 Copyright (c) 1982, 2011, Oracle.  All rights reserved.
 40 
 41 ERROR:
 42 ORA-01035: ORACLE only available to users with RESTRICTED SESSION privilege
 43 
 44 
 45 Enter user-name:
 46 ERROR:
 47 ORA-01017: invalid username/password; logon denied
 48 
 49 
 50 Enter user-name:
 51 ERROR:
 52 ORA-01017: invalid username/password; logon denied
 53 
 54 
 55 SP2-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus
 56 [oracle@localhost dbs]$ sqlplus / as sysdba;
 57 
 58 SQL*Plus: Release 11.2.0.3.0 Production on Wed Apr 18 23:07:40 2018
 59 
 60 Copyright (c) 1982, 2011, Oracle.  All rights reserved.
 61 
 62 
 63 Connected to:
 64 Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
 65 With the Partitioning, OLAP, Data Mining and Real Application Testing options
 66 
 67 
 68 SYS@orcl> grant restricted session to u1;
 69 
 70 Grant succeeded.
 71 
 72 SYS@orcl> quit
 73 Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
 74 With the Partitioning, OLAP, Data Mining and Real Application Testing options
 75 [oracle@localhost dbs]$ sqlplus u1/u1;
 76 
 77 SQL*Plus: Release 11.2.0.3.0 Production on Wed Apr 18 23:08:49 2018
 78 
 79 Copyright (c) 1982, 2011, Oracle.  All rights reserved.
 80 
 81 
 82 Connected to:
 83 Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
 84 With the Partitioning, OLAP, Data Mining and Real Application Testing options
 85 
 86 U1@orcl>



  1 [oracle@localhost dbs]$ sqlplus / as sysdba;
  2 
  3 SQL*Plus: Release 11.2.0.3.0 Production on Wed Apr 18 23:13:27 2018
  4 
  5 Copyright (c) 1982, 2011, Oracle.  All rights reserved.
  6 
  7 
  8 Connected to:
  9 Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
 10 With the Partitioning, OLAP, Data Mining and Real Application Testing options
 11 
 12 --关闭数据库 
 13 SYS@orcl> shutdown immediate;
 14 Database closed.
 15 Database dismounted.
 16 ORACLE instance shut down.
 17 -- 正常启动数据库
 18 SYS@orcl> startup
 19 
 20 ORACLE instance started.
 21 
 22 Total System Global Area 1221992448 bytes
 23 Fixed Size                  1344596 bytes
 24 Variable Size             939527084 bytes
 25 Database Buffers          268435456 bytes
 26 Redo Buffers               12685312 bytes
 27 Database mounted.
 28 Database opened.
 29 ---查看数据库登录状态:是否处于受限制状态
 30 SYS@orcl> select instance_name,logins from v$instance;
 31 
 32 INSTANCE_NAME    LOGINS
 33 ---------------- ----------
 34 orcl             ALLOWED --(非受限制状态)
 35 
 36 ---解除 登录受限制状态
 37 SYS@orcl> alter system  disable restricted session;
 38 
 39 System altered.
 40  ---查看数据库登录状态:是否处于受限制状态
 41 SYS@orcl> select instance_name,logins from v$instance;
 42 
 43 INSTANCE_NAME    LOGINS
 44 ---------------- ----------
 45 orcl             ALLOWED  --(非受限制状态)
 46 --给所有数据库用户 开启数据库登录受限制状态
 47 SYS@orcl> alter system enable restricted session;
 48 
 49 System altered.
 50  ---查看数据库登录状态:是否处于受限制状态
 51 SYS@orcl> select instance_name,logins from v$instance;
 52 
 53 INSTANCE_NAME    LOGINS
 54 ---------------- ----------
 55 orcl             RESTRICTED --( 受限制状态)
 56 ---解除 登录受限制状态
 57 SYS@orcl> alter system disable restricted session;
 58 
 59 System altered.
 60  ---查看数据库登录状态:是否处于受限制状态
 61 SYS@orcl> select instance_name ,logins from v$instance;
 62 
 63 INSTANCE_NAME    LOGINS
 64 ---------------- ----------
 65 orcl             ALLOWED  --(非受限制状态)
 66 
 67 SYS@orcl>



8:数据库和实例关闭概述

image

image

image

     image

image

                 shutdown abort

image

                 shutdown immediate

image

                 shutdown normal

image

 

 

9:数据库是如何关闭的

image

imageimage

 

10:数据库是如何卸载的

image

 

11:数据库实例是如何关闭的

image

 

 

 

三:检查点概述

 image

 

注:1:实例是如何恢复的:

  • SCN :系统改变的一个number号。可以把它理解一个时间戳。该值只会增大不会减少。
  • 可以使用SCN 那个时刻点的数据值。
  • redo 流:即很多个 redo 记录。
  • redo数据是如何产生的:例如 把数据值1 update 为 2 ,其过程是: 首先是客户端链接服务器,启动服务器进程。然后 客户端给服务器发送了一个 把 数值 1 update 2 的操作,此时 服务器进程则会在 database buffer cache(数据库缓冲区)查 值为1的数据,若有数值 1,则会在 数据库缓冲区中把数值 1改为2(此时的数值2 称作 脏数据),若此时数据库缓冲区没有这个数值1 ,则此时服务器进程会读取 数据文件里的数值1,此时会把数值1 从数据库数据文件读取到数据库缓冲区中,然后在把数值1 改为2 。数据库的Update的操作会有2个事务性操作:commit、rollback.如果使用 commit,此时数值1则 永久的被改为数值2了。如果执行了rollback,则 把已经修改的数值2 还原为修改前的数值1.那么,这个操作就涉及到了一个 Redo Log Buffer(Redo 日志缓冲区)。具体为:当在数据库缓冲区里把数值1改为数值2的时候,此时数据库服务器进程会向 redo日志缓冲区申请一个 redo数据块,然后在把数值1写入到刚申请的redo数据块中,然后就是再把 数据库缓冲区里面的数值1修改为数值2.同时也把数值1修改为数值2的这个变化过程也写入到了redo日志缓冲区中。要注意的是:此时数据文件了的数值1的值还是1.而数据库缓冲区里原本为数值1的值此时值为2了。如果此时执行了commit操作,则会把reod日志缓冲区的数据记录通过后台进程LGWR写入到 在线的redo日志文件中。
  • redo流就是很多 reod数据像小河流水一样的产生了redo数据流。
  •  
  • 检查点为:日志文件、数据文件、控制文件等三个文件的数据信息(SCN)一致的时刻点。

  • “当从检查点位置的SCN处开始使用redo流进行实例恢复”:从非检查点一直的SCN号向前最前一次的检查点时刻SCN号处进行数据库的数据文件、日志文件、控制文件等信息通过redo流实现实例恢复还原。
  • 如何保证(日志文件、数据文件、控制文件)的检查点的一致性:则需要把数据库缓冲区里的数据写入到数据文件中。则需要调用后台进程,进行I/O的磁盘写入操作。当检查点执行很频繁的时候,那么会有很多后台进程在哪里频繁的执行数据I/O的写入操作。此时数据库的性能会有所下降。所以检查点没有必要过于频繁的执行。
  • 所以检查点的间隔太长,在进行实例恢复所需要的时间也要长些。
  1 
  2 ---查看数据库当前的SCN号值
  3 SYS@orcl> select current_scn from v$database;
  4 
  5 CURRENT_SCN
  6 -----------
  7     5723851
  8 
  9 SYS@orcl>
 10 


  1 
  2 ----SCN 随着时间的增大而增大 
  3 SYS@orcl> select current_scn from v$database;
  4 
  5 CURRENT_SCN
  6 -----------
  7     5724200
  8 
  9 SYS@orcl> select current_scn from v$database;
 10 
 11 CURRENT_SCN
 12 -----------
 13     5724201
 14 
 15 SYS@orcl> select current_scn from v$database;
 16 
 17 CURRENT_SCN
 18 -----------
 19     5724203
 20 
 21 SYS@orcl> select current_scn from v$database;
 22 
 23 CURRENT_SCN
 24 -----------
 25     5724204
 26 
 27 SYS@orcl>



1:检查点的目的

image

 

 

2:Oracle 数据库何时启动检查点

image

     1:线程检查点

image

  1 
  2 
  3 ---手动 执行 检查点 一致性
  4 SYS@orcl> alter system checkpoint;
  5 
  6 System altered.
  7 ---联机重做日志切换
  8 SYS@orcl> alter system switch logfile;
  9 
 10 System altered.
 11 
 12 SYS@orcl>
 13 


  1 
  2 ---在归档模式下:开启数据库的热备份
  3 SYS@orcl> alter database begin backup;
  4 
  5 Database altered.
  6 ---查看数据库是否开启了归档模式
  7 SYS@orcl> archive log list;
  8 Database log mode              Archive Mode
  9 Automatic archival             Enabled
 10 Archive destination            /home/oracle/oracle_system_files_back/archivelog/archivelog_20180305/archive2
 11 Oldest online log sequence     266
 12 Next log sequence to archive   269
 13 Current log sequence           269
 14 SYS@orcl>



     2:表空间和数据文件的检查点

image

 

     3:增量检查点

image


  1 ---查看日志文件信息
  2 SYS@orcl> select member from v$logfile;
  3 
  4 MEMBER
  5 --------------------------------------------------------------------------------
  6 /u01/app/oracle/oradata/orcl/redo03.log
  7 /u01/app/oracle/oradata/orcl/redo02.log
  8 /u01/app/oracle/oradata/orcl/redo01.log
  9 /home/oracle/oracle_system_files_back/logfile_bak/logfile_bak_20180228/redo20180228.log
 10 /home/oracle/oracle_system_files_back/logfile_bak/logfile_bak_20180228/redo20180228_1.log
 11 /home/oracle/oracle_system_files_back/logfile_bak/logfile_bak_20180228/redo04.log
 12 
 13 6 rows selected.
 14 
 15 SYS@orcl>

日志切换:是第一个日志文件使用完了,就切换使用第二个日志文件,如此循环使用。

日志切换时:后台进程DBWn则会把数据库缓冲区中的数据信息写入到数据文件中。

回到顶部

四:实例恢复概述

 

image

 

1:实例恢复的目的

image        image

 

2: Oracle 数据库何时执行实例恢复

image

 

 

3:实例恢复检查点的重要性

image

            image

image

 

4:实例恢复阶段

image

image

 

 

 

 

 

 

 

 

 

 

 

 

——————————————————————————————————————————————————————————————————————

为人:谦逊、激情、博学、审问、慎思、明辨、 笃行 
学问:纸上得来终觉浅,绝知此事要躬行 
为事:工欲善其事,必先利其器。 
转载请标注出处!
原文地址:https://www.cnblogs.com/ios9/p/8878210.html