物理standby database 环境搭建
Arch asysnc
Oracle Dataguard
host |
IP |
Oracle_sid |
DB_unique_name |
FAL_server |
FAL_client |
primary |
192.168.126.136 |
dgtest |
dg_pd |
dg_st |
dg_pd |
standy |
192.168.126.128 |
dgtest |
dg_st |
dg_pd |
dg_st |
一. Primary 端的配置
1. 主库设置为force logging 模式
SQL> alter database force logging;
2. 主库设为归档模式
SQL> archive log list;
SQL> shutdown immediate
SQL> startup mount
SQL> alter database archivelog;
SQL> archive log list;
. 创建备库的密码文件和控制文件
SQL> alter database create standby controlfile as '/u01/control01.ctl';
-- 说明: 判断一个数据库是Primary还是Standby,就是通过控制文件来判断的。
[oracle@localhostdbs]$ orapwd file=/u01/orapwDG password=ank88ank
如果已经存在,就不用创建了。 缺省情况下,win下口令文件的格式是pwdsid.ora,unix下的格式是orapwSID(大小写敏感)
4. 修改初始化参数文件
*.DB_UNIQUE_NAME='dg_pd'
*.LOG_ARCHIVE_CONFIG='DG_CONFIG=(dg_pd,dg_st)'
*.log_archive_dest_1='location=/u01/archive VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=dg_pd'
*.LOG_ARCHIVE_DEST_2='SERVICE=dg_st ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=dg_st '
*.LOG_ARCHIVE_DEST_STATE_1=ENABLE
*.LOG_ARCHIVE_DEST_STATE_2=ENABLE
*.standby_file_management='AUTO'
*.FAL_SERVER='dg_st'
*.FAL_CLIENT='dg_pd'
如果主库和备库的数据文件位置不同,还需要加如下2个参数:
*.log_file_name_convert='/u02/oradata/dgtest/','/u03/oradata/dgtest/'
*.db_file_name_convert='/u02/oradata/dgtest/','/u03/oradata/dgtest/'
-- 注意:orcl_st,orcl_pd 是在tnsnames文件中配置的
用'/u01/pri.ora' 这个pfile 启动数据库,并生成spfile。
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup pfile='/u01/pri.ora';
ORACLE instance started.
Total System Global Area 167772160 bytes
Fixed Size 1218316 bytes
Variable Size 79694068 bytes
Database Buffers 83886080 bytes
Redo Buffers 2973696 bytes
Database mounted.
Database opened.
SQL> create spfile from pfile='/u01/pri.ora';
File created.
6. 修改listener.ora 和tnsnames.ora 文件
Listener.ora 文件:
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME =/u01/app/oracle/product/10.2.0/db)
(PROGRAM = extproc)
)
(SID_DESC =
(GLOBAL_DBNAME = dg)
(ORACLE_HOME = /u01/app/oracle/product/10.2.0/db)
(SID_NAME = dgtest)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST =192.168.32.129)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
)
)
注意:SID_LIST_LISTENER 配置的是静态注册,如果没有该参数,而且Data Guard 启动顺序又不正确,那么在主库可能会报 PING[ARC1]: Heartbeat failed to connect to standby 'orcl_st'. Error is 12514. 错误,导致归档无法完成。
Oracle Listener 动态注册 与 静态注册
http://blog.csdn.net/tianlesoftware/archive/2010/04/30/5543166.aspx
Tnsnames.ora 文件
DG_ST =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.32.128)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = dg)
)
)
DG_PD =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.32.129)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = dg)
)
)
二. Standby 端配置
1. 创建备库存放数据文件和后台跟踪目录, 这个目录可以和主库相同, 如果不同,就需要在主库的初始化文件中进行转换。
如:
*.log_file_name_convert='/u02/oradata/dgtest/','/u03/oradata/orcl/'
*.db_file_name_convert='/u02/oradata/dgtest/','/u03/oradata/orcl/'
--建相目录
mkdir -p $ORACLE_BASE/oradata/dgtest
mkdir $ORACLE_BASE/admin/dgtest
mkdir -p $ORACLE_BASE/admin/dgtest/adump
mkdir $ORACLE_BASE/admin/dgtest/bdump
mkdir $ORACLE_BASE/admin/dgtest/cdump
mkdir $ORACLE_BASE/admin/dgtest/dpdump
mkdir $ORACLE_BASE/admin/dgtest/pfile
mkdir $ORACLE_BASE/admin/dgtest/udump
mkdir $ORACLE_BASE/admin/dgtest/
2. 将主库的密码文件,控制文件,数据文件,参数文件,日志文件copy到备库。
说明一点,这个控制文件是我们自己创建的standby 控制文件。将copy过来的控制文件再复制三份就可以了。 主备的控制文件是不一样的。 这里除了采用直接copy 文件之外,还可以采用Rman 恢复来做。 直接copy 需要停数据库,如果采用RMAN 的话,就不需要停机了。
--数据文件,redo文件
scp *.dbf 192.168.32.144:/u01/app/oracle/oradata/dgtest
scp *.log 192.168.32.144:/u01/app/oracle/oradata/dgtest
--密码文件,参数文件
scp pri.ora 192.168.32.144:/u01
scp orapwdgtest 192.168.32.144:/u01/app/oracle/product/10.2.0/db/dbs
--listener tnsnames
scp listener.ora 192.168.32.144:/u01/app/oracle/product/10.2.0/db/network/admin
scp tnsnames.ora 192.168.32.144:/u01/app/oracle/product/10.2.0/db/network/admin
--standby 控制文件
scp *.ctl 192.168.32.144:/u01/app/oracle/oradata/dgtest/
cp control01.ctl control02.ctl
cp control01.ctl control03.ctl
3. 修改初始化参数文件
*.DB_UNIQUE_NAME='dg_st'
*.LOG_ARCHIVE_CONFIG='DG_CONFIG=(dg_pd,dg_st)'
*.log_archive_dest_1='location=/u01/archive VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=dg_st'
*.LOG_ARCHIVE_DEST_2='SERVICE=dg_pd ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=dg_pd'
*.LOG_ARCHIVE_DEST_STATE_1=ENABLE
*.LOG_ARCHIVE_DEST_STATE_2=ENABLE
*.standby_file_management='AUTO'
*.FAL_SERVER='dg_pd'
*.FAL_CLIENT='dg_sd'
--
保存为std.ora
4. 修改listener.ora 和 tnsnames.ora 文件,如果不存在,就从主库上copy 过去。
至此,Data Guard 的操作已经完成,下面来开始验证。
补充:
启动备库
SQL>startup nomount pfile='/u01/std.ora'
SQL>create spfile from pfile='/u01/std.ora'
------------------------------------环境 End-----------------------------------
-----------start------------
--注意Data Guard 启动顺序:
启动顺序:先standby ,后primary;
关闭顺序:先primary 后standby;
在备库将实例启动到mount 状态:
SQL> startup nomount;
SQL>alter database mount standby database ;
SQL>ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
SQL>alter database recover managed standby database disconnect from session;
在备库启动监听:
$lsnrctl start
在主库启动实例:
SQL> startup;
在主库启动监听:
$lsnrctl start
--------------------DataGuard Acitve OK------------
Now
Primary:open
Standby:mount
--Standby只读模式打开
--停止redo应用
SQL>alter database recover managed standby database cancel
SQL>alter database open;
SQL>select open_mode from v$database;
OPEN_MODE
----------
READ ONLY
--Standby重新回到redo apply状态
SQL>shutdown immediate
SQL>startup nomount
SQL>alter database mount standby database disconnect
SQL>alter database recover managed standby database disconnect
--验证是否传输到了Standby database
--Primary database
SQL>SELECT SEQUENCE#, FIRST_TIME, NEXT_TIME FROM V$ARCHIVED_LOG ORDER BY SEQUENCE#;
SQL> SELECT SEQUENCE#, FIRST_TIME, NEXT_TIME FROM V$ARCHIVED_LOG ORDER BY SEQUENCE#;
SEQUENCE# FIRST_TIM NEXT_TIME
7 23-FEB-11 23-FEB-11
7 23-FEB-11 23-FEB-11
8 23-FEB-11 23-FEB-11
8 23-FEB-11 23-FEB-11
9 23-FEB-11 23-FEB-11
9 23-FEB-11 23-FEB-11
SQL> alter system switch logfile;
System altered.
--Standby database
SQL>SELECT SEQUENCE#, FIRST_TIME, NEXT_TIME FROM V$ARCHIVED_LOG ORDER BY SEQUENCE#;
SEQUENCE# FIRST_TIM NEXT_TIME
---------- --------- ---------
7 23-FEB-11 23-FEB-11
8 23-FEB-11 23-FEB-11
9 23-FEB-11 23-FEB-11
10 23-FEB-11 23-FEB-11
--验证Standby redo 是否应用
SQL>SELECT SEQUENCE#,APPLIED FROM V$ARCHIVED_LOG ORDER BY SEQUENCE#;
SEQUENCE# APP
---------- ---
3 YES
4 YES
5 YES
6 YES
7 YES
8 YES
9 YES
10 YES
8 rows selected.
--如果出错
在主库验证归档目录是否有效:
SQL> SELECT STATUS,DESTINATION, ERROR FROM V$ARCHIVE_DEST;
如果有错误,要排查原因。
SQL> SELECT STATUS ,ERROR FROM V$ARCHIVE_DEST;
STATUS ERROR
--------- -----------------------------------------------------------------
VALID
VALID
INACTIVE
INACTIVE
INACTIVE
INACTIVE
INACTIVE
INACTIVE
INACTIVE
INACTIVE
注意:如果在主库执行 alter database clear unarchived logfile或alter database open resetlogs , 则dataguard要重建。
补充:
--监控日志应用服务
SQL>select database_role,db_unique_name,open_mode,protection_mode,protection_level,switchover_status from v$database;
--设置新的数据保护模式并重启数据库
当保护模式更改顺序:
maximize protection ---> maximize availability ----> maximize performance
当在把dataguard的保护级别按这上面的顺序减低的时候, 不需要primary库在mount状态,否则primary 必须在mount 状态。
如:
SQL> alter database set standby database to maximize availability;
alter database set standby database to maximize availability
*
ERROR at line 1:
ORA-01126: database must be mounted in this instance and not open in any
instance
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.
Total System Global Area 167772160 bytes
Fixed Size 1218316 bytes
Variable Size 79694068 bytes
Database Buffers 83886080 bytes
Redo Buffers 2973696 bytes
Database mounted.
SQL> alter database set standby database to maximize availability;
Database altered.
SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-03113: end-of-file on communication channel
报错了,这是因为最大可用性需要先修改日志传送方式为lgwr同步方式,否则,数据库是无法open.
SQL> alter system set log_archive_dest_2='service=orcl_st lgwr sync AFFIRM';
System altered.
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.
Total System Global Area 167772160 bytes
Fixed Size 1218316 bytes
Variable Size 79694068 bytes
Database Buffers 83886080 bytes
Redo Buffers 2973696 bytes
Database mounted.
SQL> alter database set standby database to maximize availability;
Database altered.
SQL> alter database open;
Database altered.
SQL> select protection_mode,protection_level from v$database;
PROTECTION_MODE PROTECTION_LEVEL
-------------------- --------------------
MAXIMUM AVAILABILITY MAXIMUM AVAILABILITY
alter database set standby database to maximize performance;
提示:maximize后可跟{PROTECTION | AVAILABILITY | PERFORMANCE},分别对应最大保护,最高可用性及最高性能。
在最大保护模式下,直接关闭备库是不行的,如果在备库上关闭数据库,会有如下提示:
SQL> shutdown immediate
ORA-01154: database busy. Open, close, mount, and dismount not allowed now
SQL>
在最大保护模式下,备库是不允许关闭的,此时首先关闭主库,然后备库就可以顺利关闭了
注意: 主库的保护模式修改之后,备库的模式也会改变,和主库保持一致。
3. 查看日志归档情况
主库进行日志切换:
SQL>Alter system switch logfile;
select max(sequence#) from v$archived_log;
select max(sequence#) from v$log_history;
select group#,sequence#,archived,status from v$log;
select name,sequence#,applied from v$archived_log;
select sequence#,applied from v$archived_log;
若不同步,
1>看log日志, archive是否有丢失
2>可以在备库坐如下操作:
--停止redo apply
alter database recover managed standby database cancel;
--开始redo apply
alter database recover managed standby database disconnect from session;