Oracle单机搭建dataguard的过程以及需要注意的重要的点

环境    系统:Redhat 6.5    数据库版本:Oracle 11.2.0.4
源端(主库)    db_name:YHD    db_unique_name:YHDZ
目标端(备库)     db_name:YHD(必须和主库一样)    db_unique_name:YHDC
注意:
目标端的数据存放的路径,日志存放路径,$ORACLE_HOME,$ORACLE_BASE等环境,最好和源端一样
需要关闭防火墙和修改/etc/selinux/config文件的参数SELINUX=disabled

源端:
1,开启归档

2,开启force_logging

SQL> alter database force logging;

SQL> select force_logging from v$database;

3,源端和目标端修改/etc/hosts文件  添加自己和对方的IP和主机名字

[root@ora1 ~]# cat /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.56.51  ora1

192.168.56.53  ora3


4,源端和目标端修改监听文件(以目标端为例)

vim $ORACLE_HOME/network/admin/listener.ora

LISTENER =

  (DESCRIPTION_LIST =

    (DESCRIPTION =

      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))

      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.56.53)(PORT = 1521))

    )

  )

ADR_BASE_LISTENER = /u01/app/oracle

 

SID_LIST_LISTENER =

  (SID_LIST =

    (SID_DESC =

      (GLOBAL_DBNAME = YHD)

      (ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1)

      (SID_NAME = YHD)

    )

  )


重载监听
lsnrctl reload

5,源端和目标端修改tnsname.ora(两端要一样)

vim $ORACLE_HOME/network/admin/ tnsnames.ora

YHDZ=                                                 

  (DESCRIPTION =

    (ADDRESS_LIST =

      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.56.51)(PORT = 1521))

    )

    (CONNECT_DATA =

      (SID = YHD)

    )

  )

 

YHDC=

  (DESCRIPTION =

    (ADDRESS_LIST =

      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.56.53)(PORT = 1521))

    )

    (CONNECT_DATA =

      (SID = YHD)

    )

  )

 
之后测试,看是否可以互相使用tnsping通对方
主库
[oracle@ora1 ~]#tnsping ora3
备库
[oracle@ora3 ~]#tnsping ora1

注释:YHDZYHDC这两个名字很重要,不要搞混,后面的参数会用到


6,源端设置参数LOG_ARCHIVE_CONFIG

SQL> alter system set LOG_ARCHIVE_CONFIG='DG_CONFIG=(YHDZ,YHDC)';

SQL> show parameters LOG_ARCHIVE_CONFIG

注释:DG_CONFIG=(YHDZ,YHDC),意思是DG_CONFIG=(主库唯一名,备库唯一名)DG_CONFIG=(主库db_unique_name,备库db_unique_name)。


7,查看源端库的LOG_ARCHIVE_DEST_1,LOG_ARCHIVE_DEST_2,并修改

SQL> show parameters LOG_ARCHIVE_DEST_1

SQL> show parameters LOG_ARCHIVE_DEST_2

SQL> alter system set LOG_ARCHIVE_DEST_1='LOCATION=/log valid_for=(all_logfiles,all_roles) DB_UNIQUE_NAME=YHDZ';

SQL> alter system set LOG_ARCHIVE_DEST_2='SERVICE=YHDC ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=YHDC';

注释:LOG_ARCHIVE_DEST_2='SERVICE=YHDC,这个service=TNS中的备库的tns名字


8,确认log_archive_dest_state_1参数和log_archive_dest_state_2参数为ENABLE状态

show parameters LOG_ARCHIVE_DEST_STATE

alter system set log_archive_dest_state_2=ENABLE;


9,修改参数LOG_ARCHIVE_MAX_PROCESSES
SQL> show parameters LOG_ARCHIVE_MAX_PROCESSES

SQL> alter system set  LOG_ARCHIVE_MAX_PROCESSES=5;


10,确认REMOTE_LOGIN_PASSWORDFILE参数为"EXCLUSIVE"

SQL> show parameters REMOTE_LOGIN_PASSWORDFILE


NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

remote_login_passwordfile            string      EXCLUSIVE


11,设置switch over参数FAL_SERVER,FAL_CLIENT
注意:主库的服务端是备库,客户端是主库;备库的服务端是主库,客户端是备库;不要混淆。

SQL> alter system set FAL_SERVER=YHDC;

SQL> alter system set FAL_CLIENT=YHDZ;

SQL> show parameters FAL_SERVER

SQL> show parameters FAL_CLIENT


12,设置备用文件管理方式参数
SQL> show parameters STANDBY_FILE_MANAGEM

SQL> alter system set STANDBY_FILE_MANAGEMENT=AUTO;


13,重启数据库使参数生效

Startup force


14,主库全备,然后通过scp传输到备库

$ rman target / nocatalog

RMAN>  run{

allocate channel c1 device type disk;

backup database format '/backup/full_%U';

sql 'alter system archive log current';

sql 'alter system archive log current';

sql 'alter system archive log current';

backup archivelog all format '/backup/arch_%U ';

release channel c1;

}

备份当前控制文件为standby模式

