Oracle 11g DataGuard 配置

11/30/2009来源:Oracle教程人气:15240

Oracle 11g DataGuard 配置

Oracle Database 11g EnterPRise Edition Release 11.1.0.6.0 - 64bit Production
Red Hat Enterprise linux Server release 5.3 (Tikanga)

一、测试环境准备
1.主库和备库oracle版本一致,运行在相同平台如linux,服务器硬件可以不一样。
2.Primary 数据库必须运行于归档模式,并且务必确保在primary 数据库上打开FORCE LOGGING,以避免用户通过nologging 等方式避免写redo 造成对应的操作无法传输到standby 数据库。
3.Primary 和standby 数据库均可应用于单实例或RAC 架构下,并且同一个data guard 配置可以混合使用逻辑standby 和物理standby.
4.建议数据库必须采用相同的存储架构。比如存储采用ASM/OMF 的话,那不分primarty 或是standby也都需要采用ASM/OMF。
5.standby库的环境和primary一致。

二、调整主库
1.将主数据库改为强制产生日志模式
alter database force logging;
2. 创建密码
orapwd file=$ORACLE_HOME/dbs/orapwtest passWord=test entries=3
3. 修改主库的初始化参数
db_unique_name ='test01'
db_name指定数据库的名称,db_unique_name指定了数据库(主库和备库)的不同名称,是对db_name的唯一的标识。其实是为了区分db_name名一样的数据库,方便应用和管理。是10g以后的参数。
log_archive_config='DG_CONFIG=(test01,test02)'
test01,test02是.db_unique_name
log_archive_dest_1='location=/u01/app/oracle/arch/test/ VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=test01'
log_archive_dest_2='SERVICE=test_db_02 LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=test02'
确定log_archive_dest_state_1
log_archive_dest_state_1= enable #默认就是enable
log_archive_dest_state_2= enable
/*以下部分为主机切换为备库使用*/
fal_server=test_db_02
fal_client=test_db_01
standby_file_management=auto
#如果主备库文件或路径不同
db_file_name_convert='/u01/app/oracle/oradata/test','/u01/app/oracle/oradata/test'
log_file_name_convert='/u01/app/oracle/oradata/test','/u01/app/oracle/oradata/test'

建议参数文件使用spfile,这样可以在不停库使用命令修改参数。
4. 确保数据库运行在archivelog模式下
SQL> archive log list      
Database log mode           No Archive Mode
Automatic archival           Disabled
Archive destination           USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence     2
Current log sequence           4
把数据库调整为archivelog模式下
SQL> shutdown immediate
SQL> startup mount
SQL> alter database archivelog ;
SQL> alter database open;

5.在主库上创建备用日志 (为切换而用)
alter database recover managed standby database cancel;#未执行
SQL>ALTER DATABASE ADD STANDBY LOGFILE GROUP 4 SIZE 50M;
SQL>ALTER DATABASE ADD STANDBY LOGFILE GROUP 5 SIZE 50M;
SQL>ALTER DATABASE ADD STANDBY LOGFILE GROUP 6 SIZE 50M;

mkdir -p /u01/app/oracle/standby/test
SQL>alter database add standby logfile group 4('/u01/app/oracle/standby/test/stdby_redo04.log') size 50M;
SQL>alter database add standby logfile group 5('/u01/app/oracle/standby/test/stdby_redo05.log') size 50M;
SQL>alter database add standby logfile group 6('/u01/app/oracle/standby/test/stdby_redo06.log') size 50M;

SQL>alter database recover managed standby database disconnect from session;#未执行

三、创建standby database
1. 在standby database 创建pwdfile
orapwd file=$ORACLE_HOME/dbs/orapwtest password=test entries=3
密码要与primary database相同
2. 修改standby database初始化参数
与primary database不同的参数如下:
db_unique_name=test02
log_archive_dest_1='location=/u01/app/oracle/arch/test valid_for=(all_logfiles,all_roles) db_unique_name=test02'
log_archive_dest_2='service=test_db_01 ARCH ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) db_unique_name=test01'
fal_client='test_db_02'
fal_server='test_db_01'
db_file_name_convert='/u01/app/oracle/oradata/TEST01/datafile','/u01/app/oracle/oradata/TEST02/datafile'
log_file_name_convert='/u01/app/oracle/oradata/TEST01/onlinelog','/u01/app/oracle/oradata/TEST02/onlinelog', '/u01/app/oracle/Flash_recovery_area/TEST01/onlinelog','/u01/app/oracle/flash_recovery_area/TEST01/onlinelog'  

