Oracle DataGuard 物理Standby 搭建
分类: Oracle
物理standby database 环境搭建
Arch asysnc
Oracle Dataguard
primary
host: primary
IP: 192.168.198.136
Oracle_sid: dgtest
DB_unique_name: dg_pd
FAL_server: dg_st
FAL_client: dg_pd
standby
host: standy
IP: 192.168.198.128
Oracle_sid: dgtest
DB_unique_name: dg_st
FAL_server: dg_pd
FAL_client: dg_st
一. Primary 端的配置
1. 主库设置为force logging 模式
SQL> alter database force logging;
2.修改归档日志存放路径:
mkidr -p /u01/archive/
chown -R oracle:oinstall /u01/archive/
chmod 775 /u01/archive/
SQL>alter system set log_archive_dest_1='location=/u01/archive/' scope=both;
3. 主库设为归档模式
SQL> archive log list;
SQL> shutdown immediate
SQL> startup mount
SQL> alter database archivelog;
SQL> archive log list;
4. 创建备库的密码文件和控制文件
SQL> alter database create standby controlfile as '$ORACLE_BASE/oradata/dgtest/standby01.ctl';
-- 说明: 判断一个数据库是Primary还是Standby,就是通过控制文件来判断的。
[oracle@localhostdbs]$ orapwd file=$ORACLE_HOME/dbs/orapwdgtest password=oracle
如果已经存在,就不用创建了。 缺省情况下,win下口令文件的格式是pwdsid.ora,unix下的格式是orapwSID(大小写敏感)
5. 修改初始化参数文件
*.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文件中配置的
用'$ORACLE_HOME/dbs/initdgtest.ora' 这个pfile 启动数据库,并生成spfile。
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup pfile='$ORACLE_HOME/dbs/initdgtest.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='$ORACLE_HOME/dbs/initdgtest.ora';
File created.
6. 修改listener.ora 和tnsnames.ora 文件
Listener.ora 文件:$ORACLE_HOME/network/admin/listener.ora
# listener.ora Network Configuration File: /u01/app/oracle/product/10.2.0/db1/network/admin/listener.ora
# listener.ora Network Configuration File: /u01/app/oracle/product/10.2.0/db1/network/admin/listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = /u01/app/oracle/product/10.2.0/db1)
(PROGRAM = extproc)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
(ADDRESS = (PROTOCOL = TCP)(HOST = primary)(PORT = 1521))
)
)
注意: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 文件
# tnsnames.ora Network Configuration File: /u01/app/oracle/product/10.2.0/db1/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
DG_PD =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.198.136)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = DG_PD)
)
)
DG_ST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.198.128)(PORT =1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = DG_ST)
)
)
EXTPROC_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
)
(CONNECT_DATA =
(SID = PLSExtProc)
(PRESENTATION = RO)
)
)
二. 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/
mkdir $ORACLE_BASE/flash_recovery_area/DGTEST/onlinelog
在root账户下创建目录 /u01/archive/并修改该目录权限
mkidr -p /u01/archive/
chown -R oracle:oinstall /u01/archive/
chmod 775 /u01/archive/
2. 将主库的密码文件,控制文件,数据文件,参数文件,日志文件copy到备库。
说明一点,这个控制文件是我们自己创建的standby 控制文件。将copy过来的控制文件再复制三份就可以了。 主备的控制文件是不一样的。 这里除了采用直接copy 文件之外,还可以采用Rman 恢复来做。 直接copy 需要停数据库,如果采用RMAN 的话,就不需要停机了。
--数据文件,redo文件
scp *.dbf 192.168.198.128:/u01/app/oracle/oradata/dgtest
scp *.log 192.168.198.128:/u01/app/oracle/oradata/dgtest
--密码文件,参数文件
scp initdgtest.ora 192.168.198.128:/u01/app/oracle/product/10.2.0/db1/dbs
scp orapwdgtest 192.168.198.128:/u01/app/oracle/product/10.2.0/db1/dbs
--listener tnsnames
scp listener.ora 192.168.198.128:/u01/app/oracle/product/10.2.0/db/network/admin
scp tnsnames.ora 192.168.198.128:/u01/app/oracle/product/10.2.0/db/network/admin
--standby 控制文件
scp standby01.ctl 192.168.198.128:/u01/app/oracle/oradata/dgtest/
--archivelog
scp /u01/archive/* 192.168.198.128:/u01/archive/
登陆到192.168.198.128备库这台服务器将传过去的standby01.ctl文件复制成如下:
cp standby01.ctl control01.ctl
cp standby01.ctl control02.ctl
cp standby01.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'st
*.FAL_CLIENT='dg_sd'
--
保存为std.ora
4. 修改listener.ora 和 tnsnames.ora 文件,如果不存在,就从主库上copy 过去。
至此,Data Guard 的操作已经完成,下面来开始验证。
补充:
启动备库
SQL>startup nomount pfile='/u01/app/oracle/product/10.2.0/db1/dbs/initdgtest.ora'
SQL>create spfile from pfile='/u01/app/oracle/product/10.2.0/db1/dbs/initdgtest.ora'
------------------------------------环境 End-----------------------------------
-----------start------------
--注意Data Guard 启动顺序:
启动顺序:先standby ,后primary;
关闭顺序:先primary 后standby;
在备库将实例启动到mount 状态:
SQL> startup nomount;
SQL>alter database mount standby database ;
启动redo apply: SQL>alter database recover managed standby database disconnect from session;
(停止redo apply: SQL>ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;)
在备库启动监听:
$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;
SQL>alter database recover managed standby database disconnect from session;
--验证是否传输到了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=dg_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;