这个是我Blog的第一个文章,这个已经是我1年半前写好的文档了,当时实施一个项目,从知道DG,到实施完成,很是艰苦,什么都很新鲜,什么都要查资料,现在回头看看,并不是多难的一个事情,这里也是第一次上传,可能写得不是很好,我会不断修改,完善。争取大家只要按照我的一步步做,就一定能成功,大家支持下吧。
最近由于考试需要,学习使用Broken的方法,感觉似乎更方便简单。后面我会在写一篇基于Broken创建Dataguard的文档。
文档分为 主库部分 和 备库部分 讲解设置方法。
主库部分设置
1、执行archive log list; ---确认是归档模式
alter database archivelog; 在mount模式下更改
查当前状态select status from v$instance;
2、select force_logging from v$database; ---允许FORCE LOGGING模式
alter database force logging; ---直接更改即生效
3、创建密钥文件
ORAPWD FILE=/oratest/app/oracle/product/10.2.0/db_1/dbs/ORAPW[SID] password=oracle entries=30
此处注意,是在主库执行,要查询主库具体路径,然后复制到standby对应路径下
如果主库有密码文件,直接拷贝主库的密码文件(在$ORACLE_HOME/dbs下)到备库即可,并把该密码文件根据备库实例名修改为orapwd<sid>
4、设置主库的初始化参数,(首先生成一个pfile文件,做为额外备份)
通过spfile创建pfile
create pfile='/tmp/backupdg/rspfilebk' from spfile;
主库中增加下列参数
#dataguard
*.db_unique_name=primdb
*.log_archive_config='dg_config=(primdb,stddb)'
*.log_archive_dest_1='location=/u01/oradata/QUNYAN/archivelog/ valid_for=(all_logfiles,all_roles) db_unique_name=primdb'
*.log_archive_dest_2= 'service=stddb lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=stddb'
*.log_archive_dest_state_1=enable
*.log_archive_dest_state_2=defer
*.log_archive_max_processes=10
(下面部分是主机的standy部分,以备将来容灾切换到备机时使用)
*.standby_file_management=auto
*.fal_server=stddb
*.fal_client=primdb
*.db_file_name_convert='/u01/oradata/QUNYAN/','/oradata/dg'
*.log_file_name_convert= '/u01/oradata/QUNYAN/','/oradata/dg'
或通过执行命令添加参数
#dataguard
ALTER SYSTEM SET db_unique_name=primdb scope=spfile;
ALTER SYSTEM SET log_archive_config='dg_config=(primdb,stddb)' scope=spfile;
ALTER SYSTEM SET log_archive_dest_1='location=/oratest/app/archivelog/ valid_for=(all_logfiles,all_roles) db_unique_name=primdb' scope=spfile;
ALTER SYSTEM SET log_archive_dest_2= 'service=stddb lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=stddb' scope=spfile;
ALTER SYSTEM SET log_archive_dest_state_1=enable scope=spfile;
ALTER SYSTEM SET log_archive_dest_state_2=defer scope=spfile;
ALTER SYSTEM SET log_archive_max_processes=10 scope=spfile;
(下面部分是主机的standy部分,以备将来容灾切换到备机时使用)
ALTER SYSTEM SET standby_file_management=auto scope=spfile;
ALTER SYSTEM SET fal_server=stddb scope=spfile;
ALTER SYSTEM SET fal_client=primdb scope=spfile;
ALTER SYSTEM SET db_file_name_convert='/oradata/dg','/oratest/app/oracle/admin/allinpay/' scope=spfile;
ALTER SYSTEM SET log_file_name_convert= '/oradata/dg','/oratest/app/archivelog/' scope=spfile;
3、对主库做全备份
backup database format '/sdb/oracle/bk_%U'; --指定备份路径,该指定路径要记住,并在将备份拷贝到备库相应目录下,控制文件会记录备份的路径,如果更改,会找不到备份文件,或者需要更改参数文件convert值
4、创建standy的数据库控制文件(在主库创建,然后拷贝到备库,在主库全备份完成之后创建备库控制文件) -----注意,创建控制文件的路径,在备库也要把创建的standby控制文件放到相应的目录下,不然在RMAN恢复时会报错
alter database create standby controlfile as '/sdb/oracle/standby01.ctl';
在备机端使用cp命令再复杂两个并且命名为standby02.ctl和standby03.ctl
5、修改主库的TNS文件
============================================================
tnsnames.ora配置(需要注意服务名,涉及到日志传输)
primary =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = primary-ip)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = primary )
(SERVER = DEDICATED)
)
)
standby =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = standby-ip )(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = standby)
(SERVER = DEDICATED)
)
)
==================================================================
6、至此主库已经设置完成,需要重新启动才能使参数文件生效,并将产生的备份文件、standby控制文件、密码文件拷贝到备机相应目录下
============================================================================================
备库部分的设置
1、配置监听(配置完成后需要重启监听才能生效)
======================================================================================
# listener.ora Network Configuration File: /oracle/oracle/product/10.2.1/network/admin/listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = /oracle/oracle/product/10.2.1)
(PROGRAM = extproc)
)
(SID_DESC =
(GLOBAL_DBNAME = primary)
(ORACLE_HOME = /oracle/oracle/product/10.2.1)
(SID_NAME = primary)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
(ADDRESS = (PROTOCOL = TCP)(HOST = rac2)(PORT = 1521))
)
)
=========================================================================================
2、tnsnames.ora配置(需要注意服务名,涉及到日志传输)
primary =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = primary-ip)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = primary )
(SERVER = DEDICATED)
)
)
standby =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = standby-ip )(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = standby)
(SERVER = DEDICATED)
)
)
=============================================================================================
3、创建日志输出文件的相关目录(注意登录的用户,和文件权限)
/oradata/dg/mkdir standy
/oradata/dg/cd standy
/oradata/dg/mkdir adump bdump cdump ddump
=====================================================================
4、增加standby redologs
在备机创建,注意组号不要与主机online redologs重复,大小要与主库的online redologs一致
首先查询当前主库的online redologs大小等信息
select group#,type,member from v$logfile; 查询online redologs路径
select thread#,group#,bytes/1024/1024 from v$log; 查询online redologs大小,及组号
ALTER DATABASE ADD STANDBY LOGFILE GROUP 4 ('/oradata/dg/redost4_1') size 10M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 5 ('/oradata/dg/redost5_1') size 10M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 6 ('/oradata/dg/redost6_1') size 10M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 7 ('/oradata/dg/redost7_1') size 10M;
================================================================
检查日志文件
select member from v$logfile;
select * from v$logfile;
MEMBER
--------------------------------------------------------------------------------
/oratest/app/oracle/oradata/allinpay/redo03.log
/oratest/app/oracle/oradata/allinpay/redo02.log
/oratest/app/oracle/oradata/allinpay/redo01.log
/oradata/dg/redost4_1
/oradata/dg/redost5_1
/oradata/dg/redost6_1
/oradata/dg/redost7_1
5、env | grep ORACLE_SID -----查看并确认当前备机的实例名
export ORACLE_SID=standby
6、测试两台服务器网络
TNSPING primdb
TNSPING standby
7、配置备机初始化参数文件(在从primary复制过来的pfile文件上更改)
================================================================
background_dump_dest='/oradata/app/oracle/oradata/stddb/bdump'
core_dump_dest='/oradata/app/oracle/oradata/stddb/cdump'
user_dump_dest='/oradata/app/oracle/oradata/stddb/udump'
compatible='11.1.0.0.0'
db_block_size=8192
db_recovery_file_dest='/oradata/app/oracle/flash_recovery_area'
db_recovery_file_dest_size=8G
undo_tablespace='UNDOTBS1'
undo_management=auto
db_name=primdb
db_unique_name=stddb
log_archive_config='dg_config=(primdb,stddb)'
control_files='/oradata/app/oracle/oradata/stddb/control01.ctl','/oradata/app/oracle/oradata/stddb/control02.ctl','/oradata/app/oracle/oradata/stddb/control03.ctl'
log_archive_dest_1='location=/oradata/stddb/ valid_for=(all_logfiles,all_roles) db_unique_name=stddb'
log_archive_dest_2= 'service=primdb lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=primdb'
log_archive_dest_state_1=enable
log_archive_dest_state_2=enable
remote_login_passwordfile='EXCLUSIVE'
log_archive_format=%t_%s_%r.arc
log_archive_max_processes=30
db_file_name_convert='/oradata/app/oracle/oradata/primdb/','/oradata/app/oracle/oradata/stddb/',/oradata/primdb/ ,/oradata/stddb/
log_file_name_convert= '/oradata/app/oracle/oradata/primdb/','/oradata/app/oracle/oradata/stddb/',/oradata/primdb/ ,/oradata/stddb/
fal_server=primdb
fal_client=stddb
standby_file_management=auto
================================================================
8、修改并保存参数文件后,以SYSDBA登录standby通过pfile创建spfile
create spfile from pfile='/tmp/pfile_pri.ora';
9、启动standby到nomount状态
startup nomount
10、在主库通过RMAN来恢复
rman target / auxiliary sys/123456@orcl --在主库执行该命令,连接到standby数据库,orcl是备库的实例
RMAN> duplicate target database for standby; --连接成功后,执行该命令,来恢复备库,前提要保证能够找到备份文件
11、检查standby数据库状态
select status from v$instance;
12、查看路径转换
show parameter convert
13、在数据字典中查看standby实际识别的数据文件路径,看是否已经转换成功
select name from v$datafile;
如果路径显示正确的话,standby已经可以接受primary的日志了
14、在主库,更改log_archive_dest_state_2参数
ALTER SYSTEM SET log_archive_dest_state_2=enable scope=spfile;
===========================================================
备库日志接收应用
1、下面在主机操作
ALTER SYSTEM SET LOG_ARCHIVE_DEST_STATE_2=ENABLE
日志发送已经开始
2、查询日志发送接受结果
在主机
select max(sequence#) from v$archived_log;
在备机
select max(sequence#) from v$archived_log;
select sequence#,name from v$archived_log order by sequence# desc;
select DEST_ID,THREAD#,SEQUENCE#,APPLIED from v$archived_log order by sequence# desc;
select max(SEQUENCE#),APPLIED from v$archived_log group by APPLIED;
select DEST_ID,THREAD#, max(SEQUENCE#),APPLIED from v$archived_log group by APPLIED,DEST_ID,THREAD#;
3、启动备机的redo应用
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
4、暂停redo应用
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
打开数据库并查询
ALTER DATABASE OPEN;
select * from scott.tmp;
验证有效性
1、在primary数据库和standby数据库分别输入以下查询语句:
SQL>select switchover_status from v$database;
如果2者输出结果分别为”to standby”和”to primary”,则表示成功。
2、在主库切换日志,备库观察alert文件,确认日志接收到并且应用,表明DG成功。
主库
alter system set logfile;
备库
tail -f /oradata/app/oracle/oradata/stddb/bdump/alert_stddb.log
3、查询数据库的表空间和对应的数据文件
set line 150 pages 1000
col file_name for a50
select file_name,tablespace_name from dba_data_files order by 2;
4、查看联机日志文件
select group#,type,member from v$logfile;
select group#,thread#,sequence#,archived,status from v$standy_log;
5、查看控制文件
select name from v$controlfile;
6、首先查看当前的保护模式 ---primary数据库操作
SQL> select protection_mode,protection_level from v$database