修改完之后用如下语句查看
SQL> select * from V$DATAGUARD_CONFIG;

DB_UNIQUE_NAME
------------------------------------------------------------------------------------------
test01
test02

4.创建好相应目录,dump文件目录,数据文件目录,归档目录
mkdir -p /u01/app/oracle/oradata
mkdir -p /u01/app/oracle/flash_recovery_area
mkdir -p /u01/app/oracle/arch/test

5. 配置standby database的listener.ora 和tnsnames.ora文件同主库
重起监听并检验是否可以tnsping通
6. 启动standby database到nomount状态
SQL> startup nomount
7. 用RMAN的duplicate功能创建standby database,11g可以不做备份直接复制到备库:
RMAN> rman auxiliary /
connect target sys/test@test_db_01
duplicate target database for standby dorecover nofilenamecheck;

检查standby database的状态
SQL> select database_role,protection_mode,protection_level from v$database;
SQL> select sequence# ,applied from v$archived_log order by sequence#;
8.开始Redo应用
主库
SQL> alter system archive log current;
备库,11g可以只读打开库应用日志
SQL> alter database open read only;
SQL> alter database recover managed standby database disconnect from session;

也可以使用alter database recover managed standby database using current logfile disconnect from session;应用日志。
没有using current logfile是去apply standby archived log.
有using current logfile是当日志传到standby redo log的时候就去应用,是real-time的,必须要有standby redo log.


四.备用服务器的管理模式与只读模式
1.启动到管理模式
SQL>shutdown immediate;
SQL>startup nomount;
SQL>alter database mount standby database;
SQL>alter database recover managed standby database disconnect from session;
2.启动到只读方式
SQL>shutdown immediate;
SQL>startup nomount;
SQL>alter database mount standby database;
SQL>alter database open read only;
(3)如果在管理恢复模式下到只读模式
SQL> recover managed standby database cancel;
SQL> alter database open read only;
这个时候,可以给数据库增加临时数据文件(这个在热备份的时候是没有备份过来的)

alter tablespace temp add tempfile '/u02/oradata/test/temp01.dbf' size 100M;
5.从只读方式到管理恢复方式
SQL> recover managed standby database disconnect from session;

五.主备切换
主库
lsnrctl stop

SQL> alter database commit to switchover to physical standby with session shutdown;
SQL> shutdown immediate;
SQL> startup mount;
SQL> alter database recover managed standby database disconnect;

lsnrctl start

备库

SQL> alter database commit to switchover to primary;
SQL> shutdown immediate;
SQL> startup

六.附件
以下是配置文件内容:
listener.ora

LISTENER =
  (DESCRipTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = oracle6)(PORT = 1521))
  )

tnsnames.ora

TEST_DB_02 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.202.5)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = test02)
    )
  )

TEST_DB_01 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.202.6)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = test01)
    )
  )


pfile

test.__db_cache_size=1526726656
test.__java_pool_size=16777216
test.__large_pool_size=16777216
test.__oracle_base='/u01/app/oracle'#ORACLE_BASE set from environment
test.__pga_aggregate_target=1342177280
test.__sga_target=2013265920
test.__shared_io_pool_size=0
test.__shared_pool_size=436207616
test.__streams_pool_size=0
*.audit_file_dest='/u01/app/oracle/admin/test/adump'
*.audit_trail='db'
*.compatible='11.1.0.0.0'
*.control_files='/u01/app/oracle/oradata/test/control01.ctl','/u01/app/oracle/oradata/test/control02.ctl','/u01/app/oracle/oradata/test/control03.ctl'
*.db_block_size=8192
*.db_domain='greatsky.com'
*.db_name='test'
*.db_recovery_file_dest='/u01/app/oracle/flash_recovery_area'
*.db_recovery_file_dest_size=2147483648
*.db_unique_name='test01'
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=testXDB)'
*.fal_client='TEST_DB_01'
*.fal_server='TEST_DB_02'
*.log_archive_config='dg_config=(test01,test02)'
*.log_archive_dest_1='location=/u01/app/oracle/arch/test/ VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=test01'
*.log_archive_dest_2='SERVICE=test_db_02 LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=test02'
*.memory_target=3347054592
*.open_cursors=300
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.service_names='test.greatsky.com','test01.greatsky.com'
*.standby_file_management='AUTO'
*.undo_tablespace='UNDOTBS1'