SELECT log_mode FROM v$database;
若已开归档,则显示如下:
LOG_MODE
------------------------
ARCHIVELOG
SHUTDOWN IMMEDIATE;
STARTUP MOUNT;
ALTER DATABASE ARCHIVELOG;
ALTER DATABASE OPEN;
ALTER DATABASE FORCE LOGGING;
SQL> show parameter db_name
SQL> show parameter db_unique_name
为了保证db_unique_name写入pfile内,alter system一下;
alter system set db_unique_name=wqdb scope=spfile;
列出主备库上的DB_UNIQUE_NAME 参数。默认情况下,定义该参数能确保主备库数据库能够互相识别对方Primary与Standby端的db_unique_name不一致时
ALTER SYSTEM SET LOG_ARCHIVE_CONFIG='DG_CONFIG=(wqdb,wqdbdg)';
如在主备库db_unique_name不一致的情况下未配置 LOG_ARCHIVE_CONFIG则会出现如下报错
ORA-16057: DGID from server not in Data Guard configuration
原因:主库没有设置参数log_archive_config
解决方法*.log_archive_config=‘dg_config=( Primary , Standby )’
SQL> select name from v$datafile;
NAME
--------------------------------------------------------------------------------
/opt/oracle/oradata/wqdb/system01.dbf
/opt/oracle/oradata/wqdb/sysaux01.dbf
/opt/oracle/oradata/wqdb/undotbs01.dbf
/opt/oracle/oradata/wqdb/users01.dbf
SQL> select * from v$logfile;
GROUP# STATUS TYPE
---------- -------------- --------------
MEMBER
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IS_REC
------
3 ONLINE
/opt/oracle/oradata/wqdb/redo03.log
NO
2 ONLINE
/opt/oracle/oradata/wqdb/redo02.log
NO
GROUP# STATUS TYPE
---------- -------------- --------------
MEMBER
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IS_REC
------
1 ONLINE
/opt/oracle/oradata/wqdb/redo01.log
NO
该参数仅当数据库角色为primary时生效,指定primary归档redo log到该参数定义的standby database上。
log_archive_dest_2可以说是dataguard上最重要的参数之一,它定义了redo log的传输方式(sync or async)以及传输目标(即standby apply node),直接决定了dataguard的数据保护级别。
ALTER SYSTEM SET LOG_ARCHIVE_DEST_2='SERVICE=wqdbdg NOAFFIRM ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=wqdbdg';
ALTER SYSTEM SET LOG_ARCHIVE_DEST_STATE_2=ENABLE;
LOG_ARCHIVE_DEST_2参数里定义的service值,比如wqdbdg,是tnsnames.ora文件里定义的Oracle Net名称。
有时会在LOG_ARCHIVE_DEST_2定义DB_UNIQUE_NAME的值,当前节点设置的均为另一端数据库的db_unique_name。以primary端为例.
这个参数的意义为: 在数据库wqdbdg上log_archive_dest_2对主库上的联机日志都有效
ALTER SYSTEM SET LOG_ARCHIVE_FORMAT='%t_%s_%r.arc' SCOPE=SPFILE;
ALTER SYSTEM SET LOG_ARCHIVE_MAX_PROCESSES=10;
ALTER SYSTEM SET REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE SCOPE=SPFILE;
当Primary Database的某些日志没有成功发送到Standby Database, 这时候发生饿了归档裂缝(Archive Gap)。
FAL是Fetch Archive Log的简写,它是dataguard主备之间GAP的处理机制。
当Primary Database的某些日志没有成功发送到Standby Database, 这时候发生饿了归档裂缝(Archive Gap)。
Primary上不会有GAP,所以fal_server和fal_client也是只在standby上生效的参数,当然为了switch over的需要同样会在primary端进行预设置。
缺失的这些日志就是裂缝(Gap)。 Data Guard能够自动检测,解决归档裂缝,不需要DBA的介入。这需要配置FAL_CLIENT, FAL_SERVER 这两个参数(FAL: Fetch Archive Log)。
从FAL 这个名字可以看出,这个过程是Standby Database主动发起的“取”日志的过程,Standby Database 就是FAL_CLIENT. 它是从FAL_SERVER中取这些Gap,
ALTER SYSTEM SET FAL_SERVER=wqdbdg;
alter system set fal_client=wqdb;
ALTER SYSTEM SET DB_FILE_NAME_CONVERT='/opt/oracle/oradata/wqdb/','/opt/oracle/oradata/wqdbdg/' SCOPE=SPFILE;
ALTER SYSTEM SET LOG_FILE_NAME_CONVERT='/opt/oracle/oradata/wqdb/','/opt/oracle/oradata/wqdbdg/' SCOPE=SPFILE;
ALTER SYSTEM SET STANDBY_FILE_MANAGEMENT=AUTO;
primary与standby上diskgroup的名称或是数据文件的存放路径不一致的时候,需要定义该参数进行转换,否则standby apply后无法创建与primary一致的数据文件并报错。
db_file_name_convert 主数据库和备用数据库的数据文件转换目录对映(如果两数据库的目录结构不一样),如果有多个对映,逐一指明对映关系。
格式:
*.db_file_name_convert=主数据库数据文件目录,备用数据库数据文件目录
LOG_FILE_NAME_CONVERT定义主备log文件的存放路径转换。
初始化参数STANDBY_FILE_MANAGEMENT作用于standby数据库 ,用来控制是否自动将Primary数据库增加表空间或数据文件的改动,传播到物理Standby数据库。该参数有两个值:
AUTO:如果该参数值设置为AUTO,则Primary数据库执行的表空间创建操作也会被传播到物理Standby数据库上执行。
MANUAL:如果设置为MANUAL或未设置任何值(默认值是MANUAL),需要手工复制新创建的数据文件到物理Standby服务器。
commit;
shutdown immediate
startup
非常关键的一步,一个是路径必须和原来的日志路径一致,第二个日志个数要比原来至少多一个
ALTER DATABASE ADD STANDBY LOGFILE ('/opt/oracle/oradata/wqdb/standby_redo04.log') SIZE 50M;
ALTER DATABASE ADD STANDBY LOGFILE ('/opt/oracle/oradata/wqdb/standby_redo05.log') SIZE 50M;
ALTER DATABASE ADD STANDBY LOGFILE ('/opt/oracle/oradata/wqdb/standby_redo06.log') SIZE 50M;
ALTER DATABASE ADD STANDBY LOGFILE ('/opt/oracle/oradata/wqdb/standby_redo07.log') SIZE 50M;
commit;
alter system set local_listener='';
commit;
shutdown immediate
startup
cat /opt/oracle/products/network/admin/tnsnames.ora
WQDB =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.160.117)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = wqdb)
)
)
WQDBDG =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.160.118)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = wqdbdg)
)
)
tnsping wqdb
tnsping wqdbdg
commit;
shutdown immediate
startup
这步修改pfile文件非常关键,该pfile文件是后续给备库使用;或者直接后面创建一个pfile也是可以的。
control file也可以不用。
ALTER DATABASE CREATE STANDBY CONTROLFILE AS '/archive/archive1/wqdbdg.ctl';
CREATE PFILE='/archive/archive1/initwqdbdg.ora' FROM SPFILE;
//*.db_unique_name='wqdbdg'
//*.fal_server='wqdb'
//*.log_archive_dest_2='SERVICE=wqdb ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=wqdb'
//*.local_listener='wqdb' //动态注册参数
修改前后对比图:
$ mkdir -p /opt/oracle/oradata/wqdb //control file
$ mkdir -p /opt/oracle/admin/wqdb/adump //adump目录
$ mkdir -p /opt/oracle/oradata/wqdb //datafile
$ mkdir -p /opt/oracle/fast_recovery_area/wqdb
$ # Standby controlfile to all locations.
$ scp [email protected]:/archive/archive1/wqdbdg.ctl /opt/oracle/oradata/wqdb/control01.ctl
$ cp /opt/oracle/oradata/wqdb/control01.ctl /opt/oracle/fast_recovery_area/wqdb/control02.ctl
$ # Parameter file.
$ scp [email protected]:/archive/archive1/initwqdb.ora /data/data1/initwqdb.ora //pfile
$ # Remote login password file.
$ scp [email protected]:$ORACLE_HOME/dbs/orapwwqdb $ORACLE_HOME/dbs/
Notice, the backups were copied across to the standby server as part of the FRA copy. If your backups are not held within the FRA, you must make sure you copy them to the standby server and make them available from the same path as used on the primary server.
网络监听是非常关键的一步,本步配置的是静态监听,所以在lsnrctl status里显示的应该是UNKNOWN
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = wqdbdg)
(ORACLE_HOME = /opt/oracle/products)
(SID_NAME = wqdbdg)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.160.118)(PORT = 1521))
)
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
ADR_BASE_LISTENER = /opt/oracle
$ lsnrctl start
$ lsnrctl reload
$ lsnrctl status
export ORACLE_SID=wqdbdg
$ sqlplus / as sysdba
SQL> STARTUP NOMOUNT PFILE='/data/data1/initwqdbdg.ora';
SQL> exit
rman TARGET sys/oracle@wqdb AUXILIARY sys/oracle@wqdbdg
DUPLICATE TARGET DATABASE
FOR STANDBY
FROM ACTIVE DATABASE
DORECOVER
SPFILE
SET db_unique_name='WQDBDG' COMMENT 'Is standby'
SET LOG_ARCHIVE_DEST_2='SERVICE=wqdb ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=wqdb'
SET FAL_SERVER='WQDB' COMMENT 'Is primary'
NOFILENAMECHECK;
在备库上操作,主要使用第二个命令
# Foreground redo apply. Session never returns until cancel.
SQL>ALTER DATABASE RECOVER MANAGED STANDBY DATABASE;
# Background redo apply. Control is returned to the session once the apply process is started.
SQL>ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
SQL>ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
SQL>ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
SQL>ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DELAY 30 DISCONNECT FROM SESSION;
SQL>ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
SQL>ALTER DATABASE RECOVER MANAGED STANDBY DATABASE NODELAY DISCONNECT FROM SESSION;
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE;
1)启动Redo Apply
在物理Standby数据库中启动日志应用服务前,应确保物理Standby数据库已启动,加载,然后使用alter database recover managed standby database SQL语句启动Redo Apply。可以明确指定Redo Apply作为前台会话或者后台进程运行,并启动实时应用。
a)执行下面的的SQL语句在前台启动Redo Apply。
SQL> alter database recover managed standby database;
如果开始一个前台会话,控制权不会返回到命令提示符。
b)执行行以下SQL语句在后台启动Redo Apply。
SQL>alter database recover managed standby database disconnect from session;
以上语句开始一个单独的服务器进程,立即返回控制权给用户,当恢复进程在后台运行时,执行recover语句的窗口能继续执行其它工作,DISCONNECT FROM SESSION选项表示Redo Apply在后台会话中运行。以上两种方式都只会发生在日志切换的时候才应用日志。
c)执行以下SQL语句在前台启动启动Redo实时应用:
SQL>alter database recover managed standby database using current logfile;
实时应用包含USING CURRENT LOGFILE子句,表示Redo在接收到的时候就应用
d)执行以下SQL语句在后台启动Redo实时应用:
SQL>alter database recover managed standby database using current logfile disconnect from session;
2)停止Redo Apply
执行以下的SQL语句停止Redo Apply;
SQL>alter database recover managed standby database cancel;
ALTER SESSION SET nls_date_format='DD-MON-YYYY HH24:MI:SS';
SELECT sequence#, first_time, next_time FROM v$archived_log ORDER BY sequence#;
ALTER SYSTEM SWITCH LOGFILE;
ALTER SESSION SET nls_date_format='DD-MON-YYYY HH24:MI:SS';
SELECT sequence#, first_time, next_time, applied FROM v$archived_log ORDER BY sequence#;
对于主库会有以下三种不同的保护模式:
SELECT protection_mode FROM v$database;
PROTECTION_MODE
--------------------
MAXIMUM PERFORMANCE
SQL>
-- Maximum Availability.
ALTER SYSTEM SET LOG_ARCHIVE_DEST_2='SERVICE=db11g_stby AFFIRM SYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=DB11G_STBY';
ALTER DATABASE SET STANDBY DATABASE TO MAXIMIZE AVAILABILITY;
-- Maximum Performance.
ALTER SYSTEM SET LOG_ARCHIVE_DEST_2='SERVICE=db11g_stby NOAFFIRM ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=DB11G_STBY';
ALTER DATABASE SET STANDBY DATABASE TO MAXIMIZE PERFORMANCE;
-- Maximum Protection.
ALTER SYSTEM SET LOG_ARCHIVE_DEST_2='SERVICE=db11g_stby AFFIRM SYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=DB11G_STBY';
SHUTDOWN IMMEDIATE;
STARTUP MOUNT;
ALTER DATABASE SET STANDBY DATABASE TO MAXIMIZE PROTECTION;
ALTER DATABASE OPEN;
-- Convert primary database to standby
CONNECT / AS SYSDBA
ALTER DATABASE COMMIT TO SWITCHOVER TO STANDBY;
-- Shutdown primary database
SHUTDOWN IMMEDIATE;
-- Mount old primary database as standby database
STARTUP NOMOUNT;
ALTER DATABASE MOUNT STANDBY DATABASE;
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
-- Convert standby database to primary
CONNECT / AS SYSDBA
ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY;
-- Shutdown standby database
SHUTDOWN IMMEDIATE;
-- Open old standby database as primary
STARTUP;
当主库不可用的时候
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE FINISH;
ALTER DATABASE ACTIVATE STANDBY DATABASE;
SQL> select * from v$archive_gap;
THREAD#,LOW_SEQUENCE# HIGH_SEQUENCE#
--------------------------------------------------------------------------------
1 85 89
SQL> select name from v$archived_log where thread#=1 and dest_id=1 and sequence#>=84 and sequence#<90;
SQL> alter database register logfile '/arch/testdb/1_85_882457974.arc';
SQL> alter database register logfile '/arch/testdb/1_86_882457974.arc';
SQL> alter database register logfile '/arch/testdb/1_87_882457974.arc';
SQL> alter database register logfile '/arch/testdb/1_88_882457974.arc';
SQL> alter database register logfile '/arch/testdb/1_89_882457974.arc';