RMAN> backup current controlfile for standby format '/backup/controlfile_%U';

备份参数文件

RMAN> backup spfile format '/backup/spfile_%U ';
传输备份文件到目标端

scp -r /backup/*   [email protected]:/home/oracle/backup


15,在备库上恢复

RMAN控制台中启动数据库到nomount阶段:

$ rman target / nocatalog

Recovery Manager: Release 10.2.0.4.0 - Production on Tue May 12 09:13:39 2015

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

connected to target database (not started)

RMAN> startup mount;

-----------

RMAN-03002: failure of startup command at 05/12/2015 09:13:48

ORA-00205: error in identifying control file, check alert log for more info

RMAN> exit

Recovery Manager complete.


$ rman target / nocatalog

Recovery Manager: Release 10.2.0.4.0 - Production on Tue May 12 09:14:17 2015

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

connected to target database: DUMMY (not mounted)

using target database control file instead of recovery catalog

RMAN>

恢复spfile参数文件:

RMAN> restore spfile to '/home/oracle/yhdc.ora' from '/home/oracle/backup/spfile_05s7m4dj_1_1';

SQL*PLUS中通过恢复的spfile参数文件创建pfile初始化参数文件

$ sqlplus / as sysdba

SQL> create pfile='/home/oracle/init.ora' from spfile='/home/oracle/yhdc.ora';

关闭数据库实例

SQL> shutdown immediate

编辑pfile初始化参数文件,重点修改的地方标颜色

$ vi /home/oracle/init.ora
YHD.__db_cache_size=671088640
YHD.__java_pool_size=4194304
YHD.__large_pool_size=8388608
YHD.__oracle_base='/u01/app/oracle'#ORACLE_BASE set from environment
YHD.__pga_aggregate_target=104857600
YHD.__sga_target=985661440
YHD.__shared_io_pool_size=0
YHD.__shared_pool_size=289406976
YHD.__streams_pool_size=0
*.audit_file_dest='/u01/app/oracle/admin/YHD/adump'
*.audit_trail='db'
*.compatible='11.2.0.4.0'
*.control_files='/data/YHD/control01.ctl','/data/YHD/control02.ctl'
*.db_block_size=8192
*.db_create_online_log_dest_1='/log'
*.db_domain=''
*.db_name='YHD'
*.db_unique_name='YHDC'
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=YHDXDB)'
*.fal_client='YHDC'
*.fal_server='YHDZ'
*.log_archive_config='DG_CONFIG=(YHDC,YHDZ)'
*.log_archive_dest_1='LOCATION=/log valid_for=(all_logfiles,all_roles) DB_UNIQUE_NAME=YHDC'
*.log_archive_dest_2='SERVICE=YHDZ ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=YHDZ'
*.log_archive_format='%t_%s_%r.dbf'
*.log_archive_max_processes=5
*.open_cursors=300
*.pga_aggregate_target=104857600
*.processes=1500
*.remote_login_passwordfile='EXCLUSIVE'
*.sessions=1655
*.sga_target=985661440
*.standby_file_management='AUTO'
*.undo_tablespace='UNDOTBS1'


根据参数文件,创建一些没有的目录(参数文件中有的文件都要创建,否则startup nomount的时候会报错)

创建spfile动态初始化参数

SQL> create spfile from pfile='/home/oracle/init.ora';

启动数据库到mount状态

SQL> startup nomount

Rman恢复

$ rman target / nocatalog

先恢复控制文件

RMAN> RESTORE STANDBY CONTROLFILE from '/home/oracle/backup/controlfile_04s7m4bh_1_1’

Mount数据库

RMAN> alter database mount;

重载数据库前,先同步元数据,使控制文件元数据和实际存在数据同步。

RMAN> crosscheck backup;

RMAN> crosscheck archivelog all;

RMAN> delete expired backup;

RMAN> delete expired archivelog all;

RMAN> catalog start with '/home/oracle/backup/';

restore数据库

RMAN> restore database;

记录所有线程最后一个归档日志信息

RMAN> list backup of archivelog all;

recover数据库,恢复到最后一个归档日志Next SCN较小的值为恢复结束点恢复:

RMAN> recover database until scn 5614038512;

注释:如果恢复失败,就从主库再次拷贝日志文件到备库(一般我很少一次成功过)

再一次restorerecover,recover的时候也是要使用until scn恢复


16,主库生成密码文件,并且传输到备库/u01/app/oracle/product/11.2.0/db_1/dbs位置下(很重要,一定要一模一样的),如果主库没有运行可以重新生成

[oracle@ora1 dbs]$ pwd

/u01/app/oracle/product/11.2.0/db_1/dbs

[oracle@ora1 dbs]$ orapwd file=$ORACLE_HOME/dbs/orapw$ORACLE_SID password=''entries=n

[oracle@ora1 dbs]$ scp orapwYHD [email protected]:/u01/app/oracle/product/11.2.0/db_1/dbs/

注释:生成规则是  orapwd file=$ORACLE_HOME/dbs/orapw$ORACLE_SID password='' entries=n   


17,备库添加standby redo log

如果主库是单实例数据库:Standby Redo Log组数=主库redo日志组总数+1

如果主库是RAC数据库:Standby Redo Log组数=(所有节点中redo日志组数最大值 + 1) * RAC节点数

确认主库有几组redo

SQL> set linesize 150;

set pagesize 50;

column MB format a5;

column STATUS format a12;

column MEMBER format a50;

select l.GROUP#,l.THREAD#,l.BYTES/1024/1024||'MB' MB,l.STATUS, lf.TYPE,lf.MEMBER from v$log l,v$logfile lf where l.GROUP#=lf.GROUP#;

备库添加standby redo log(大小和主库的redo一样)

SQL> alter database add standby logfile thread 1 group 4 '/data/YHD/srl4' size 256m;
SQL> alter database add standby logfile thread 1 group 5 '/data/YHD/srl5' size 256m;
SQL> alter database add standby logfile thread 1 group 6 '/data/YHD/srl6' size 256m;

备库确认添加的standby

SQL> set linesize 150;

set pagesize 50;

column MB format a5;

column STATUS format a12;

column MEMBER format a50;

select sl.GROUP#,sl.THREAD#,sl.BYTES/1024/1024||'MB' MB,sl.STATUS, lf.TYPE,lf.MEMBER from v$standby_log sl,v$logfile lf where sl.GROUP#=lf.GROUP#;

主库添加stanby redo log

步骤和上面一样


18,备库启用恢复管理

打开数据库

SQL> alter database open;

:如果提示需要介质恢复,说明数据文件恢复不一致,可再次备份主库归档日志到备库继续进行恢复,同样需要指定SCN号进行恢复。

查询备库状态

SQL> set linesize 150;

column DBNAME format a6;

column DBUNAME format a10;

column cftype format a8;

column OPEN_MODE format a20;

column DATABASE_ROLE format a18;

select name dbname,db_unique_name dbuname,controlfile_type cftype,database_role,open_mode from v$database;


DBNAME DBUNAME    CFTYPE   DATABASE_ROLE      OPEN_MODE

------ ---------- -------- ------------------ --------------------

ORCL   ORCLDG     STANDBY  PHYSICAL STANDBY   READ ONLY


注:
11g数据库可做成ADG模式,即数据库以read_only状态打开并实时应用日志;

11g ADG模式:
SQL> startup force;

开启日志应用进程:
SQL>  ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT;

备库查询:

SQL> SELECT RECOVERY_MODE FROM V$ARCHIVE_DEST_STATUS WHERE DEST_ID=1;   

RECOVERY_MODE

---------------------------------------------------------------------

MANAGED REAL TIME APPLYs

19,修改备库文件管理参数为自动
:备库修改参数 standby_file_management AUTO 后,将无法更改备库的任何数据文件属性,若需更改,需切换为 manual

SQL> alter system set standby_file_management=AUTO;

20,查看备库状态
SQL> set linesize 150;
column DBNAME format a6;
column DBUNAME format a10;
column cftype format a8;
column OPEN_MODE format a20;
column DATABASE_ROLE format a18;
select name dbname,db_unique_name dbuname,controlfile_type cftype,open_mode,database_role from v$database;
DBNAME DBUNAME    CFTYPE   OPEN_MODE            DATABASE_ROLE
------ ---------- -------- -------------------- ------------------
ORCL   ORCLDG     STANDBY  MOUNTED              PHYSICAL STANDBY

21,查询备库恢复管理进程情况
SQL> SELECT PROCESS, STATUS FROM V$MANAGED_STANDBY;
PROCESS   STATUS
--------- ------------
ARCH      CONNECTED
ARCH      CONNECTED
ARCH      CONNECTED
ARCH      CONNECTED
ARCH      CONNECTED
MRP0      WAIT_FOR_LOG

22,验证:
主库切redo,查看日志应用情况

SQL> alter system archive log current;
SQL> select sequence#,applied from v$archived_log; 

备库查看是否主从日志应用的一样(一样就没有问题,说明成功了)

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

23,备库查询重做数据传输和应用滞后

SQL> set linesize 150;

set pagesize 20;

column name format a13;

column value format a20;

column unit format a30;

column TIME_COMPUTED format a30;

select name,value,unit,time_computed from v$dataguard_stats where name in ('transport lag','apply lag');


NAME          VALUE                UNIT                           TIME_COMPUTED

------------- -------------------- ------------------------------ ------------------------------

transport lag +00 00:33:39         day(2) to second(0) interval   06/16/2014 21:43:28

apply lag     +00 00:33:40         day(2) to second(0) interval   06/16/2014 21:43:28


其中:
     "transport lag"的值表示主数据库到物理备库的重做数据传输落后时间,这里落后3339秒。如果主数据库发生灾难,就要容忍损失3339秒的数据。
   
 "apply lag"的值表示应用重做数据时间,这里落后3340秒。




来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31447263/viewspace-2141255/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/31447263/viewspace-2141255/

你可能感兴趣的:(Oracle单机搭建dataguard的过程以及需要注意的重要的点)