1、概念

归档是将联机重做日志以文件的形式保存到硬盘,联机日志归档的前提条件是数据库要处于归档模式。当数据库处于 ARCHIVELOG 模式并进行日志切换时,后台进程 ARCH 会将联机重做日志的内容保存到归档日志中,当数据库出现介质故障时,使用数据文件备份、归档日志和联机重做日志可以完全恢复数据库到正常状态。

Oracle 数据库设置为归档模式之后,如果归档空间写满,数据库的 redo 文件不能归档,会出现数据库挂起的问题,导致 数据库无法使用。

2、查看数据库是否为归档模式

SQL> archive log list;Database log mode Archive ModeAutomatic archival EnabledArchive destination /app/archiveOldest online log sequence 30Next log sequence to archive 34Current log sequence 34SQL> select log_mode from v$database;LOG_MODE————————————ARCHIVELOG

3、打开或关闭归档模式

如果需要打开或关闭归档模式,在数据库处于 mount 状态时使用如下命令:

3.1 打开归档模式

alter database archivelog;

3.2 关闭归档模式

alter database noarchivelog;

4、配置归档的位置

当数据库处于归档模式时,进行日志切换时后台进程将自动生成归档日志。

4.1 查看归档进程数

初始化参数 LOG_ARCHIVE_MAX_PROCESSES 用于指定最大归档进程个数。通过改变该初始化参数的取值,可以动态地增加或减少归档进程的个数。

SQL> show parameter LOG_ARCHIVE_MAX_PROCESSES;NAME TYPEVALUE------------------------------------ ----------- ------------------------------log_archive_max_processesinteger 4

4.2 配置归档的位置

可以使用 LOG_ARCHIVE_DEST_n 配置多个归档位置:该参数最多可以指定 10 个归档位置。格式如下:

ALTER SYSTEM SET log_archive_dest_1 = 'location = 路径1';ALTER SYSTEM SET log_archive_dest_2 = 'location = 路径2';.....

4.2.1 查看归档文件

SQL> select sequence#, name from v$archived_log;SEQUENCE#NAME---------- --------------------------------------------------------------------------------34 /app/archive/orcl_62725153_0000000034_1_1125947150.dbf

可知归档的文件位置 /app/archive/

4.2.2 修改归档日志的位置为:/home/oracle/archivelog

SQL> alter system set log_archive_dest_1 = 'location=/app/archive' scope = both sid='*';SQL> archive log list;SQL> alter system switch logfile;SQL> select sequence#, name from v$archived_log;SQL> select sequence#, name from v$archived_log;SEQUENCE#NAME---------- --------------------------------------------------------------------------------34 /app/archive/orcl_62725153_0000000034_1_1125947150.dbf35 /app/archive/orcl_62725153_0000000035_1_1125947150.dbf

5、配置归档日志文件的名称格式

初始化参数 LOG_ARCHIVE_FORMAT 用于指定归档日志文件的名称格式。设置该初始化参数时,可以指定以下匹配符:

(1)%s:日志序列号;

(2)%S:日志序列号(带有前导 0);

(3)%t:线程编号;

(4)%T:线程编号(带有前导 0);

(5)%a:活动 ID 号;

(6)%d:数据库 ID 号;

(7)%r:RESETLOGS的ID值。

归档日志的文件名称格式设置如下:

SQL> alter system set LOG_ARCHIVE_FORMAT = 'orcl_%d_%S_%t_%r.dbf' scope = spfile sid='*';

重启数据库使设置生效

SQL> shutdown immediateSQL> startup

查看参数 LOG_ARCHIVE_FORMAT 的值:

SQL> show parameter LOG_ARCHIVE_FORMATNAME TYPEVALUE------------------------------------ ----------- ------------------------------log_archive_format stringorcl_%d_%S_%t_%r.dbf

查看归档日志:

SQL> select sequence#, name from v$archived_log;

6、删除归档日志

6.1 手工删除归档日志文件

[oracle@node1 archive]$ cd /app/archive/[oracle@node1 archive]$ ll -r总用量 4516-rw-r----- 1 oracle oinstall 10752 2月 2 10:54 orcl_62725153_0000000036_1_1125947150.dbf-rw-r----- 1 oracle oinstall 54784 2月 2 10:51 orcl_62725153_0000000035_1_1125947150.dbf-rw-r----- 1 oracle oinstall 4551680 2月 2 10:46 orcl_62725153_0000000034_1_1125947150.dbf[oracle@node1 archive]$ rm -rf orcl_62725153_0000000034_1_1125947150.dbf[oracle@node1 archive]$ ll总用量 68-rw-r----- 1 oracle oinstall 54784 2月 2 10:51 orcl_62725153_0000000035_1_1125947150.dbf-rw-r----- 1 oracle oinstall 10752 2月 2 10:54 orcl_62725153_0000000036_1_1125947150.dbf

