DataGuard环境搭建
自己做的工作一直涉及的是单实例数据库,对容灾也没有特别高的要求,平时基本上是靠rman备份或是手工逻辑备份,所以一直想做一些关于dataguard的实验去验证一些感觉很棒的操作,本文参照了“三思”的一些文档搭建了一套这样的环境,很是兴奋,虽然对理论知识不是太理解,但是用实践来验证理论会更好的理解吧;下面是自己搭建dataguard环境时的一些简要记录:
创建主库,即在一台物理机上安装数据库软件及创建数据库作为主库,并启动到force logging 状态(alter database force logging;);
创建备库,在另一台物理机上只安装数据库软件作为备库,所有路径与主库一致;
在主库上创建备库的控制文件:SQL> alter database create standbycontrolfile as 'd:\backup\control.ctl';
关闭主库,把主库所有数据文件(如果备库没有相关目录,那么连带目录一并),拷贝到备库的相应位置,把创建的备库控制文件拷贝到拷贝到相应位置然后复制与主库一样的份数及名称;
拷贝主库的密码文件到备库的相应文件中;
修改主库的spfile 文件,主要是添加下列内容,修改的时候创建pfile,修改后再创建spfile:
*.log_archive_format='%T%S%r.ARC'
*.DB_UNIQUE_NAME='primary'
*.log_archive_config='DG_CONFIG=(primary,standby)'
*.log_archive_dest_1='location=F:\flash_recover_area\ORCL\ARCHIVELOGVALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=PRIMARY'
*.log_archive_dest_2='SERVICE=standbyarch ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=standby'
*.STANDBY_FILE_MANAGEMENT=AUTO
*.LOG_ARCHIVE_DEST_STATE_1=ENABLE
*.LOG_ARCHIVE_DEST_STATE_2=ENABLE
*.FAL_SERVER='standby'
*.FAL_CLIENT='primary'
修改主库的tns,添加下列内容:
PRIMARY =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 45394bcd93e1473)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
STANDBY =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.217.130)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
当然备库的spfile也得修改,主要添加下列内容:
*.log_archive_format='%T%S%r.ARC'
*.DB_UNIQUE_NAME='standby'
*.log_archive_config='DG_CONFIG=(primary,standby)'
*.log_archive_dest_1='location=F:\flash_recover_area\ORCL\ARCHIVELOGVALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=standby'
*.log_archive_dest_2='SERVICE=primaryarch ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=primary'
*.STANDBY_FILE_MANAGEMENT='AUTO'
*.LOG_ARCHIVE_DEST_STATE_1='ENABLE'
*.LOG_ARCHIVE_DEST_STATE_2='ENABLE'
*.FAL_CLIENT='standby'
*.FAL_SERVER='primary'
修改备库的tns,添加下列内容:
PRIMARY =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.106.57)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
STANDBY =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.217.130)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = standby)
)
)
由于备库没有实力服务,所以在备库中创建实例的服务:
1.用oradim工具创建备库orcl实例
oradim-new -sid orcl -startmode m
oradim-edit -sid orcl -startmode a
其实到这里配置基本上就结束了,下面就是启动和验证是否可用了;
启动及关闭顺序,启动时先启动备库,再主库;关闭时,先关闭主库,再备库;
在备库将实例启动到mount 状态:
SQL> startup nomount;
SQL>alter database mount standby database ;
SQL>alter database recover managed standby database disconnect from session;
SQL>alter database recover managed standby database cancel;
备库启监听:
$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;
主备切换
--主库
SQL> select switchover_status fromv$database;
SWITCHOVER_STATUS
--------------------
TO STANDBY
SQL> alter database commit to switchoverto physical standby with session shutdown ;
数据库已更改。
SQL> alter database commit to switchoverto physical standby;
alter database commit to switchover tophysical standby
*
第 1 行出现错误:
ORA-01507: 未装载数据库
SQL> shutdown immediate;
ORA-01507: 未装载数据库
ORACLE 例程已经关闭。
SQL> startup
ORACLE 例程已经启动。
Total System Global Area 836976640 bytes
Fixed Size 1377812 bytes
Variable Size 687868396 bytes
Database Buffers 142606336 bytes
Redo Buffers 5124096 bytes
数据库装载完毕。
数据库已经打开。
SQL> alter database commit to switchoverto physical standby;
数据库已更改。
SQL> select switchover_status fromv$database;
SWITCHOVER_STATUS
--------------------
TO PRIMARY
SQL> shutdown immediate;
数据库已经关闭。
已经卸载数据库。
ORACLE 例程已经关闭。
SQL> startup nomount;
ORACLE 例程已经启动。
Total System Global Area 836976640 bytes
Fixed Size 1377812 bytes
Variable Size 687868396 bytes
Database Buffers 142606336 bytes
Redo Buffers 5124096 bytes
SQL> alter database mount standbydatabase;
数据库已更改。
SQL> alter database recover managedstandby database disconnect from session;
数据库已更改。
---备