一. 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;
3. 添加redo log file
添加一个新的Standby Redologs组(注意组号不要与当前存在的Online Redologs组重复),并为该组指定一个成员:
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 4 ('/u01/app/oracle/oradata/orcl/redo04.log') size 50M;
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 5 ('/u01/app/oracle/oradata/orcl/redo05.log') size 50M;
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 6 ('/u01/app/oracle/oradata/orcl/redo06.log') size 50M;
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 7 ('/u01/app/oracle/oradata/orcl/redo07.log') size 50M;
4. 修改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_1)
(PROGRAM = extproc)
)
(SID_DESC =
(GLOBAL_DBNAME = orcl)
(ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)
(SID_NAME = orcl)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost.localdomain)(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 文件
ORCL_ST =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.85.10.2)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
ORCL_PD =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.85.10.1)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
5. 创建备库的密码文件和控制文件
SQL> alter database create standby controlfile as '/u01/control01.ctl';
-- 说明: 判断一个数据库是Primary还是Standby,就是通过控制文件来判断的。
[oracle@localhost dbs]$ orapwd file=/u01/app/oracle/product/10.2.0/db_1/dbs/orapworcl password=admin
如果已经存在,就不用创建了。 缺省情况下,win下口令文件的格式是pwdsid.ora,unix下的格式是orapwSID(大小写敏感)
6. 修改初始化参数文件
SQL> create pfile='/u01/initorcl.ora' from spfile;
在initorcl.ora 添加如下内容:
####主库参数######
*.DB_NAME ='orcl';
#--监听中配置的
*.DB_UNIQUE_NAME='orcl_pd'
#--列出DG中所有DB_UNIQUE_NAME
*.LOG_ARCHIVE_CONFIG='DG_CONFIG=(orcl_pd,orcl_st)'
*.LOG_ARCHIVE_DEST_1='LOCATION=/u01/archive VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=orcl_pd'
*.LOG_ARCHIVE_DEST_2='service=orcl_st DB_UNIQUE_NAME=orcl_st'
*.LOG_ARCHIVE_DEST_STATE_1=ENABLE
*.LOG_ARCHIVE_DEST_STATE_2=ENABLE
*.REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE
#####备库参数#######
*.FAL_SERVER=orcl_st
*.FAL_CLIENT=orcl_pd
*.standby_file_management='AUTO'
*.standby_archive_dest='/u01/archive'
#如果主备库目录不同,还需要添加:
#*.log_file_name_convert='/u02/oradata/orcl/','/u03/oradata/orcl/'
#*.db_file_name_convert='/u02/oradata/orcl/','/u03/oradata/orcl/'
-- 注意:orcl_st,orcl_pd 是在tnsnames文件中配置的
用'/u01/initorcl.ora' 这个pfile 启动数据库,并生成spfile。
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup pfile='/u01/initorcl.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/initorcl.ora';
File created.
二. Standby 端配置
1. 创建备库存放数据文件和后台跟踪目录, 这个目录可以和主库相同, 如果不同,就需要在主库的初始化文件中进行转换。
如:
*.log_file_name_convert='/u02/oradata/orcl/','/u03/oradata/orcl/'
*.db_file_name_convert='/u02/oradata/orcl/','/u03/oradata/orcl/'
$ORACLE_BASE/ORADATA/ORCL
$ORACLE_BASE/admin/orcl
$ORACLE_BASE/admin/orcl/adump
$ORACLE_BASE/admin/orcl/bdump
$ORACLE_BASE/admin/orcl/cdump
$ORACLE_BASE/admin/orcl/dpdump
$ORACLE_BASE/admin/orcl/pfile
$ORACLE_BASE/admin/orcl/udump
$ORACLE_BASE/admin/orcl/
2. 将主库的密码文件,控制文件,数据文件,参数文件,日志文件copy到备库。
说明一点,这个控制文件是我们自己创建的standby 控制文件。将copy过来的控制文件再复制三份就可以了。 主备的控制文件是不一样的。 这里除了采用直接copy 文件之外,还可以采用Rman 恢复来做。 直接copy 需要停数据库,如果采用RMAN 的话,就不需要停机了。
[oracle@localhost orcl]$ pwd
/u01/app/oracle/oradata/orcl
[oracle@localhost orcl]$ ls
control01.ctl redo01.log redo04.log redo07.log temp01.dbf
control02.ctl redo02.log redo05.log sysaux01.dbf undotbs01.dbf
control03.ctl redo03.log redo06.log system01.dbf users01.dbf
[oracle@localhost orcl]$ scp *.dbf 10.85.10.2://u01/app/oracle/oradata/orcl
[oracle@localhost u01]$ scp *.ctl 10.85.10.2://u01/app/oracle/oradata/orcl
[oracle@localhost u01]$ scp *.log 10.85.10.2://u01/app/oracle/oradata/orcl
[oracle@localhost u01]$ scp initorcl.ora 10.85.10.2://u01/app/oracle/product/10.2.0/db_1/dbs
[oracle@localhost dbs]$ scp orapworcl 10.85.10.2://u01/app/oracle/product/10.2.0/db_1/dbs
3. 修改初始化参数文件
修改之后如下:
####主库参数######
*.DB_NAME ='orcl';
#--监听中配置的
*.DB_UNIQUE_NAME='orcl_pd'
#--列出DG中所有DB_UNIQUE_NAME
*.LOG_ARCHIVE_CONFIG='DG_CONFIG=(orcl_pd,orcl_st)'
*.LOG_ARCHIVE_DEST_1='LOCATION=/u01/archive VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=orcl_st'
*.LOG_ARCHIVE_DEST_2='service=orcl_pd DB_UNIQUE_NAME=orcl_pd'
*.LOG_ARCHIVE_DEST_STATE_1=ENABLE
*.LOG_ARCHIVE_DEST_STATE_2=ENABLE
*.REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE
#####备库参数#######
*.FAL_SERVER=orcl_pd
*.FAL_CLIENT=orcl_st
*.standby_file_management='AUTO'
*.standby_archive_dest='/u01/archive'
#如果主备库目录不同,还需要添加:
#*.log_file_name_convert='/u02/oradata/orcl/','/u03/oradata/orcl/'
#*.db_file_name_convert='/u02/oradata/orcl/','/u03/oradata/orcl/'
4. 修改listener.ora 和 tnsnames.ora 文件,如果不存在,就从主库上copy 过去。
5. 在备库添加redo log file
如果主库没有添加redo log file,可以先用copy 过来的初始化文件将数据库启动到mount 状态。在创建个spfile,最后添加redo log。
SQL> startup mount pfile='/u01/app/oracle/product/10.2.0/db_1/dbs/initorcl.ora'
ORACLE instance started.
SQL> create spfile from pfile='/u01/app/oracle/product/10.2.0/db_1/dbs/initorcl.ora';
添加一个新的Standby Redologs组(注意组号不要与当前存在的Online Redologs组重复),并为该组指定一个成员:
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 4 ('/u01/app/oracle/oradata/orcl/redo04.log') size 50M;
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 5 ('/u01/app/oracle/oradata/orcl/redo05.log') size 50M;
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 6 ('/u01/app/oracle/oradata/orcl/redo06.log') size 50M;
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 7 ('/u01/app/oracle/oradata/orcl/redo07.log') size 50M;
提示,由于从Primary数据库复制文件时并没有复制Online Redologs,因此物理Standby数据库在第一次启动REDO应用时,会在Alert文件中报Online Redo Logfile文件不存在,没有关系,物理Standby会自动重建这批文件,同时你也不用担心会丢失数据,Online Redologs中的数据会以归档文件的形式从Primary端接收。
至此,Data Guard 的操作已经完成,下面来开始验证。
注意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
在主库验证归档目录是否有效:
SQL> SELECT STATUS,DESTINATION, ERROR FROM V$ARCHIVE_DEST;
如果有错误,要排查原因。
SQL> alter system switch logfile;
SQL> select max(sequence#) from v$archived_log;
MAX(SEQUENCE#)
--------------
70
主备查询结果一致,Data Guard 搭建结束。
注意:如果在主库执行 alter database clear unarchived logfile或alter database open resetlogs , 则dataguard要重建。
三. 一些其他操作
1. 首先查看当前的保护模式 ---primary数据库操作
SQL> select protection_mode,protection_level from v$database;
PROTECTION_MODE PROTECTION_LEVEL
-------------------- --------------------
MAXIMUM PERFORMANCE MAXIMUM PERFORMANCE
2. 设置新的数据保护模式并重启数据库 --primary数据库操作
当保护模式更改顺序:
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.
Maximum protection/AVAILABILITY模式必须满足以下条件
Redo Archival Process: LGWR
Network Tranmission mode: SYNC
Disk Write Option: AFFIRM
Standby Redo Logs: Yes
standby database type: Physical Only
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)可以在备库坐如下操作:
alter database recover managed standby database cancel;
alter database recover managed standby database disconnect from session;
四. 主备库切换
4.1 Switchover
一般SWITCHOVER切换都是计划中的切换,特点是在切换后,不会丢失任何的数据,而且这个过程是可逆的,整个DATA GUARD环境不会被破坏,原来DATA GUARD环境中的所有物理和逻辑STANDBY都可以继续工作。
在进行DATA GUARD的物理STANDBY切换前需要注意:
1)确认主库和从库间网络连接通畅;
2)确认没有活动的会话连接在数据库中;
3)PRIMARY数据库处于打开的状态,STANDBY数据库处于MOUNT状态;
4)确保STANDBY数据库处于ARCHIVELOG模式;
5)如果设置了REDO应用的延迟,那么将这个设置去掉;
6)确保配置了主库和从库的初始化参数,使得切换完成后,DATA GUARD机制可以顺利的运行。
主库:
1. 查看switchover 状态
SQL> SELECT SWITCHOVER_STATUS FROM V$DATABASE;
SWITCHOVER_STATUS
--------------------
TO STANDBY
附: A:switchover_status出现session active/not allowed
当出现session active的时候表示还有活动的session,则运行
Alter database commit to switchover to physical standby with session shutdown;
当出现not allowed时,在官方文档说转换会不成功,但是我测试的时候成功了。
B.ora- 01153: an incompatible media recovery is active
运行下面代码
Alter database recover managed standby database finish;
或者Alter database recover managed standby database finish force;
Alter database recover managed standby database disconnect from session;
2 切换成备库
SQL>Alter database commit to switchover to physical standby with session shutdown;
或者
SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PHYSICAL STANDBY;
Database altered.
3 启动到mount和应用日志状态
SQL> SHUTDOWN IMMEDIATE
SQL> startup nomount;
SQL> alter database mount standby database;
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
4. 查看数据库模式
SQL>select dest_name,status,database_mode,recovery_mode,protection_mode from v$archive_dest_status;
SQL>select status,database_mode from v$archive_dest_status;
备库:
1.查看switchover状态
SQL> SELECT SWITCHOVER_STATUS FROM V$DATABASE;
TO PRIMARY
附:若不是用此语句切换:ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY with session shutdown
补充:若出现:ORA-16139: media recovery required
是因为没有执行:alter database recover managed standby database disconnect from session;
2. 切换成主库
SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY;
Database altered.
SQL> shutdown immediate;
SQL> startup;
SQL> alter system switch logfile;
3. 查看数据库模式
SQL>select dest_name,status,database_mode,recovery_mode,protection_mode from v$archive_dest_status;
SQL>select status,database_mode from v$archive_dest_status;
验证同步:
SQL> select max(sequence#) from v$archived_log;
MAX(SEQUENCE#)
--------------
78
4.2. Failovers:
FAILOVER切换一般是PRIMARY数据库发生故障后的切换,这种情况是STANDBY数据库发挥其作用的情况。这种切换发生后,可能会造成数据的丢失。而且这个过程不是可逆的,DATA GUARD环境会被破坏。
由于PRIMARY数据库已经无法启动,所以FAILOVER切换所需的条件并不多,只要检查STANDBY是否运行在最大保护模式下,如果是的话,需要将其置为最大性能模式,否则切换到PRIMARY角色也无法启动。
1. 查看是否有日志GAP,没有应用的日志:
SQL> SELECT UNIQUE THREAD#, MAX(SEQUENCE#) OVER(PARTITION BY THREAD#) LAST FROM V$ARCHIVED_LOG;
SQL> SELECT THREAD#, LOW_SEQUENCE#, HIGH_SEQUENCE# FROM V$ARCHIVE_GAP;
如果有,则拷贝过来并且注册
SQL> ALTER DATABASE REGISTER PHYSICAL LOGFILE '路径';
重复查看直到没有应用的日志:
2. 然后停止应用归档:
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
Database altered.
3. 下面将STANDBY数据库切换为PRIMARY数据库:
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE FINISH;
或 SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE FINISH FORCE;
Database altered.
SQL> SELECT DATABASE_ROLE FROM V$DATABASE;
DATABASE_ROLE
----------------
PHYSICAL STANDBY
SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY;
Database altered.
SQL> ALTER DATABASE OPEN; 或者 shutdown immediate+startup
Database altered.
检查数据库是否已经切换成功:
SQL> SELECT DATABASE_ROLE FROM V$DATABASE;
DATABASE_ROLE
----------------
PRIMARY
至此,FAILOVER切换完成。这个时候应该马上对新的PRIMARY数据库进行备份。
One Response