查看归档日志:

SQL> select sequence#, name from v$archived_log;SEQUENCE#NAME---------- --------------------------------------------------------------------------------34 /app/archive/orcl_62725153_0000000034_1_1125947150.dbf35 /app/archive/orcl_62725153_0000000035_1_1125947150.dbf36 /app/archive/orcl_62725153_0000000036_1_1125947150.dbf

虽然物理文件已经删除,但是归档日志文件的信息还在。

6.2 用 RMAN 删除数据库记录的归档列表信息

[oracle@node1 ~]$ rman target /RMAN> list archivelog all;List of Archived Log Copies for database with db_unique_name ORCL=====================================================================Key Thrd Seq S Low Time ------- ---- ------- - ---------36134X 02-FEB-23Name: /app/archive/orcl_62725153_0000000034_1_1125947150.dbf37135A 02-FEB-23Name: /app/archive/orcl_62725153_0000000035_1_1125947150.dbf38136A 02-FEB-23Name: /app/archive/orcl_62725153_0000000036_1_1125947150.dbfRMAN> crosscheck archivelog all;released channel: ORA_DISK_1allocated channel: ORA_DISK_1channel ORA_DISK_1: SID=1996 device type=DISKvalidation failed for archived logarchived log file name=/app/archive/orcl_62725153_0000000034_1_1125947150.dbf RECID=36 STAMP=1127731566validation succeeded for archived logarchived log file name=/app/archive/orcl_62725153_0000000035_1_1125947150.dbf RECID=37 STAMP=1127731881validation succeeded for archived logarchived log file name=/app/archive/orcl_62725153_0000000036_1_1125947150.dbf RECID=38 STAMP=1127732067Crosschecked 3 objectsRMAN> delete expired archivelog all;released channel: ORA_DISK_1allocated channel: ORA_DISK_1channel ORA_DISK_1: SID=1996 device type=DISKList of Archived Log Copies for database with db_unique_name ORCL=====================================================================Key Thrd Seq S Low Time ------- ---- ------- - ---------36134X 02-FEB-23Name: /app/archive/orcl_62725153_0000000034_1_1125947150.dbfDo you really want to delete the above objects (enter YES or NO)? yesdeleted archived logarchived log file name=/app/archive/orcl_62725153_0000000034_1_1125947150.dbf RECID=36 STAMP=1127731566Deleted 1 EXPIRED objectsRMAN> select sequence#, name from v$archived_log;select sequence#, name from v$archived_log;SEQUENCE#NAME---------- --------------------------------------------------------------------------------34 35 /app/archive/orcl_62725153_0000000035_1_1125947150.dbf36 /app/archive/orcl_62725153_0000000036_1_1125947150.dbf

6.3 清除 v$archived_log 视图中的过期信息

使用 RMAN 命令删除归档后,v$archived_log 视图中的 name 列为空,但其他列的信息仍然存在。出现这种现象的原因是因为使用 RMAN 命令在删除归档日志的时候不能够清除控制文件中的内容。

6.3.1 清除控制文件中关于 v$archived_log 的信息

SQL> execute sys.dbms_backup_restore.resetCfileSection(11);PL/SQL procedure successfully completed.

6.3.2 查询发现 v$archived_log 视图中的信息全部被清除了

SQL> select sequence#, name from v$archived_log; SEQUENCE# NAME---------- --------------------------------------------------------------------------------

6.3.3 将未过期的归档文件信息重新注册到控制文件中

RMAN> catalog start with '/app/archive/';using target database control file instead of recovery catalogsearching for all files that match the pattern /app/archive/List of Files Unknown to the Database=====================================File Name: /app/archive/orcl_62725153_0000000035_1_1125947150.dbfFile Name: /app/archive/orcl_62725153_0000000036_1_1125947150.dbfDo you really want to catalog the above files (enter YES or NO)? yescataloging files...cataloging doneList of Cataloged Files=======================File Name: /app/archive/orcl_62725153_0000000035_1_1125947150.dbfFile Name: /app/archive/orcl_62725153_0000000036_1_1125947150.dbfRMAN> select sequence#, name from v$archived_log; SEQUENCE# NAME---------- --------------------------------------------------------------------------------35 /app/archive/orcl_62725153_0000000035_1_1125947150.dbf36 /app/archive/orcl_62725153_0000000036_1_1125947150.dbf