【DG】在Linux平台上搭建单实例的dataguard--duplicate
一 : 实验环境介绍
*** 鉴于生产环境均采用 64 位系统和数据库软件,本实验也将采用这个标准 ***
采用vmware workstation 10 (版本随意,我用的 10 )虚拟机软件作为安装平台
对于真机的要求:CPU : INTEL : CORE I3 级别或以上 ( 推荐 )
AMD : 4 核心 CPU 或以上 ( 推荐 )
内存: 建议4 G 以上
系统:WIN7 /8 64 位 /WINDOWS SERVER 2003/2008 64 位
硬盘:虚拟机所在分区剩余空间 5 0G 以上,固态硬盘最好
虚拟机系统: Oracle Enterprise Linux 5 update 5 (64 位)
数据库版本: Oracle 11gR2 11.2.0. 3 (64 位)
基础要求:(1 )安装两台虚拟机 分别 作为 主库和备库 , 硬盘大小随意。但要保证
根分区 有2 0G , 别给太小了就行。 物理内存 1 G , SWAP 2 G
二:安装数据库软件前的系统 配置 (主库和备库端都要做下列操作)
1 .基本配置:
一定要用新装的系统(这里以红帽企业版5.5 为例),系统要求最低配置 1G 内存, 2Gswap 分区,根分区 20G 以上,装好系统后先配置好 yum , IP 地址, /etc/sysconfig/network 文件中的主机名,以及 /etc/hosts 文件中的 IP 地址和主机名的对应关系。
主库IP : 192.168.1.101
主库主机名:pri.cty.com pri
备库IP : 192.168.1.102
备库主机名:std.cty.com std
2 .装包
yum install compat-libstdc++-33 ksh gcc gcc-c++ libgomp elfutils-libelf-devel glibc-devel glibc-headers libaio-devel libstdc++-devel sysstat unixODBC unixODBC-devel - y
3 .查看共享内存大小,要求最低为 1GB
# df -h /dev/shm
Filesystem Size Used Avail Use% Mounted on
tmpfs 506M 0 506M 0% /dev/shm
发现不够,修改
# vim /etc/fstab
如图,在tmpfs 那一行的 defaults 后面加上 ,size=1024m
保存退出后重新挂载:
# mount -o remount /dev/shm
再次查看共享内存:
# df -h /dev/shm
Filesystem Size Used Avail Use% Mounted on
tmpfs 1.0G 0 1.0G 0% /dev/shm
修改成功
4 .创建相关的组与用户:
groupadd oinstall
groupadd dba
g roupadd oper
useradd -g oinstall -G dba,oper oracle
passwd oracle
5 .创建所需的目录并赋予特定的属主和属组
mkdir /u01/app/oracle - pv
chown oracle.oinstall /u01 -R
建议:最好为单独分一个区,然后将u01 挂载到该分区上(可不做,看情况)
6 .编辑 limits.conf 文件
vim /etc/security/limits.conf
添加下面5 行内容:
oracle soft nproc 2047
oracle hard nproc 16384
oracle soft nofile 1024
oracle hard nofile 65536
oracle soft stack 10240
7 .编辑 sysctl.conf 文件,设置相关参数
vim /etc/sysctl.conf
添加/ 修改下列内容:(注意!下面的参数,若是已经存在,则直接修改数值,不要再添加同样的参数,相同的参数只能有一个!如果需要修改的参数已经大于下面的数字,则不用修改,请仔细核对!)
kernel.shmall = 2097152
kernel.shmmax = 536870912
kernel.shmmni = 4096
kernel.sem =250 32000 100 128
fs.file-max = 6815744
fs.aio-max-nr = 1048576
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
保存退出后,别忘了用 sysctl - p 命令使参数生效!
8 .编辑 vim /home/oracle/.bash_profile 文件,配置相关环境变量
添加下列几行:
export TMP=/tmp
export TMPDIR=/tmp
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1
export ORACLE_SID= pri ( 备库端设置为 std)
export PATH=$ORACLE_HOME/bin:$PATH
至此,安装环境相关配置结束。
三 : 开始 安装oracle11g 数据库软件 (主库和备库端都要做下列操作)
1. 挂载 oracle11g 的镜像到 /mnt/cdrom 目录下,并把 3 个包解压到 /usr/local/src 目录下
cd /mnt/cdrom/
unzip p10404530_112030_LINUX_1of7.zip -d /usr/local/src
unzip p10404530_112030_LINUX_2of7.zip -d /usr/local/src
unzip p10404530_112030_LINUX_3of7.zip -d /usr/local/src
( 只装数据库的话,解压前两个包即可,第三个包是 grid)
2. root 身份执行 xhost + ,然后切换到 oracle 用户进入 /usr/local/src/database 目录中,执行 runInstaller 开始安装 (database 是默认解压完的目录名 )
# xhost +
# su - oracle
$ ./runInstaller
注意:安装oracle11g 要求分辨率最低为 1024x768, 不然无法完整显示安装过程
安装过程:略
注意:主库在安装完软件后需要进行DBCA 建库操作,推荐不要启用归档模式,会节省时间,备库端不要建库!
四:开始搭建Dataguard
1 :将数据库改为强制日志模式 (此步骤只在主库上做)
[oracle@ pri ~]$ sqlplus / as sysdba
查看 当前是否强制日志模式:
SYS@pri> select name,log_mode,force_logging from v$database;
NAME LOG_MODE FOR
--------- ------------ ---
PRI NOARCHIVELOG NO
SYS@pri> alter database force logging;
Database altered.
SYS@pri> select name,log_mode,force_logging from v$database;
NAME LOG_MODE FOR
--------- ------------ ---
PRI NOARCHIVELOG YES
2 : 创建密码文件 (此步骤只在主库上做)
注意:两端 分别 创建自己的密码文件好像有问题,备库的密码文件需要跟主库一致,否则导致日志传输不到备库,有待验证。我最后是将 主库 的 密码文件 直接copy 到备库,重命名后使用。
[oracle@ pri ~]$ cd $ORACLE_HOME/dbs
[oracle@ pri dbs]$ ls
hc_racdb1.dat init.ora initracdb1.ora orapwpri 已经有一个密码文件了
[oracle@ pri dbs]$ orapwd file=orapw pri password=oracle force=y
这条命令可以手动生成密码文件,force=y 的意思是强制覆盖当前已有的密码文件
[oracle@ pri dbs]$ ls
hc_pri.dat init.ora initpri.ora lkPRI orapwpri spfilepri.ora
将 主库 的密码文件copy 给备库,并重命名
[oracle@ pri dbs]$ scp orapwpri 192.168.1.102 :$ORACLE_HOME/dbs/ orapwstd
orapwpri 100% 1536 1.5KB/s 00:00
3 :创建 standby redolog 日志组 (此步骤只在主库上做)
原则:
1 : standby redo log 的文件大小与 primary 数据库 online redo log 文件大小相同
2 : standby redo log 日志文件组的个数依照下面的原则进行计算:
Standby redo log 组数公式 >=( 每个 instance 日志组个数 +1)*instance 个数
假如只有一个节点,这个节点有三组redolog ,
所以Standby redo log 组数 >=(3+1)*1 == 4
所以至少需要创建4 组 Standby redo log
查看当前线程与日志组的对应关系及日志组的大小:
SYS@pri> select thread#,group#,bytes/1024/1024 from v$log;
THREAD# GROUP# BYTES/1024/1024
--------------- ------------- ------------------------
1 1 50
1 2 50
1 3 50
如上,我现在的环境有 三 组redolog ,每个日志组的大小都是 50M ,
所以Standby redo log 组数 >=( 3 +1)* 1 == 4
所以至少需要创建 4 组Standby redo log ,大小均为 50M
(thread :线程,只有在多实例数据库才有用的参数,例如 RAC 环境,单实例不考虑)
查看当前有哪些日志组及其成员:
SYS@pri> col member for a50
SYS@pri> select group#,member from v$logfile;
GROUP# MEMBER
---------- --------------------------------------------------
3 /u01/app/oracle/oradata/pri/redo03.log
2 /u01/app/oracle/oradata/pri/redo02.log
1 /u01/app/oracle/oradata/pri/redo01.log
先 手动 创建standby log 日志组所需的目录:
(创建新目录只是为了便于区分,并非必须 )
[oracle@pri dbs]$ cd /u01/app/oracle/oradata/
[oracle@pri oradata]$ ls
pri
[oracle@pri oradata]$ mkdir standbylog
[oracle@pri oradata]$ ls
pri standbylog
新建 4 个日志组作为standby redolog 日志组,大小与原来的日志组一致:
由于已经存在group1- 3 , ,所以 group 号只能从 4 开始
SYS@pri> alter database add standby logfile group 4
2 ' /u01/app/oracle/oradata/standbylog/ std_ redo0 4 .log '
3 size 50m;
Database altered.
SYS@pri> alter database add standby logfile group 5
2 ' /u01/app/oracle/oradata/standbylog/ std_ redo0 5 .log '
3 size 50m;
Database altered.
SYS@pri> alter database add standby logfile group 6
2 ' /u01/app/oracle/oradata/standbylog/ std_ redo0 6 .log '
3 size 50m;
Database altered.
SYS@pri> alter database add standby logfile group 7
2 ' /u01/app/oracle/oradata/standbylog/ std_ redo0 7 .log '
3 size 50m;
Database altered.
查看standby 日志组的信息:
SYS@pri> select group#,sequence#,status, bytes/1024/1024 from v$standby_log;
GROUP# SEQUENCE# STATUS BYTES/1024/1024
---------- ------------------ ------------------- ------------------------
4 0 UNASSIGNED 50
5 0 UNASSIGNED 50
6 0 UNASSIGNED 50
7 0 UNASSIGNED 50
查看当前有哪些日志组及其成员:
SYS@pri> set pagesize 100
SYS@pri> col member for a60
SYS@pri> select group#,member from v$logfile order by group# ;
GROUP# MEMBER
------------ ------------------------------------------------------------
1 /u01/app/oracle/oradata/pri/redo01.log
2 /u01/app/oracle/oradata/pri/redo02.log
3 /u01/app/oracle/oradata/pri/redo03.log
4 /u01/app/oracle/oradata/standbylog/std_redo04.log
5 /u01/app/oracle/oradata/standbylog/std_redo05.log
6 /u01/app/oracle/oradata/standbylog/std_redo06.log
7 /u01/app/oracle/oradata/standbylog/std_redo07.log
4 :修改 主库的 pfile 参数文件 (此步骤只在主库上做)
查看spfile 的路径:
SYS@pri> show parameter spfile;
NAME TYPE VALUE
----------- ----------- -----------------------------------------------------------------
spfile string /u01/app/oracle/product/11.2.0/db_1/dbs/spfilepri.ora
用spfile 创建一个 pfile, 用于修改:
SYS@pri> create pfile from spfile;
修改主库的pfile :
[oracle@ pri ~]$ cd $ORACLE_HOME/dbs
[oracle@ pri dbs]$ vim init pri .ora
pri.__db_cache_size=125829120
pri.__java_pool_size=4194304
pri.__large_pool_size=4194304
pri.__oracle_base='/u01/app/oracle'#ORACLE_BASE set from environment
pri.__pga_aggregate_target=146800640
pri.__sga_target=272629760
pri.__shared_io_pool_size=0
pri.__shared_pool_size=125829120
pri.__streams_pool_size=4194304
*.audit_file_dest='/u01/app/oracle/admin/pri/adump'
*.audit_trail='db'
*.compatible='11.2.0.0.0'
*.control_files='/u01/app/oracle/oradata/pri/control01.ctl','/u01/app/oracle/fast_recovery_area/pri/control02.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='pri' DG 主库和备库的 db_name 必须一致, db_unique_name 不一致
*.db_recovery_file_dest='/u01/app/oracle/fast_recovery_area'
*.db_recovery_file_dest_size=4227858432
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE= priXDB )'
*.memory_target=419430400
*.open_cursors=300
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.undo_tablespace='UNDOTBS1'
下面几行是需要手动添加的:
*.db_unique_name=' pri '
*.log_archive_config='dg_config=( pri , std )'
*.log_archive_dest_1='location= /u01/app /arch/ pri valid_for=(all_logfiles,all_roles) db_unique_name= pri '
*.log_archive_dest_2='service= std valid_for=(online_logfiles,primary_role) db_unique_name= std '
*.log_archive_dest_state_1=enable
*.log_archive_dest_state_2=enable
*.log_archive_max_processes=4
*.fal_server=' std '
*.fal_client=' pri '
*.db_file_name_convert= ' /u01/app/oracle/oradata/std ' , ' /u01/app/oracle/oradata/pri '
*.log_file_name_convert= ' /u01/app/oracle/oradata/std ' , ' /u01/app/oracle/oradata/pri '
*.standby_file_management='auto'
修改完毕,保存退出
手工创建 /u01/app /arch/ pri :
[oracle@ pri dbs]$ mkdir /u01/app/arch/pri -pv
mkdir: created directory `/u01/app/arch'
mkdir: created directory `/u01/app/arch/pri'
5 :用修改过的 pfile 重新 创建一个spfile ,用于启动数据库 ( 此步骤只在主库上做 )
关闭数据库:
SYS@pri> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS@pri> create spfile from pfile;
此时把数据库改为归档模式: (如果当初建库时选择了启用归档,则此步骤忽略)
由于当前数据库已关闭,首先需要把数据库启动到mount 状态
SYS@pri> startup mount;
ORACLE instance started.
Database mounted.
SYS@pri> alter database archivelog; 启用归档模式
Database altered.
SYS@pri> alter database open; OPEN 数据库
Database altered.
SYS@pri> archive log list; 查看是否启用归档模式
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /u01/app/arch/pri
Oldest online log sequence 6
Next log sequence to archive 8
Current log sequence 8
如上,归档路径已经改为/u01/app/arch/pri ,证明对 pfile 的修改已生效
查看当前数据库是否使用spfile 启动:
SYS@pri> show parameter spfile;
NAME TYPE VALUE
----------- ----------- -----------------------------------------------------------------
spfile string /u01/app/oracle/product/11.2.0/db_1/dbs/spfilepri.ora
如上,若能看到spfile 的路径,则证明数据库是使用 spfile 启动的,若没有值,则说明是用 pfile 启动的 。
确认数据库已经启用归档模式和强制日志模式:
SYS@pri> select name,log_mode,force_logging from v$database;
NAME LOG_MODE FOR
--------- ------------------- -------
PRI ARCHIVELOG YES
5 :对主库进行 RMAN 全备
创建备份所需的目录并赋予相应权限: ( 主库和备库 端都 要 创建)
[root@ pri ~]# mkdir /rman_backup
[root@ pri ~]# chown oracle.oinstall /rman_backup/
[root@ std ~]# mkdir /rman_backup
[root@ std ~]# chown oracle.oinstall /rman_backup/
以oracle 用户身份登录,进行备份操作: (只在 主库 上做)
[root@ pri ~]# su - oracle
[oracle@ pri ~]$ rman target /
RMAN> backup database format '/rman_backup/Full_ %T _ %U.bak'; 备份数据库
Starting backup at 14-JAN-14
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=41 device type=DISK
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00001 name=/u01/app/oracle/oradata/pri/system01.dbf
input datafile file number=00002 name=/u01/app/oracle/oradata/pri/sysaux01.dbf
input datafile file number=00005 name=/u01/app/oracle/oradata/pri/example01.dbf
input datafile file number=00003 name=/u01/app/oracle/oradata/pri/undotbs01.dbf
input datafile file number=00004 name=/u01/app/oracle/oradata/pri/users01.dbf
channel ORA_DISK_1: starting piece 1 at 14-JAN-14
channel ORA_DISK_1: finished piece 1 at 14-JAN-14
piece handle=/rman_backup/Full_20140114_01ou2791_1_1.bak tag=TAG20140114T143721 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:45
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
including current SPFILE in backup set
channel ORA_DISK_1: starting piece 1 at 14-JAN-14
channel ORA_DISK_1: finished piece 1 at 14-JAN-14
piece handle=/rman_backup/Full_20140114_02ou27ae_1_1.bak tag=TAG20140114T143721 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 14-JAN-14
RMAN> list archivelog all; 查看当前的归档日志有哪些
List of Archived Log Copies for database with db_unique_name PRI
=====================================================================
Key Thrd Seq S Low Time
------- ---- ------- - ---------
1 1 6 A 13-JAN-14
Name: /u01/app/arch/pri/1_6_836760649.dbf
2 1 7 A 14-JAN-14
Name: /u01/app/arch/pri/1_7_836760649.dbf
RMAN> backup archivelog all format'/rman_backup/ARC_ %T _ %U.bak'; 备份归档日志
Starting backup at 14-JAN-14
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=6 RECID=1 STAMP=836835599
input archived log thread=1 sequence=7 RECID=2 STAMP=836835600
input archived log thread=1 sequence=8 RECID=3 STAMP=836836802
channel ORA_DISK_1: starting piece 1 at 14-JAN-14
channel ORA_DISK_1: finished piece 1 at 14-JAN-14
piece handle=/rman_backup/ARC_20140114_03ou27e3_1_1.bak tag=TAG20140114T144002 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 14-JAN-14
RMAN> backup current controlfile for standby format'/rman_backup/std_con.ctl';
备份standby 控制文件并命名为 std_con.ctl
Starting backup at 14-JAN-14
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including standby control file in backup set
channel ORA_DISK_1: starting piece 1 at 14-JAN-14
channel ORA_DISK_1: finished piece 1 at 14-JAN-14
piece handle=/rman_backup/std_con.ctl tag=TAG20140114T144036 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 14-JAN-14
RMAN> list backup; 查看都有哪些备份
List of Backup Sets
===================
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
1 Full 1.04G DISK 00:00:35 14-JAN-14
BP Key: 1 Status: AVAILABLE Compressed: NO Tag: TAG20140114T143721
Piece Name: /rman_backup/Full_20140114_01ou2791_1_1.bak
List of Datafiles in backup set 1
File LV Type Ckp SCN Ckp Time Name
---- -- ---- ---------- --------- ----
1 Full 1064545 14-JAN-14 /u01/app/oracle/oradata/pri/system01.dbf
2 Full 1064545 14-JAN-14 /u01/app/oracle/oradata/pri/sysaux01.dbf
3 Full 1064545 14-JAN-14 /u01/app/oracle/oradata/pri/undotbs01.dbf
4 Full 1064545 14-JAN-14 /u01/app/oracle/oradata/pri/users01.dbf
5 Full 1064545 14-JAN-14 /u01/app/oracle/oradata/pri/example01.dbf
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
2 Full 9.36M DISK 00:00:01 14-JAN-14
BP Key: 2 Status: AVAILABLE Compressed: NO Tag: TAG20140114T143721
Piece Name: /rman_backup/Full_20140114_02ou27ae_1_1.bak
SPFILE Included: Modification time: 14-JAN-14
SPFILE db_unique_name: PRI
Control File Included: Ckp SCN: 1064580 Ckp time: 14-JAN-14
BS Key Size Device Type Elapsed Time Completion Time
------- ---------- ----------- ------------ ---------------
3 16.06M DISK 00:00:00 14-JAN-14
BP Key: 3 Status: AVAILABLE Compressed: NO Tag: TAG20140114T144002
Piece Name: /rman_backup/ARC_20140114_03ou27e3_1_1.bak
List of Archived Logs in backup set 3
Thrd Seq Low SCN Low Time Next SCN Next Time
---- ------- ---------- --------- ---------- ---------
1 6 1051084 13-JAN-14 1062515 14-JAN-14
1 7 1062515 14-JAN-14 1062557 14-JAN-14
1 8 1062557 14-JAN-14 1064725 14-JAN-14
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
4 Full 9.33M DISK 00:00:02 14-JAN-14
BP Key: 4 Status: AVAILABLE Compressed: NO Tag: TAG20140114T144036
Piece Name: /rman_backup/std_con.ctl
Standby Control File Included: Ckp SCN: 1064792 Ckp time: 14-JAN-14
RMAN> quit
6 :修改监听文件,添加静态监听 ( 主库、 备库都要 做)
主库:
[oracle@ pri ~]$ cd $ORACLE_HOME/network/admin
[oracle@ pri admin]$ vim listener.ora 添加如下内容:
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = pri )
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1)
(SID_NAME = pri )
)
)
主 库修改后最终效果如下图:
备库:
[oracle@ pri ~]$ cd $ORACLE_HOME/network/admin
[oracle@ pri admin]$ vim listener.ora 添加如下内容:
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = std )
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1)
(SID_NAME = std )
)
)
备库修改后最终效果如下图:
使新增加的监听生效: (主库和备库端都要做)
[oracle@ pri admin]$ lsnrctl stop
[oracle@ pri admin]$ lsnrctl start
确认新增加的静态监听有效:
主库:
[oracle@pri ~]$ lsnrctl status
..........................................(N 行省略 )
Service "pri" has 2 instance(s).
Instance "pri" , status UNKNOWN , has 1 handler(s) for this service...
Instance "pri", status READY, has 1 handler(s) for this service...
Service "priXDB" has 1 instance(s).
Instance "pri", status READY, has 1 handler(s) for this service...
The command completed successfully
备库:
[ oracle @ std ~]$ lsnrctl status
..........................................(N 行省略 )
Service " std " has 1 instance(s).
Instance " std " , status UNKNOWN , has 1 handler(s) for this service...
The command completed successfully
如上,静态监听添加成功
7 :编辑网络服务名配置文件 tnsnames.ora (主库和备库端都要做)
[oracle@ pri admin]$ cd $ORACLE_HOME/network/admin
[oracle@ pri admin]$ pwd
/u01/app/oracle/product/11.2.0/db_1/network/admin
[oracle@ pri admin]$ ls
listener.ora samples shrept.lst tnsnames.ora
[oracle@ pri admin]$ vim tnsnames.ora
编辑结果如下图:
保证 主库和 备库的tnsnames.ora 文件中的内容完全相同, 可以把修改后的文件 直接传 给 备库。
[oracle@ pri admin]$ scp tnsnames.ora 192.168.1.102 :$ORACLE_HOME/network/admin
tnsnames.ora 100% 925 0.9KB/s 00:00
配置完后,确保在任意一 端 上都能tnsping 通 对方 :
[oracle@pri admin]$ tnsping std
[oracle@std admin]$ tnsping pri
8 :在备库上恢复主库之前的准备工作 (主库和备库端分别有各自的操作,仔细看)
首先把 主库的 pfile 参数文件拷贝到 /rman_backup/ 目录中去,并重命名:
[oracle@ pri dbs]$ cd $ORACLE_HOME/dbs
[oracle@ pri dbs]$ cp init pri .ora /rman_backup/ init std .ora
[oracle@ pri dbs]$ cd /rman_backup/
[oracle@pri rman_backup]$ ll -h
total 1.1G
-rw-r----- 1 oracle oinstall 17M Jan 14 14:40 ARC_20140114_03ou27e3_1_1.bak
-rw-r----- 1 oracle oinstall 1.1G Jan 14 14:37 Full_20140114_01ou2791_1_1.bak
-rw-r----- 1 oracle oinstall 9.4M Jan 14 14:38 Full_20140114_02ou27ae_1_1.bak
-rw-r--r-- 1 oracle oinstall 1.5K Jan 14 15:22 initstd.ora
-rw-r----- 1 oracle oinstall 9.4M Jan 14 14:40 std_con.ctl
然后把 主库上的 这些备份的文件拷贝到备库上的对应目录中去:
[oracle@pri rman_backup]$ scp * 192.168.1.102:/rman_backup/
[email protected]'s password:
ARC_20140114_03ou27e3_1_1.bak 100% 16MB 16.1MB/s 00:00
Full_20140114_01ou2791_1_1.bak 100% 1070MB 41.1MB/s 00:26
Full_20140114_02ou27ae_1_1.bak 100% 9600KB 9.4MB/s 00:00
initstd.ora 100% 1497 1.5KB/s 00:00
std_con.ctl 100% 9568KB 9.3MB/s 00:00
在 备库端 把pfile 参数文件拷贝到 $ORACLE_HOME/dbs 目录下:
[oracle@std admin]$ cd $ORACLE_HOME/dbs
[oracle@std dbs]$ ls
init.ora orapwstd
[oracle@std dbs]$ cp /rman_backup/initstd.ora .
[oracle@std dbs]$ ls
init.ora initstd.ora orapwstd
9 :备库修改 pfile 参数文件 (只在备库端做)
[oracle@std dbs]$ pwd
/u01/app/oracle/product/11.2.0/db_1/dbs
[oracle@std dbs]$ vim initstd.ora
std.__db_cache_size=125829120
std.__java_pool_size=4194304
std.__large_pool_size=4194304
std.__oracle_base='/u01/app/oracle'#ORACLE_BASE set from environment
std.__pga_aggregate_target=146800640
std.__sga_target=272629760
std.__shared_io_pool_size=0
std.__shared_pool_size=125829120
std.__streams_pool_size=4194304
*.audit_file_dest='/u01/app/oracle/admin/ std /adump'
*.audit_trail='db'
*.compatible='11.2.0.0.0'
*.control_files='/u01/app/oracle/oradata/ std / std_con .ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='pri' DG 主库和备库的 db_name 必须一致, db_unique_name 不一致
*.db_recovery_file_dest='/u01/app/oracle/fast_recovery_area'
*.db_recovery_file_dest_size=4227858432
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE= std XDB )'
*.memory_target=419430400
*.open_cursors=300
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.undo_tablespace='UNDOTBS1'
下面几行是需要手动添加的:
*.db_unique_name=' std '
*.log_archive_config='dg_config=( pri , std )'
*.log_archive_dest_1='location= /u01/app /arch/ std valid_for=(all_logfiles,all_roles) db_unique_name= std '
*.log_archive_dest_2='service= pri valid_for=(online_logfiles,primary_role) db_unique_name= pri '
*.log_archive_dest_state_1=enable
*.log_archive_dest_state_2=enable
*.log_archive_max_processes=4
*.fal_server=' pri '
*.fal_client=' std '
*.db_file_name_convert= ' /u01/app/oracle/oradata/pri ' , ' /u01/app/oracle/oradata/std '
*.log_file_name_convert= ' /u01/app/oracle/oradata/pri ' , ' /u01/app/oracle/oradata/std '
*.standby_file_management='auto'
修改完毕,保存退出
注意:整个搭建过程最需要留意的就是主库和备库的PFILE 配置,建议修改完后仔细对照主备库 PFILE 的区别
10 :在备库端手工创建所需的目录 (备库端做,不提前创建的话恢复时会报错!)
/u01/app/oracle/admin/ std /adump
/u01/app/oracle/diag/rdbms/ std / std/ trace
/u01/app /arch/ std
/u01/app/oracle/oradata/std
/u01/app/oracle/oradata/standbylog
创建 /u01/app/oracle/fast_recovery_area :
创建 /u01/app/oracle/admin/ std /adump :
[oracle@std dbs]$ cd /u01/app/oracle/
[oracle@std oracle]$ ls
cfgtoollogs checkpoints diag product (由于没建库,此时没有admin 目录)
[oracle@std oracle]$ mkdir -pv admin/std/adump
mkdir: created directory `admin'
mkdir: created directory `admin/std'
mkdir: created directory `admin/std/adump'
创建 /u01/app/oracle/diag/rdbms/ std / std/ trace :
[oracle@std oracle]$ cd /u01/app/oracle/diag/rdbms/
[oracle@std rdbms]$ ls (由于没建库,该目录下为空)
[oracle@std rdbms]$ mkdir -pv std/std/trace
mkdir: created directory `std'
mkdir: created directory `std/std'
mkdir: created directory `std/std/trace'
创建 /u01/app /arch/ std :
[oracle@std rdbms]$ cd /u01/app/
[oracle@std app]$ ls
oracle oraInventory
[oracle@std app]$ mkdir -pv arch/std
mkdir: created directory `arch'
mkdir: created directory `arch/std'
创建 /u01/app/oracle/oradata/std :
[oracle@std app]$ cd /u01/app/oracle/
[oracle@std oracle]$ ls
admin cfgtoollogs checkpoints diag product
[oracle@std oracle]$ mkdir -pv oradata/std
mkdir: created directory `oradata'
mkdir: created directory `oradata/std'
创建 /u01/app/oracle/oradata/standbylog :
[oracle@std oracle]$ cd /u01/app/oracle/oradata/
[oracle@std oradata]$ ls
std
[oracle@std oradata]$ mkdir -pv standbylog
mkdir: created directory `standbylog'
创建 /u01/app/oracle/fast_recovery_area :
[oracle@std dbs]$ cd /u01/app/oracle/
[oracle@std oracle]$ ls
admin cfgtoollogs checkpoints diag oradata product
[oracle@std oracle]$ mkdir -pv fast_recovery_area
mkdir: created directory `fast_recovery_area'
11 :用修改后的 pfile 创建一个 spfile ,用于启动数据库 (备库端做)
[oracle@std ~]$ sqlplus / as sysdba
Connected to an idle instance.
SYS@std> create spfile from pfile;
File created.
将数据库启动到nomount 状态:
SYS@pri> startup nomount;
ORACLE instance started.
Total System Global Area 839282688 bytes
Fixed Size 2233000 bytes
Variable Size 482348376 bytes
Database Buffers 352321536 bytes
Redo Buffers 2379776 bytes
SYS@pri>
12 :利用 RMAN 在备库上恢复主库 (备库端做)
[oracle@ std ~]$ rman target sys/oracle@ pri auxiliary /
Recovery Manager: Release 11.2.0.3 .0 - Production on Tue Jan 14 16:39:26 2014
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
connected to target database: PRI (DBID=767417863)
connected to auxiliary database: PRI (not mounted)
RMAN> duplicate target database for standby nofilenamecheck;
这条命令可以直接恢复数据文件,standby 控制文件, standby 日志组,非常霸道
Starting Duplicate Db at 14-JAN-14
using target database control file instead of recovery catalog
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=18 device type=DISK
contents of Memory Script:
{
restore clone standby controlfile;
}
executing Memory Script
Starting restore at 14-JAN-14
using channel ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: starting datafile backup set restore
channel ORA_AUX_DISK_1: restoring control file
channel ORA_AUX_DISK_1: reading from backup piece /rman_backup/std_con.ctl
channel ORA_AUX_DISK_1: piece handle=/rman_backup/std_con.ctl tag=TAG20140114T144036
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:01
output file name=/u01/app/oracle/oradata/std/std_con.ctl
Finished restore at 14-JAN-14
contents of Memory Script:
{
sql clone 'alter database mount standby database';
}
executing Memory Script
sql statement: alter database mount standby database
contents of Memory Script:
{
set newname for tempfile 1 to
"/u01/app/oracle/oradata/std/temp01.dbf";
switch clone tempfile all;
set newname for datafile 1 to
"/u01/app/oracle/oradata/std/system01.dbf";
set newname for datafile 2 to
"/u01/app/oracle/oradata/std/sysaux01.dbf";
set newname for datafile 3 to
"/u01/app/oracle/oradata/std/undotbs01.dbf";
set newname for datafile 4 to
"/u01/app/oracle/oradata/std/users01.dbf";
set newname for datafile 5 to
"/u01/app/oracle/oradata/std/example01.dbf";
restore
clone database
;
}
executing Memory Script
executing command: SET NEWNAME
renamed tempfile 1 to /u01/app/oracle/oradata/std/temp01.dbf in control file
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
Starting restore at 14-JAN-14
using channel ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: starting datafile backup set restore
channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_AUX_DISK_1: restoring datafile 00001 to /u01/app/oracle/oradata/std/system01.dbf
channel ORA_AUX_DISK_1: restoring datafile 00002 to /u01/app/oracle/oradata/std/sysaux01.dbf
channel ORA_AUX_DISK_1: restoring datafile 00003 to /u01/app/oracle/oradata/std/undotbs01.dbf
channel ORA_AUX_DISK_1: restoring datafile 00004 to /u01/app/oracle/oradata/std/users01.dbf
channel ORA_AUX_DISK_1: restoring datafile 00005 to /u01/app/oracle/oradata/std/example01.dbf
channel ORA_AUX_DISK_1: reading from backup piece /rman_backup/Full_20140114_01ou2791_1_1.bak
channel ORA_AUX_DISK_1: piece handle=/rman_backup/Full_20140114_01ou2791_1_1.bak tag=TAG20140114T143721
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:36
Finished restore at 14-JAN-14
contents of Memory Script:
{
switch clone datafile all;
}
executing Memory Script
datafile 1 switched to datafile copy
input datafile copy RECID=2 STAMP=836844042 file name=/u01/app/oracle/oradata/std/system01.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=3 STAMP=836844042 file name=/u01/app/oracle/oradata/std/sysaux01.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=4 STAMP=836844042 file name=/u01/app/oracle/oradata/std/undotbs01.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=5 STAMP=836844042 file name=/u01/app/oracle/oradata/std/users01.dbf
datafile 5 switched to datafile copy
input datafile copy RECID=6 STAMP=836844042 file name=/u01/app/oracle/oradata/std/example01.dbf
Finished Duplicate Db at 14-JAN-14
RMAN>
恢复数据库结束
13 :尝试开启备库
登陆并 查看数据库当前状态:
[oracle@std ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.3.0 Production on Tue Jan 14 16:41:50 2014
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SYS@std> select status from v$instance;
STATUS
------------
MOUNTED (RMAN 恢复完直接就是 mount 状态)
1 4 :备库启动日志应用
SYS@ std > alter database recover managed standby database disconnect from session;
Database altered.
( 停止日志应用的命令是:alter database recover managed standby database cancel; )
查看日志应用情况:
SYS@ std > set pagesize 100
SYS@ std > select sequence#,applied from v$archived_log order by 1;
SEQUENCE# APPLIED
---------- ---------
8 YES
9 YES
10 YES
如上, 如果 发现有个NO 的, 也 是正常的,说明该日志在主库上还没有归档,可以在主库上运行 alter system switch logfile ; 命令来进行日志切换,再到备库查看日志应用情况
15 :分别查看主库和备库的归档序列号是否一致:
先在主库手动切换一下日志:
SYS@pri> alter system switch logfile;
System altered.
然后查看主库:
SYS@pri> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /u01/app/arch/ pri
Oldest online log sequence 10
Next log sequence to archive 12
Current log sequence 12
备库:
SYS@std> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /u01/app/arch/ std
Oldest online log sequence 10
Next log sequence to archive 0
Current log sequence 12
结果完全一致, 至此,DataGuard 的搭建成功!
五:相关知识补充:
1 :DataGuard 的三种数据保护模式:
( 1 ) MAXIMIZE PROTECTION (最大保护模式):
最大数据保护与无数据分歧,LGWR 将同时传送到备用节点, 在主节点事务确认之前,备用节点也必须完全收到日志数据 。如果网络不好,引起LGWR 不能传送数据,将引起严重的性能问题,导致主节点 DOWN 机。
( 2 ) MAXIMIZE AVAILABILITY (最大可用模式) :
无数据丢失模式,允许数据分歧,允许异步传送。 正常情况下运行在最大保护模式,在主节点与备用节点的网络断开或连接不正常时,自动切换到最大性能模式,主节点的操作还是可以继续的。在网络不好的情况下有较大的性能影响。
( 3 ) MAXIMIZE PERFORMANCE (最大性能模式) :
这种模式应当可以说是从8i 继承过来的备用服务器模式, 异步传送,无数据同步检查,可能丢失数据 ,但是能获得主节点的最大性能。
2 :DataGuard 保护模式 切换 : (重点,注意升级切换和降级切换的区别)
( 1 ) 查看 当前的 保护模式:
select database_role, protection_mode, protection_level from v$database;
( 2 ) 三种保护模式的级别排名:
最大性能 --> 最大可用 --> 最大保护 ( 从左至右,保护模式级别提升 )
( 3 ) 升级切换步骤:
1. 备库停止 redo 应用:
alter database recover managed standby database cancel;
2. 主库重启 到mount 状态 : ( 升级切换,需要重启主库 )
shutdown immediate;
startup mount
3. 主库升级切换保护模式 并OPEN 数据库 : (升级切换是重点,仔细看)
重点:当要进行升级切换时,一定要先查看一下主库和备库的 LOG_ARCHIVE_DEST_ 2 参数, 若配置不正确,那么在OPEN 数据库时会报错 ORA-16072 !(尤其是要修改成最大保护模式时)下 表中描述了不同保护模式下LOG_ARCHIVE_DEST_ 2 参数应该设置的属性:
最大保护 |
最高可用 |
最高性能 |
|
REDO 写进程 |
LGWR |
LGWR |
LGWR 或 ARCH |
网络传输模式 |
SYNC |
SYNC |
LGWR 进程时 SYNC 或 ASYNC , ARCH 进程时 SYNC |
磁盘写操作 |
AFFIRM |
AFFIRM |
AFFIRM 或 NOAFFIRM |
是否需要 standby redologs |
YES |
YES |
可没有但推荐有 |
查看: show parameter log_archive_dest_2
NAME TYPE VALUE
------------------------ ---------- ----------------------------------------------------------------
log_archive_dest_2 string service=std valid_for=(online_logfiles,primary_role) db_unique_name=std
如上,并没有包含升级切换所需的参数,可以用下列两条命令修改:
主库修改: SYS@ pri > alter system set log_archive_dest_2='service= std lgwr sync affirm valid_for=(online_logfiles,primary_role) db_unique_name= std ';
备库修改: SYS@ std > alter system set log_archive_dest_2='service= pri lgwr sync affirm valid_for=(online_logfiles,primary_role) db_unique_name= pri ';
然后修改保护模式:
alter database set standby database to maximize availability;
(或者alter database set standby database to maximize protection; )
开启数据库:
alter database open;
4. 备库启动 redo 应用:
alter database recover managed standby database disconnect from session;
( 4 ) 降级切换步骤: (相对于升级切换,降级切换的步骤就简单的多了)
1. 备库停止 redo 应用:
alter database recover managed standby database cancel;
2. 主库调整保护模式:
alter database set standby database to maximize performance;
3. 备库启动 redo 应用:
alter database recover managed standby database disconnect from session;
3 : 举例: 将主库和备库的保护模式设置为最大 保护 模式(默认是最大性能模式)
1 . 查看当前的保护模式 : (在主库或者备库端查看都可以)
SYS@ pri > select database_role, protection_mode, protection_level from v$database;
DATABASE_ROLE PROTECTION_MODE PROTECTION_LEVEL
---------------------------- -------------------------------------- --------------------------------------
PHYSICAL STANDBY MAXIMUM PERFORMANCE MAXIMUM PERFORMANCE
2 . 备库停止 redo 应用:
S YS@std> alter database recover managed standby database cancel;
Database altered.
2. 主库重启 到mount 状态 : ( 由于是 升级切换, 所以 需要重启主库 )
SYS@pri> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS@pri> startup mount;
ORACLE instance started.
Total System Global Area 417546240 bytes
Fixed Size 2227072 bytes
Variable Size 331351168 bytes
Database Buffers 79691776 bytes
Redo Buffers 4276224 bytes
Database mounted.
3. 查看主库和备库的parameter log_archive_dest_2 参数设置是否正确 :
主库查看:
SYS@ pri > show parameter log_archive_dest_2
NAME TYPE VALUE
------------------------ ---------- ----------------------------------------------------------------
log_archive_dest_2 string service= std valid_for=(online_logfiles,primary_role) db_unique_name= std
备库查看:
SYS@ std > show parameter log_archive_dest_2
NAME TYPE VALUE
------------------------ ---------- ----------------------------------------------------------------
log_archive_dest_2 string service= pri valid_for=(online_logfiles,primary_role) db_unique_name= pri
如上,并没有包含升级切换所需的参数,可以用下列两条命令修改:
主库修改:
SYS@ pri > alter system set log_archive_dest_2='service= std lgwr sync affirm valid_for=(online_logfiles,primary_role) db_unique_name= std ';
备库修改:
SYS@ std > alter system set log_archive_dest_2='service= pri lgwr sync affirm valid_for=(online_logfiles,primary_role) db_unique_name= pri ';
( 当然,也可以在一开始搭建的时候,PFILE 里就这么写,具体情况看实际要求 )
主库再次查看:
SYS@ pri > show parameter log_archive_dest_2
NAME TYPE VALUE
------------------------ ---------- ----------------------------------------------------------------
log_archive_dest_2 string service= std lgwr sync affirm valid_for=(online_logfiles,prim
ary_role) db_unique_name= std
备库再次查看:
SYS@ std > show parameter log_archive_dest_2
NAME TYPE VALUE
------------------------ ---------- ----------------------------------------------------------------
log_archive_dest_2 string service= pri lgwr sync affirm valid_for=(online_logfiles,prim
ary_role) db_unique_name= pri
4 . 主库升级切换 到最大 保护模式 并OPEN 数据库 :
SYS@pri> alter database set standby database to maximize protection;
Database altered.
SYS@pri> alter database open;
Database altered.
5 . 备库启动 redo 应用:
SYS@std> alter database recover managed standby database disconnect from session;
Database altered.
查看主库的相关信息:
SYS@pri> select name,database_role,open_mode,protection_mode from v$database;
NAME DATABASE_ROLE OPEN_MODE PROTECTION_MODE
--------- ------------------------ -------------------- ------------------------------
PRI PRIMARY READ WRITE MAXIMUM PROTECTION
查看备库的相关信息:
SYS@ std > select name,database_role,open_mode,protection_mode from v$database;
NAME DATABASE_ROLE OPEN_MODE PROTECTION_MODE
--------- ------------------------ -------------------- ------------------------------
PRI PHYSICAL STANDBY MOUNTED MAXIMUM PROTECTION
可以看到,主库修改完保护模式,备库是自动跟着修改的,至于降级操作,参考上面的步骤即可,太简单就不做演示了。
5 : Oracle Data Guard 的开启关闭:
(1 ) Data Guard 主备库开关 顺序 :
启动:
1 : MOUNT 备库
2 : OPEN 主库
3 :备库开启日志应用 alter database recover managed standby database disconnect from session;
关 闭 :
1 :备库关闭日志应用 alter database recover managed standby database cancel;
2 :关闭主库
3 :关闭备库
(2 ) 在最大保护模式下,直接关闭备库是不行的,如果 直接关闭 ,会有如下提示:
SQL> shutdown immediate
ORA-01154: database busy. Open, close, mount, and dismount not allowed now
SQL>
在最大保护模式下,备库是不允许关闭的,此时首先关闭主库,然后备库就可以顺利关闭了。
6 : Oracle Data Guard 主备切换 :
(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 状态
SYS@pri> select name,database_role,switchover_status from v$database;
NAME DATABASE_ROLE SWITCHOVER_STATUS
--------- ------------------------ ---------------------------------
PRI PRIMARY 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 时, 说明切换标记还没收到,此时不能执行转换。
当主库备库状态都正常,并且没有进行主备切换的操作时,备库的 switchover_status 就是 not allowed 。当主库执行了切换成备库的操作后,备库的 switchover_status 才是 to primary 。
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 切换成备库
SYS@pri> a lter database commit to switchover to physical standby with session shutdown;
或者
SYS@pri> a lter database commit to switchover to physical standby ;
Database altered.
3 启动到 mount 和应用日志状态
SYS@pri> shutdown immediate;
若此时提示ORA-01092: ORACLE instance terminated. Disconnection forced ,则 quit 退出,重新 sqlplus 登陆数据库即可。
SYS@pri> startup nomount;
SYS@pri> alter database mount standby database;
SYS@pri> alter database recover managed standby database disconnect from session;
4. 查看数据库模式
SYS@pri> select name,database_role,switchover_status from v$database;
NAME DATABASE_ROLE SWITCHOVER_STATUS
--------- ----------------------------- ---------------------------------
PRI PHYSICAL STANDBY TO PRIMARY
如上,此时主库已经切换为物理备库
备库:
1. 查看 switchover 状态
SYS@std> select name,database_role,switchover_status from v$database;
NAME DATABASE_ROLE SWITCHOVER_STATUS
--------- ----------------------------- ---------------------------------
PRI PHYSICAL STANDBY TO PRIMARY
补充:若出现:ORA-16139: media recovery required
是因为没有执行:alter database recover managed standby database disconnect from session;
2. 切换成主库
SYS@std> alter database commit to switchover to primary;
Database altered.
SYS@std> shutdown immediate;
SYS@std> startup;
SYS@std> alter system switch logfile;
3. 查看数据库模式
SYS@std> select name,database_role,switchover_status from v$database;
NAME DATABASE_ROLE SWITCHOVER_STATUS
--------- ------------------------ ---------------------------------
PRI PRIMARY TO STANDBY
如上,物理备库已成功切换为主库
验证同步:
SYS@ pri > select max(sequence#) from v$archived_log;
MAX(SEQUENCE#)
---------------------------
40
SYS@ std > select max(sequence#) from v$archived_log;
MAX(SEQUENCE#)
---------------------------
40
(2 ) Failover :
FAILOVER 切换一般是 PRIMARY 数据库发生故障后 的切换,这种情况是 STANDBY 数据库发挥其作用 的情况。这种切换发生后, 可能会造成数据的丢失 。而且这个过程不是可逆的, DATA GUARD 环境会被破坏 。
由于PRIMARY 数据库已经无法启动,所以 FAILOVER 切换所需的条件并不多,只要 检查STANDBY 是否运行在最大保护模式下,如果是的话,需要将其置为最大性能模式,否则切换到 PRIMARY 角色也无法启动。
1. 查看 当前保护模式,确保当前保护模式为最大性能模式 :
SYS@std> select name,database_role,open_mode,protection_mode from v$database;
NAME DATABASE_ROLE OPEN_MODE PROTECTION_MODE
--------- ------------------------ -------------------- ------------------------------
PRI PHYSICAL STANDBY MOUNTED MAXIMUM PROTECTION
如上,当前是最大保护模式,修改为最大性能模式:
SYS@std> alter database set standby database to maximize performance;
Database altered.
再次查看,OK
SYS@std> select name,database_role,open_mode,protection_mode from v$database;
NAME DATABASE_ROLE OPEN_MODE PROTECTION_MODE
--------- ------------------------ -------------------- ------------------------------
PRI PHYSICAL STANDBY MOUNTED MAXIMUM PERFORMANCE
2 . 查看是否有日志 GAP, 没有应用的日志:
SYS@std> select unique thread#,max(sequence#) over (partition by thread#) last from v$archived_log;
THREAD# LAST
-------------- ----------
1 75
SYS@std> select thread#,low_sequence#,high_sequence# from v$archive_gap;
no rows selected
如上,no rows selected ,表示没有 GAP 。
如果有,则拷贝过来并且注册
SYS@ std > alter database register physical logfile ' 路径 ';
重复查看直到没有 未 应用的日志
3 . 然后停止应用归档:
SYS@std> alter database recover managed standby database cancel;
Database altered.
4 . 下面将 STANDBY 数据库切换为 PRIMARY 数据库:
取消日志应用:
SYS@std> alter database recover managed standby database finish;
这个finish 是 Failover 专用的,功能类似 cancel , 区别是 finish 破坏了主从结构 , 用了finish 就得重建 dg 。 所以模拟这个实验的时候,最好提前给虚拟机做个快照!
或 :
SYS@std> alter database recover managed standby database finish force;
切换为主库:
SYS@std> alter database commit to switchover to primary;
Database altered.
OPEN 主库:
SYS@std> alter database open;
Database altered.
或者 shutdown immediate+startup
5 : 检查数据库是否已经切换成功:
NAME DATABASE_ROLE OPEN_MODE PROTECTION_MODE
--------- ------------------------ -------------------- ------------------------------
PRI PRIMARY MOUNTED MAXIMUM PERFORMANCE
至此,FAILOVER 切换完成。这个时候应该马上对新的 PRIMARY 数据库进行备份。
About Me
........................................................................................................................ ● 本文作者:小麦苗,部分内容整理自网络,若有侵权请联系小麦苗删除 ● 本文在itpub、博客园、CSDN和个人微 信公众号( xiaomaimiaolhr)上有同步更新 ● 本文itpub地址: http://blog.itpub.net/26736162 ● 本文博客园地址: http://www.cnblogs.com/lhrbest ● 本文CSDN地址: https://blog.csdn.net/lihuarongaini ● 本文pdf版、个人简介及小麦苗云盘地址: http://blog.itpub.net/26736162/viewspace-1624453/ ● 数据库笔试面试题库及解答: http://blog.itpub.net/26736162/viewspace-2134706/ ● DBA宝典今日头条号地址: http://www.toutiao.com/c/user/6401772890/#mid=1564638659405826 ........................................................................................................................ ● QQ群号: 230161599 、618766405 ● 微 信群:可加我微 信,我拉大家进群,非诚勿扰 ● 联系我请加QQ好友 ( 646634621 ),注明添加缘由 ● 于 2019-08-01 06:00 ~ 2019-08-31 24:00 在西安完成 ● 最新修改时间:2019-08-01 06:00 ~ 2019-08-31 24:00 ● 文章内容来源于小麦苗的学习笔记,部分整理自网络,若有侵权或不当之处还请谅解 ● 版权所有,欢迎分享本文,转载请保留出处 ........................................................................................................................ ● 小麦苗的微店: https://weidian.com/s/793741433?wfr=c&ifr=shopdetail ● 小麦苗出版的数据库类丛书: http://blog.itpub.net/26736162/viewspace-2142121/ ● 小麦苗OCP、OCM、高可用网络班: http://blog.itpub.net/26736162/viewspace-2148098/ ● 小麦苗腾讯课堂主页: https://lhr.ke.qq.com/ ........................................................................................................................ 使用 微 信客户端扫描下面的二维码来关注小麦苗的微 信公众号( xiaomaimiaolhr)及QQ群(DBA宝典)、添加小麦苗微 信, 学习最实用的数据库技术。
........................................................................................................................ |
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26736162/viewspace-2655304/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/26736162/viewspace-2655304/