欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Duplicate复制数据库并创建物理StandBy(spfile+不同实例名+不同路径)

程序员文章站 2022-06-21 16:09:06
过程和Duplicate复制数据库并创建物理StandBy类似,只是不需要重启数据库。 目的:创建standby,不重启源数据库 1设定环境如下: Primary数据库 IP 172.17.22.16 SID orcl Standby数据库 IP 172.17.22.17 SID orcl_stan ......

过程和duplicate复制数据库并创建物理standby类似,只是不需要重启数据库。

目的:创建standby,不重启源数据库

1设定环境如下:

primary数据库

ip 172.17.22.16
sid orcl

standby数据库

ip 172.17.22.17
sid orcl_standby

设置提示,以区分操作的位置

primary数据库

set sqlprompt primary>

standby数据库

set sqlprompt standby>

1、primary端设置 归档模式

确保primary数据库运行在归档模式

primary>archive log list
database log mode           no archive mode
automatic archival           disabled
archive destination           use_db_recovery_file_dest
oldest online log sequence     5
current log sequence           7
primary>shutdown immediate;
database closed.
database dismounted.
oracle instance shut down.
primary>startup mount
oracle instance started.

total system global area 3290345472 bytes
fixed size            2217832 bytes
variable size         1795164312 bytes
database buffers     1476395008 bytes
redo buffers           16568320 bytes
database mounted.
primary>alter database archivelog;

database altered.

primary>alter database open;

database altered.


primary>archive log list
database log mode           archive mode
automatic archival           enabled
archive destination           use_db_recovery_file_dest
oldest online log sequence     5
next log sequence to archive   7
current log sequence           7
primary>

2、primary端开启强制日志

primary>select force_logging from v$database;

for
---
no

primary>alter database force logging;

database altered.

关于force logging

想必大家知道有一些ddl语句可以通过指定nologging子句的方式避免写redo log(目的是提高速度,某些时候确实有效),指定数据库为force logging模式后,数据库将会记录除临时表空间或临时回滚段外所有的操作而忽略类似nologging之类的指定参数。如果在执行force logging时有nologging之类的语句在执行,则force logging会等待直到这类语句全部执行。force logging是做为固定参数保存在控制文件中,因此其不受重启之类操作的影响(只执行一次即可),如果想取消,可以通过alter database no force logging语句关闭强制记录。

3、standby端创建相关目录

为了和primary库保存相同的结构,我们需要在standby数据库建立相同的目录,首先查询现有primary数据库的相关目录

primary>col name for a30
primary>col value for a100
primary>select name ,value from v$parameter  where name in ('audit_file_dest','background_dump_dest','control_files','core_dump_dest','user_dump_dest') order by name asc;

name                   value
------------------------------ ---------------------------------------------------------------------------------------------
audit_file_dest /usr/oracle/app/admin/orcl/adump background_dump_dest /usr/oracle/app/diag/rdbms/orcl/orcl/trace control_files /usr/oracle/app/oradata/orcl/control01.ctl, /usr/oracle/app/flash_recovery_area/orcl/control02.ctl core_dump_dest /usr/oracle/app/diag/rdbms/orcl/orcl/cdump user_dump_dest /usr/oracle/app/diag/rdbms/orcl/orcl/trace

在standby数据库服务器创建如下的目录:

[oracle@oracledb ~]$ mkdir -p /usr/oracle/standby/app/admin/orcl_standby/adump
[oracle@oracledb ~]$ mkdir -p /usr/oracle/standby/app/diag/rdbms/orcl_standby/orcl_standby/trace
[oracle@oracledb ~]$ mkdir -p /usr/oracle/standby/app/oradata/orcl_standby
[oracle@oracledb ~]$ mkdir -p /usr/oracle/standby/app/flash_recovery_area/orcl_standby
[oracle@oracledb ~]$ mkdir -p /usr/oracle/standby/app/diag/rdbms/orcl_standby/orcl_standby/

3、创建辅助实例密钥文件

方法一: 直接从primary数据库复制密钥文件过来      (如果oracle_sid不同 需要改名)

[oracle@oracledb dbs]$ scp orapworcl 172.17.22.17:/usr/oracle/standby/app/product/11.2.0/dbhome_1/dbs
oracle@172.17.22.17's password: 
orapworcl                                     100% 1536     1.5kb/s   00:00    
[oracle@oracledb dbs]$

改名

[oracle@oracledb dbs]$ mv orapworcl  orapworcl_standby

方法二: orapwd生成

orapwd file=/usr/oracle/standby/app/product/11.2.0/dbhome_1/dbs/orapworcl password=wangshengzhuang entries=30;

4、修改primary端初始化参数,

需要配置如下的参数,参数说明如下

1、与主库角色相关的初始化参数说明:
db_name

注意保持同一个dataguard环境中所有数据库db_name相同

db_unique_name

为每一个数据库指定一个唯一的名称,以标示同一个dataguard环境中不同的数据库。

log_archive_config

该参数通过dg_config属性罗列同一个data guard中所有db_unique_name(含主库db及备库db),以逗号分隔。

例如:log_archive_config='db_config=(orcl,orcl2)'

log_archive_dest_n

归档文件的生成路径。该参数非常重要,dataguard就是通过这里的设置传输日志的。

log_archive_dest_state_n

指定参数值为enable,标示对应的log_archive_dest_n参数是否有效。

remote_login_passwordfile

推荐设置参数值为exclusive或者shared,注意保证相同data guard配置中所有db服务器sys密码相同。如果不同日志传输会失败。数据库默认是exclusive,一般不用修改。

log_archive_format

指定归档文件格式。一般也不用修改,保持默认即可

2、以下参数为备库角色相关的参数,建议在主库的初始化参数中也进行设置,这样在主备库角色相互转换后不需要做修改dataguard也能正常运行。

fal_server

指定备库到主数据库的连接服务名,fal_server = orcl2日志所在服务器。

fal_client

指定主库到备库的连接服务名,fal_client = orcl日志接收客户端。

standby_file_management

如果主库的数据文件发生修改(如新建,重命名等)则按照本参数的设置在备库中做相应修改。设为auto表示自动管理。设为manual表示需要手工管理。

例如:standby_file_management=auto

 

在primary端根据spfile生成pfile,并备份

primary>create pfile from spfile;

file created.

查询primary库的db_unique_name,(默认和db_name相同)

primary>show parameter db_unique_name;

name                     type            value
------------------------------------ ---------------------- ------------------------------
db_unique_name                 string            orcl

修改spfile参数值

-------为了不重启,沿用上面的db_unique_name   
--alter system set db_unique_name=orcl scope=spfile


----这里的orcl和orcl_standby为别为主库和备库的db_unique_name
alter system set log_archive_config='dg_config=(orcl,orcl_standby)' scope=spfile   


-----这里的service为主库连接到备库的服务名,后面会在tnsnames.ora文件中配置 valid_for参数说明这个归档日志目的地在本数据库为主库的角色下才需要把online_logfile传输到备库去。arch async noaffirm说明的是同步的方式,这个同步的方式有三种方式,最大保护,最大性能,最大可用。每个方式有不同的设置,具体见后面说明
alter system set log_archive_dest_2='service=tns_standby arch async noaffirm valid_for=(online_logfiles,primary_role) db_unique_name=orcl_standby'
----- 暂时不启用日志传送
alter system set log_archive_dest_state_2=defer


------以下修改的是作为备库角色需要的参数,为了方便以后主备库切换,建议在主库中也配置作为备库角色的相关参数。
alter system set fal_server=tns_standby
alter system set fal_client=tns_primary
alter system set standby_file_management=autoalter system set  db_file_name_convert='/usr/oracle/standby/app/oradata/orcl_standby/','/usr/oracle/app/oradata/orcl/' scope=spfile;alter system set log_file_name_convert='/usr/oracle/standby/app/oradata/orcl_standby/','/usr/oracle/app/oradata/orcl/' scope=spfile;

生成standby端的pfile

在primary端根据spfile重新生成pfile,用以启动standby数据库

primary>create pfile from spfile;

file created.

复制生成的initorcl.ora到standby的$oracle_home/dbs目录下,并改名(因为实例名不同)

 

[oracle@oracledb dbs]$ pwd
/usr/oracle/app/product/11.2.0/dbhome_1/dbs
[oracle@oracledb dbs]$ scp initorcl.ora 172.17.22.17:/usr/oracle/standby/app/product/11.2.0/dbhome_1/dbs
the authenticity of host '172.17.22.17 (172.17.22.17)' can't be established.
rsa key fingerprint is 72:28:f5:f9:9c:f8:49:23:48:6d:9d:d4:0e:0c:89:71.
are you sure you want to continue connecting (yes/no)? yes
warning: permanently added '172.17.22.17' (rsa) to the list of known hosts.
oracle@172.17.22.17's password: 
initorcl.ora                                  100% 1291     1.3kb/s   00:00    
[oracle@oracledb dbs]$

 

改名

[oracle@oracledb dbs]$ mv initorcl.ora  initorcl_standby.ora

修改standbyd端的initorcl_standby.ora文件,内容如下

------这里的实例名要由orcl改为orcl_standbyorcl_standby.__db_cache_size=1476395008
orcl_standby.__java_pool_size=16777216
orcl_standby.__large_pool_size=16777216
orcl_standby.__oracle_base='/usr/oracle/app'#oracle_base set from environment
orcl_standby.__pga_aggregate_target=1325400064
orcl_standby.__sga_target=1979711488
orcl_standby.__shared_io_pool_size=0
orcl_standby.__shared_pool_size=436207616
orcl_standby.__streams_pool_size=0
*.audit_file_dest='/usr/oracle/standby/app/admin/orcl_standby/adump'
*.audit_trail='db'
*.compatible='11.2.0.0.0'
*.control_files='/usr/oracle/standby/app/oradata/orcl_standby/control01.ctl','/usr/oracle/standby/app/flash_recovery_area/orcl_standby/control02.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='orcl'
*.db_recovery_file_dest='/usr/oracle/standby/app/flash_recovery_area'
*.db_recovery_file_dest_size=4070572032
*.diagnostic_dest='/usr/oracle/standby/app'
*.dispatchers='(protocol=tcp) (service=orclxdb)'
*.memory_target=3299868672
*.nls_language='simplified chinese'
*.nls_territory='china'
*.open_cursors=300
*.processes=150
*.remote_login_passwordfile='exclusive'
*.undo_tablespace='undotbs1'




*.db_unique_name=orcl_standby
*.log_archive_config='dg_config=(orcl,orcl_standby)'
*.log_archive_dest_2='service=tns_primary arch async noaffirm valid_for=(online_logfiles,primary_role) db_unique_name=orcl'  #arch表示归档日志
*.log_archive_dest_state_2=enable
------路径转换 *.db_file_name_convert= '/usr/oracle/app/oradata/orcl/','/usr/oracle/standby/app/oradata/orcl_standby/' *.log_file_name_convert='/usr/oracle/app/oradata/orcl/','/usr/oracle/standby/app/oradata/orcl_standby/' *.fal_server=tns_primary *.fal_client=tns_standby*.standby_file_management=auto

通过复制的pfile创建standby数据库的spfile

standby> create spfile from pfile;

file created.

启动到nomount环境

sql> startup nomount
oracle instance started.

total system global area 3290345472 bytes
fixed size            2217832 bytes
variable size         1795164312 bytes
database buffers     1476395008 bytes
redo buffers           16568320 bytes

5、配置监听服务

primary端监听(应该已经存在,大多数情况下不用重新配置)

# listener.ora network configuration file: /usr/oracle/app/product/11.2.0/dbhome_1/network/admin/listener.ora
# generated by oracle configuration tools.

listener =
  (description_list =
    (description =
      (address = (protocol = ipc)(key = extproc1521))
      (address = (protocol = tcp)(host = 172.17.22.16)(port = 1521))
    )
  )

adr_base_listener = /usr/oracle/app

standby端监听(配置了静态监听 服务名global_dbname = standby)

# listener.ora network configuration file: /usr/oracle/app/product/11.2.0/dbhome_1/network/admin/listener.ora
# generated by oracle configuration tools.



sid_list_listener =
  (sid_list =
    (sid_desc =
      (global_dbname = standby)
      (oracle_home =/usr/oracle/standby/app/product/11.2.0/dbhome_1)
      (sid_name = orcl_standby)
    )
  )

listener =
  (description_list =
    (description =
      (address = (protocol = ipc)(key = extproc1521))
      (address = (protocol = tcp)(host = 172.17.22.17)(port = 1521))
    )
  )

adr_base_listener = /usr/oracle/app

启动standby端监听

[oracle@oracledb admin]$ lsnrctl start

lsnrctl for linux: version 11.2.0.1.0 - production on 23-dec-2015 15:48:15

copyright (c) 1991, 2009, oracle.  all rights reserved.

starting /usr/oracle/standby/app/product/11.2.0/dbhome_1/bin/tnslsnr: please wait...

tnslsnr for linux: version 11.2.0.1.0 - production
system parameter file is /usr/oracle/standby/app/product/11.2.0/dbhome_1/network/admin/listener.ora
log messages written to /usr/oracle/standby/app/diag/tnslsnr/oracledb/listener/alert/log.xml
listening on: (description=(address=(protocol=ipc)(key=extproc1521)))
listening on: (description=(address=(protocol=tcp)(host=172.17.22.17)(port=1521)))

connecting to (description=(address=(protocol=ipc)(key=extproc1521)))
status of the listener
------------------------
alias                     listener
version                   tnslsnr for linux: version 11.2.0.1.0 - production
start date                23-dec-2015 15:48:16
uptime                    0 days 0 hr. 0 min. 0 sec
trace level               off
security                  on: local os authentication
snmp                      off
listener parameter file   /usr/oracle/standby/app/product/11.2.0/dbhome_1/network/admin/listener.ora
listener log file         /usr/oracle/standby/app/diag/tnslsnr/oracledb/listener/alert/log.xml
listening endpoints summary...
  (description=(address=(protocol=ipc)(key=extproc1521)))
  (description=(address=(protocol=tcp)(host=172.17.22.17)(port=1521)))
services summary...
service "standby" has 1 instance(s).
  instance "orcl", status unknown, has 1 handler(s) for this service...
the command completed successfully

6、配置网络服务名,并测试互通性

primary端和standby端都要进行如下配置:

tns_primary =
  (description =
    (address = (protocol = tcp)(host = 172.17.22.16)(port = 1521))
    (connect_data =
      (server = dedicated)
      (service_name = orcl)
    )
  )


tns_standby =
  (description =
    (address = (protocol = tcp)(host = 172.17.22.17)(port = 1521))
    (connect_data =
      (server = dedicated)
      (service_name =standby )
    )
  )

***********************************************************************************************

service_name 的值参考lsnrctl的输出,确保用sqlplus工具或者navicact工具能够连接service_name

因为fal_server 会使用服务名,如果配置不正确,这归档日志无法正常发送

***********************************************************************************************

在primary端和standby端都进行测试(为了角色切换)

[oracle@oracledb admin]$ tnsping tns_primary

[oracle@oracledb admin]$ tnsping tns_standby

7、duplicate standby

rman连接两个数据库

[oracle@oracledb admin]$ rman target sys/wangshengzhuang@tns_primary auxiliary sys/wangshengzhuang@tns_standby

recovery manager: release 11.2.0.1.0 - production on wed dec 23 16:11:51 2015

copyright (c) 1982, 2009, oracle and/or its affiliates.  all rights reserved.

connected to target database: orcl (dbid=1426832466)
connected to auxiliary database: orcl (not mounted)

开始复制

rman> duplicate target database for standby from active database dorecover nofilenamecheck;

starting duplicate db at 23-dec-15
using target database control file instead of recovery catalog
allocated channel: ora_aux_disk_1
channel ora_aux_disk_1: sid=20 device type=disk
rman-00571: ===========================================================
rman-00569: =============== error message stack follows ===============
rman-00571: ===========================================================
rman-03002: failure of duplicate db command at 12/23/2015 16:13:38
rman-05541: no archived logs found in target database

primary端切换下日志,quit重新执行rman duplicate即可

[oracle@oracledb ~]$ rman target sys/wangshengzhuang@tns_primary auxiliary sys/wangshengzhuang@tns_standby 

recovery manager: release 11.2.0.1.0 - production on wed dec 30 16:56:13 2015

copyright (c) 1982, 2009, oracle and/or its affiliates.  all rights reserved.

connected to target database: orcl (dbid=1426832466)
connected to auxiliary database: orcl (not mounted)

rman> duplicate target database for standby from active database dorecover nofilenamecheck;

starting duplicate db at 30-dec-15
using target database control file instead of recovery catalog
allocated channel: ora_aux_disk_1
channel ora_aux_disk_1: sid=20 device type=disk

contents of memory script:
{
   backup as copy reuse
   targetfile  '/usr/oracle/app/product/11.2.0/dbhome_1/dbs/orapworcl' auxiliary format 
 '/usr/oracle/standby/app/product/11.2.0/dbhome_1/dbs/orapworcl_standby'   ;
}
executing memory script

starting backup at 30-dec-15
allocated channel: ora_disk_1
channel ora_disk_1: sid=35 device type=disk
finished backup at 30-dec-15

contents of memory script:
{
   backup as copy current controlfile for standby auxiliary format  '/usr/oracle/standby/app/oradata/orcl_standby/control01.ctl';
   restore clone controlfile to  '/usr/oracle/standby/app/flash_recovery_area/orcl_standby/control02.ctl' from 
 '/usr/oracle/standby/app/oradata/orcl_standby/control01.ctl';
}
executing memory script

starting backup at 30-dec-15
using channel ora_disk_1
channel ora_disk_1: starting datafile copy
copying standby control file
output file name=/usr/oracle/app/product/11.2.0/dbhome_1/dbs/snapcf_orcl.f tag=tag20151230t165618 recid=4 stamp=899830578
channel ora_disk_1: datafile copy complete, elapsed time: 00:00:01
finished backup at 30-dec-15

starting restore at 30-dec-15
using channel ora_aux_disk_1

channel ora_aux_disk_1: copied control file copy
finished restore at 30-dec-15

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 
 "/usr/oracle/standby/app/oradata/orcl_standby/temp01.dbf";
   switch clone tempfile all;
   set newname for datafile  1 to 
 "/usr/oracle/standby/app/oradata/orcl_standby/system01.dbf";
   set newname for datafile  2 to 
 "/usr/oracle/standby/app/oradata/orcl_standby/sysaux01.dbf";
   set newname for datafile  3 to 
 "/usr/oracle/standby/app/oradata/orcl_standby/undotbs01.dbf";
   set newname for datafile  4 to 
 "/usr/oracle/standby/app/oradata/orcl_standby/users01.dbf";
   backup as copy reuse
   datafile  1 auxiliary format 
 "/usr/oracle/standby/app/oradata/orcl_standby/system01.dbf"   datafile 
auxiliary format 
 "/usr/oracle/standby/app/oradata/orcl_standby/sysaux01.dbf"   datafile 
auxiliary format 
 "/usr/oracle/standby/app/oradata/orcl_standby/undotbs01.dbf"   datafile 
auxiliary format 
 "/usr/oracle/standby/app/oradata/orcl_standby/users01.dbf"   ;
   sql 'alter system archive log current';
}
executing memory script

executing command: set newname

renamed tempfile 1 to /usr/oracle/standby/app/oradata/orcl_standby/temp01.dbf in control file

executing command: set newname

executing command: set newname

executing command: set newname

executing command: set newname

starting backup at 30-dec-15
using channel ora_disk_1
channel ora_disk_1: starting datafile copy
input datafile file number=00001 name=/usr/oracle/app/oradata/orcl/system01.dbf
output file name=/usr/oracle/standby/app/oradata/orcl_standby/system01.dbf tag=tag20151230t165625
channel ora_disk_1: datafile copy complete, elapsed time: 00:00:15
channel ora_disk_1: starting datafile copy
input datafile file number=00002 name=/usr/oracle/app/oradata/orcl/sysaux01.dbf
output file name=/usr/oracle/standby/app/oradata/orcl_standby/sysaux01.dbf tag=tag20151230t165625
channel ora_disk_1: datafile copy complete, elapsed time: 00:00:16
channel ora_disk_1: starting datafile copy
input datafile file number=00003 name=/usr/oracle/app/oradata/orcl/undotbs01.dbf
output file name=/usr/oracle/standby/app/oradata/orcl_standby/undotbs01.dbf tag=tag20151230t165625
channel ora_disk_1: datafile copy complete, elapsed time: 00:00:03
channel ora_disk_1: starting datafile copy
input datafile file number=00004 name=/usr/oracle/app/oradata/orcl/users01.dbf
output file name=/usr/oracle/standby/app/oradata/orcl_standby/users01.dbf tag=tag20151230t165625
channel ora_disk_1: datafile copy complete, elapsed time: 00:00:01
finished backup at 30-dec-15

sql statement: alter system archive log current

contents of memory script:
{
   backup as copy reuse
   archivelog like  "/usr/oracle/app/flash_recovery_area/orcl/archivelog/2015_12_30/o1_mf_1_8_c8772wkh_.arc" auxiliary format 
 "/usr/oracle/standby/app/flash_recovery_area/orcl_standby/archivelog/2015_12_30/o1_mf_1_8_%u_.arc"   ;
   catalog clone recovery area;
   switch clone datafile all;
}
executing memory script

starting backup at 30-dec-15
using channel ora_disk_1
channel ora_disk_1: starting archived log copy
input archived log thread=1 sequence=8 recid=2 stamp=899830620
output file name=/usr/oracle/standby/app/flash_recovery_area/orcl_standby/archivelog/2015_12_30/o1_mf_1_8_0lqq4kqs_.arc recid=0 stamp=0
channel ora_disk_1: archived log copy complete, elapsed time: 00:00:01
finished backup at 30-dec-15

searching for all files in the recovery area

list of files unknown to the database
=====================================
file name: /usr/oracle/standby/app/flash_recovery_area/orcl_standby/archivelog/2015_12_30/o1_mf_1_8_0lqq4kqs_.arc
cataloging files...
cataloging done

list of cataloged files
=======================
file name: /usr/oracle/standby/app/flash_recovery_area/orcl_standby/archivelog/2015_12_30/o1_mf_1_8_0lqq4kqs_.arc

datafile 1 switched to datafile copy
input datafile copy recid=4 stamp=899830621 file name=/usr/oracle/standby/app/oradata/orcl_standby/system01.dbf
datafile 2 switched to datafile copy
input datafile copy recid=5 stamp=899830621 file name=/usr/oracle/standby/app/oradata/orcl_standby/sysaux01.dbf
datafile 3 switched to datafile copy
input datafile copy recid=6 stamp=899830621 file name=/usr/oracle/standby/app/oradata/orcl_standby/undotbs01.dbf
datafile 4 switched to datafile copy
input datafile copy recid=7 stamp=899830621 file name=/usr/oracle/standby/app/oradata/orcl_standby/users01.dbf

contents of memory script:
{
   set until scn  1027736;
   recover
   standby
   clone database
    delete archivelog
   ;
}
executing memory script

executing command: set until clause

starting recover at 30-dec-15
using channel ora_aux_disk_1

starting media recovery

archived log for thread 1 with sequence 8 is already on disk as file /usr/oracle/standby/app/flash_recovery_area/orcl_standby/archivelog/2015_12_30/o1_mf_1_8_0lqq4kqs_.arc
archived log file name=/usr/oracle/standby/app/flash_recovery_area/orcl_standby/archivelog/2015_12_30/o1_mf_1_8_0lqq4kqs_.arc thread=1 sequence=8
media recovery complete, elapsed time: 00:00:00
finished recover at 30-dec-15
finished duplicate db at 30-dec-15

8、启用日志传送

primary>show parameter log_archive_dest_state_2

name                     type            value
------------------------------------ ---------------------- ------------------------------
log_archive_dest_state_2         string            defer
log_archive_dest_state_20         string            enable
log_archive_dest_state_21         string            enable
log_archive_dest_state_22         string            enable
log_archive_dest_state_23         string            enable
log_archive_dest_state_24         string            enable
log_archive_dest_state_25         string            enable
log_archive_dest_state_26         string            enable
log_archive_dest_state_27         string            enable
log_archive_dest_state_28         string            enable
log_archive_dest_state_29         string            enable
primary>alter system set log_archive_dest_state_2=enable;

system altered.

8、验证结果

查询primary数据库角色

primary>select database_role from v$database;

database_role
--------------------------------
primary

查询standby数据库角色

 

standby>select database_role from v$database;

database_role
--------------------------------
physical standby

standby>

primary端插入一条数据

primary> insert into "scott"."dept" ("deptno", "dname", "loc") values ('12', 'operations', 'operations');
row created.

primary>commit;

commit complete.

primary>alter system switch logfile;

system altered.

standby端:启动redo应用、暂停redo应用 打开数据库,查询数据是否被同步过来了

standby>alter database recover managed standby database disconnect from session;

database altered.

standby>alter database recover managed standby database cancel;

database altered.

standby>alter database open;

database altered.

standby>select open_mode from v$database;

open_mode
----------------------------------------
read only

standby>

见证奇迹的时刻:

standby>select * from scott.dept;

    deptno dname            loc
---------- ---------------------------- --------------------------
accounting            new york
research            dallas
sales            chicago
operations            boston
operations            operations

9、启用实时应用redo

添加redo log

首先查询当前redo log的大小、位置

primary>col group# for 9
primary>col status for a10
primary>col type for a10
primary>col member for a50;
primary>col is_rec for a10
primary>select * from v$logfile;

group# status      type         member                        is_rec
------ ---------- ---------- -------------------------------------------------- ------
         online     /usr/oracle/app/oradata/orcl/redo03.log        no
         online     /usr/oracle/app/oradata/orcl/redo02.log        no
         online     /usr/oracle/app/oradata/orcl/redo01.log        no

primary>select * from v$log;

group#      thread#  sequence#      bytes  blocksize    members archiv status    first_change# first_time   next_change# next_time
------ ---------- ---------- ---------- ---------- ---------- ------ ---------- ------------- ------------ ------------ ------------
       1      10   52428800        512        1 no     current          1029234 23-dec-15      2.8147e+14
       1       8   52428800        512        1 yes    inactive          1028340 23-dec-15     1028421 23-dec-15
       1       9   52428800        512        1 yes    inactive          1028421 23-dec-15     1029234 23-dec-15

standby redo log的文件大小与primary数据库online redo log文件大小相同。

standby redo log的组数最好比主库online redo log多。

当前主库有三组、每组1个member、大小为50m, 我们在备库增加四组,每组1个member,大小为50m

 

standby>alter database add standby  logfile group 4('/usr/oracle/standby/app/oradata/orcl_standby/stbyredolog4a.log') size  50 m;

database altered.

standby>alter database add standby logfile group 5('/usr/oracle/standby/app/oradata/orcl_standby/stbyredolog5a.log') size  50 m;

database altered.

standby>alter database add standby logfile group 6('/usr/oracle/standby/app/oradata/orcl_standby/stbyredolog6a.log') size  50 m;

database altered.

standby>alter database add standby logfile group 7('/usr/oracle/standby/app/oradata/orcl_standby/stbyredolog7a.log') size  50 m;

database altered.

查询备库当前的日志

standby>select group#, type, member from v$logfile;

group# type      member
-----------------------------------------------------------------------------------------------------------------------------     3 online      /usr/oracle/standby/app/oradata/orcl_standby/redo03.log
online      /usr/oracle/standby/app/oradata/orcl_standby/redo02.log
online      /usr/oracle/standby/app/oradata/orcl_standby/redo01.log
standby      /usr/oracle/standby/app/oradata/orcl_standby/stbyredolog4a.log
standby      /usr/oracle/standby/app/oradata/orcl_standby/stbyredolog5a.log
standby      /usr/oracle/standby/app/oradata/orcl_standby/stbyredolog6a.log
standby      /usr/oracle/standby/app/oradata/orcl_standby/stbyredolog7a.log

为了switchover,我们在主库也增加4组standby redo log

primary>alter database add standby   logfile group 4('/usr/oracle/app/oradata/orcl/stbyredolog4a.log') size  50 m;

database altered.

primary>alter database add standby   logfile group 5('/usr/oracle/app/oradata/orcl/stbyredolog5a.log') size  50 m;

database altered.

primary>alter database add standby   logfile group 6('/usr/oracle/app/oradata/orcl/stbyredolog6a.log') size  50 m;

database altered.

primary>alter database add standby   logfile group 7('/usr/oracle/app/oradata/orcl/stbyredolog7a.log') size  50 m;

database altered.

查看结果:

primary>select * from v$logfile;

group# status      type         member                        is_rec
------ ---------- ---------- -------------------------------------------------- ------
         online     /usr/oracle/app/oradata/orcl/redo03.log        no
         online     /usr/oracle/app/oradata/orcl/redo02.log        no
         online     /usr/oracle/app/oradata/orcl/redo01.log        no
         standby    /usr/oracle/app/oradata/orcl/stbyredolog4a.log    no
         standby    /usr/oracle/app/oradata/orcl/stbyredolog5a.log    no
         standby    /usr/oracle/app/oradata/orcl/stbyredolog6a.log    no
         standby    /usr/oracle/app/oradata/orcl/stbyredolog7a.log    no
rows selected.

更改primary和standby端的log_archive_dest_2、

 

更改primary端的log_archive_dest_2

primary>alter system set log_archive_dest_2='service=tns_standby lgwr async noaffirm valid_for=(online_logfiles,primary_role) db_unique_name=orcl_standby';

system altered.

更改standby端的log_archive_dest_2

standby>alter system set log_archive_dest_2='service=tns_primary lgwr async noaffirm valid_for=(online_logfiles,primary_role) db_unique_name=orcl';

system altered.

启用实时standby端redo应用

standby>alter database recover managed standby database using current logfile  disconnect from session;

验证:

首先在primay端插入一条数据:

primary>select * from scott.dept;

    deptno dname            loc
---------- ---------------------------- --------------------------
accounting            new york
research            dallas
sales            chicago
operations            boston
operations            operations

primary>insert into "scott"."dept" ("deptno", "dname", "loc") values ('13', 'operations', 'operations');
row created.

primary>commit;

commit complete.

standby端查看

standby>select * from scott.dept;
    deptno dname            loc
---------- ---------------------------- --------------------------
accounting            new york
research            dallas
sales            chicago
operations            boston
operations            operations
operations            operations
rows selected.

10、switchover

备库暂停 redo应用

standby>alter database recover managed standby database cancel;

database altered.

查询主库是否支持switchover操作

primary> select switchover_status from v$database;

switchover_status
----------------------------------------
to standby

查询备库是否支持switchover操作

standby> select switchover_status from v$database;

switchover_status
----------------------------------------
not allowed

not allowed是因为主库还未变切换为standby

switchover,primary切换为物理standby,切换后查看数据库角色、打开模式、

primary>alter database commit to switchover to physical standby;

database altered.

primary>shutdown immediate;
ora-01507: database not mounted


oracle instance shut down.
primary>startup
oracle instance started.

total system global area 3290345472 bytes
fixed size            2217832 bytes
variable size         1795164312 bytes
database buffers     1476395008 bytes
redo buffers           16568320 bytes
database mounted.
database opened.
primary>select open_mode  from v$database;

open_mode
----------------------------------------
read only

primary>select database_role from v$database;

database_role
--------------------------------
physical standby

primary>

如果报错ora-01093: alter database close only permitted with no sessions connected,关闭活动的连接即可

primary>alter database commit to switchover to physical standby;
alter database commit to switchover to physical standby
*
error at line 1:
ora-01093: alter database close only permitted with no sessions connected

物理standby切换为primary

sql> select switchover_status from v$database;

switchover_status
----------------------------------------
switchover pending

如果是switchover pending,说明当前standby数据库没有启动redo应用,重新执行下面的命令即可

alter database recover managed standby database using current logfile  disconnect from session;

如果是session active 说明当前有用户连接到standby数据库,建议先断开这些连接,或者先关闭数据库,启动到mount状态(出现上面那个状态是你用sqlplus连接着主库,启动到mount状态就正常啦 )

sql> shutdown immediate;
database closed.
database dismounted.
oracle instance shut down.
sql> startup mount;
oracle instance started.

total system global area 3290345472 bytes
fixed size            2217832 bytes
variable size         1795164312 bytes
database buffers     1476395008 bytes
redo buffers           16568320 bytes
database mounted.
sql> select switchover_status from v$database;

switchover_status
----------------------------------------
to primary

切换

standby>select switchover_status from v$database;

switchover_status
----------------------------------------
to primary

standby> alter database commit to switchover to primary;

database altered.

standby>alter database open;

database altered.

standby>select open_mode from v$database;

open_mode
----------------------------------------
read write

standby>select database_role from v$database;

database_role
--------------------------------
primary

standby>

测试同上(插入删除),这里只进行简单测试

现在的primay切换日志

sql> select max(sequence#) from v$archived_log;

max(sequence#)
--------------

sql> alter system switch logfile;

system altered.

在现在的standby查询

sys@orcl>select max(sequence#) from v$archived_log;

max(sequence#)
--------------
        15

 

常用查询

1、查询进程的活动状态

select process,client_process,sequence#,status from v$managed_standby;

2、查询redo应用进度

select dest_name,archived_thread#,archived_seq#,applied_thread#,applied_seq#,db_unique_name from v$archive_dest_status where status ='valid';

3、查询归档文件路径及创建信息

select name,creator,sequence#,applied,completion_time from v$archived_log;

4、查询归档历史

select first_time,first_change#,next_change#,sequence# from v$log_history;

select thread#,sequence#,applied from v$archived_log;

5、参看数据库的基本信息

select database_role,db_unique_name,open_mode,protection_mode,protection_level,switchover_status from v$database;

6、查询redo应用及redo传输服务的活动状态

select database_role,db_unique_name,open_mode,protection_mode,protection_level,switchover_status from v$database;

7、检查应用模式

standby>select recovery_mode from v$archive_dest_status where dest_id=2;

recovery_mode
----------------------------------------------
idle

取值

idle

managed:

managed real_time_apply:

8、在主库上执行只主要是查看日志归档目的地是否可用,如果远程归档目录不可用则error会显示错误信息

primary>select dest_name,status,error from v$archive_dest;

dest_name          status     error
------------------------- ---------- ----------------------------------------------------------------------------------------------------
log_archive_dest_1      valid
log_archive_dest_2      valid
log_archive_dest_3      inactive
log_archive_dest_4      inactive
log_archive_dest_5      inactive
log_archive_dest_6      inactive
log_archive_dest_7      inactive
log_archive_dest_8      inactive
log_archive_dest_9      inactive
log_archive_dest_10      inactive
log_archive_dest_11      inactive
log_archive_dest_12      inactive
log_archive_dest_13      inactive
log_archive_dest_14      inactive
log_archive_dest_15      inactive
log_archive_dest_16      inactive
log_archive_dest_17      inactive
log_archive_dest_18      inactive
log_archive_dest_19      inactive
log_archive_dest_20      inactive
log_archive_dest_21      inactive
log_archive_dest_22      inactive
log_archive_dest_23      inactive
log_archive_dest_24      inactive
log_archive_dest_25      inactive
log_archive_dest_26      inactive
log_archive_dest_27      inactive
log_archive_dest_28      inactive
log_archive_dest_29      inactive
log_archive_dest_30      inactive
log_archive_dest_31      inactive
standby_archive_dest      valid
rows selected.

9、查询归档日志的应用情况。主备库上查询结果不同,在主库上对于每个归档文件会有两条记录。

10、查询dataguard状态信息

standby>select message_num,message from v$dataguard_status;

message_num message
----------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
arc0: archival started
arc1: archival started
arc2: archival started
arc1: becoming the 'no fal' arch
arc2: becoming the heartbeat arch
arc3: archival started
rfs[1]: assigned to rfs process 5881
rfs[1]: identified database type as 'physical standby': client is arch pid 9099
rfs[2]: assigned to rfs process 5892
rfs[2]: identified database type as 'physical standby': client is arch pid 9099
rfs[3]: assigned to rfs process 5897
rfs[3]: identified database type as 'physical standby': client is arch pid 9101
attempt to start background managed standby recovery process
mrp0: background managed standby recovery process started
managed standby recovery not using real time apply
media recovery log /usr/oracle/standby/app/flash_recovery_area/orcl_standby/archivelog/2015_12_30/o1_mf_1_25_c87mn8sw_.arc
media recovery waiting for thread 1 sequence 26
rfs[4]: assigned to rfs process 5906
rfs[4]: identified database type as 'physical standby': client is arch pid 9099
rfs[5]: assigned to rfs process 5911
rfs[5]: identified database type as 'physical standby': client is arch pid 9099
rfs[6]: assigned to rfs process 5918
rfs[6]: identified database type as 'physical standby': client is arch pid 9099
rfs[7]: assigned to rfs process 5925
rfs[7]: identified database type as 'physical standby': client is arch pid 9099
rfs[8]: assigned to rfs process 5932
rfs[8]: identified database type as 'physical standby': client is arch pid 9099
rfs[9]: assigned to rfs process 5938
rfs[9]: identified database type as 'physical standby': client is arch pid 9099
rfs[10]: assigned to rfs process 5945
rfs[10]: identified database type as 'physical standby': client is arch pid 9099
rfs[11]: assigned to rfs process 5952
rfs[11]: identified database type as 'physical standby': client is arch pid 9099
rfs[12]: assigned to rfs process 5959
rfs[12]: identified database type as 'physical standby': client is arch pid 9099
rfs[13]: assigned to rfs process 5970
rfs[13]: identified database type as 'physical standby': client is arch pid 9099
rfs[14]: assigned to rfs process 5976
rfs[14]: identified database type as 'physical standby': client is arch pid 9099
rfs[15]: assigned to rfs process 5984
rfs[15]: identified database type as 'physical standby': client is arch pid 9099
rfs[16]: assigned to rfs process 5991
rfs[16]: identified database type as 'physical standby': client is arch pid 9099
rfs[17]: assigned to rfs process 6006
rfs[17]: identified database type as 'physical standby': client is arch pid 9099
rfs[18]: assigned to rfs process 6018
rfs[18]: identified database type as 'physical standby': client is arch pid 9099
rfs[19]: assigned to rfs process 6024
rfs[19]: identified database type as 'physical standby': client is arch pid 9099
rfs[20]: assigned to rfs process 6035
rfs[20]: identified database type as 'physical standby': client is arch pid 9099
rfs[21]: assigned to rfs process 6047
rfs[21]: identified database type as 'physical standby': client is arch pid 9099
rfs[22]: assigned to rfs process 6059
rfs[22]: identified database type as 'physical standby': client is arch pid 9099
rfs[23]: assigned to rfs process 6070
rfs[23]: identified database type as 'physical standby': client is arch pid 9099
rfs[24]: assigned to rfs process 6076
rfs[24]: identified database type as 'physical standby': client is arch pid 9099
rfs[25]: assigned to rfs process 6082
rfs[25]: identified database type as 'physical standby': client is arch pid 9099
rfs[26]: assigned to rfs process 6090
rfs[26]: identified database type as 'physical standby': client is arch pid 9099
rfs[27]: assigned to rfs process 6097
rfs[27]: identified database type as 'physical standby': client is arch pid 9099
rfs[28]: assigned to rfs process 6104
rfs[28]: identified database type as 'physical standby': client is arch pid 9099
rfs[29]: assigned to rfs process 6110
rfs[29]: identified database type as 'physical standby': client is arch pid 9099
rfs[30]: assigned to rfs process 6116
rfs[30]: identified database type as 'physical standby': client is arch pid 9099
rfs[31]: assigned to rfs process 6123
rfs[31]: identified database type as 'physical standby': client is arch pid 9099
rfs[32]: assigned to rfs process 6133
rfs[32]: identified database type as 'physical standby': client is arch pid 9099
rfs[33]: assigned to rfs process 6144
rfs[33]: identified database type as 'physical standby': client is arch pid 9099
rfs[34]: assigned to rfs process 6161
rfs[34]: identified database type as 'physical standby': client is arch pid 9099
rfs[35]: assigned to rfs process 6167
rfs[35]: identified database type as 'physical standby': client is arch pid 9099
rfs[36]: assigned to rfs process 6174
rfs[36]: identified database type as 'physical standby': client is arch pid 9099
rfs[37]: assigned to rfs process 6181
rfs[37]: identified database type as 'physical standby': client is arch pid 9099
rfs[38]: assigned to rfs process 6188
rfs[38]: identified database type as 'physical standby': client is arch pid 9099
rfs[39]: assigned to rfs process 6201
rfs[39]: identified database type as 'physical standby': client is arch pid 9099
rfs[40]: assigned to rfs process 6207
rfs[40]: identified database type as 'physical standby': client is arch pid 9099
rfs[41]: assigned to rfs process 6214
rfs[41]: identified database type as 'physical standby': client is arch pid 9099
rfs[42]: assigned to rfs process 6221
rfs[42]: identified database type as 'physical standby': client is arch pid 9099
rfs[43]: assigned to rfs process 6232
rfs[43]: identified database type as 'physical standby': client is arch pid 9099
rfs[44]: assigned to rfs process 6238
rfs[44]: identified database type as 'physical standby': client is arch pid 9099
rfs[45]: assigned to rfs process 6244
rfs[45]: identified database type as 'physical standby': client is arch pid 9099
mrp0: background media recovery cancelled with status 16037
mrp0: background media recovery process shutdown
managed standby recovery canceled
rfs[46]: assigned to rfs process 6253
rfs[46]: identified database type as 'physical standby': client is arch pid 9099
rfs[47]: assigned to rfs process 6263
rfs[47]: identified database type as 'physical standby': client is arch pid 9099
rfs[48]: assigned to rfs process 6270
rfs[48]: identified database type as 'physical standby': client is arch pid 9099
rfs[49]: assigned to rfs process 6276
rfs[49]: identified database type as 'physical standby': client is arch pid 9099
rfs[50]: assigned to rfs process 6284
rfs[50]: identified database type as 'physical standby': client is arch pid 9099
rfs[51]: assigned to rfs process 6295
rfs[51]: identified database type as 'physical standby': client is arch pid 9099
rfs[52]: assigned to rfs process 6301
rfs[52]: identified database type as 'physical standby': client is arch pid 9099
rfs[53]: assigned to rfs process 6312
rfs[53]: identified database type as 'physical standby': client is arch pid 9099
rfs[54]: assigned to rfs process 6320
rfs[54]: identified database type as 'physical standby': client is arch pid 9099
rfs[55]: assigned to rfs process 6325
rfs[55]: identified database type as 'physical standby': client is arch pid 9099
rfs[56]: assigned to rfs process 6332
rfs[56]: identified database type as 'physical standby': client is lgwr async pid 10167
primary database is in maximum performance mode
arc0: beginning to archive thread 1 sequence 26 (1039145-1040585)
rfs[57]: assigned to rfs process 6334
rfs[57]: identified database type as 'physical standby': client is lgwr async pid 10167
arc0: completed archiving thread 1 sequence 26 (0-0)
primary database is in maximum performance mode
rfs[58]: assigned to rfs process 6339
rfs[58]: identified database type as 'physical standby': client is arch pid 9099
rfs[59]: assigned to rfs process 6356
rfs[59]: identified database type as 'physical standby': client is arch pid 9099
attempt to start background managed standby recovery process
mrp0: background managed standby recovery process started
managed standby recovery starting real time apply
media recovery log /usr/oracle/standby/app/flash_recovery_area/orcl_standby/archivelog/2015_12_30/o1_mf_1_26_c87po9r6_.arc
media recovery waiting for thread 1 sequence 27 (in transit)
rfs[60]: assigned to rfs process 6364
rfs[60]: identified database type as 'physical standby': client is arch pid 9099
mrp0: background media recovery cancelled with status 16037
managed standby recovery not using real time apply
mrp0: background media recovery process shutdown
managed standby recovery canceled
rfs[61]: assigned to rfs process 6373
rfs[61]: identified database type as 'physical standby': client is arch pid 9099
arc1: beginning to archive thread 1 sequence 27 (1040585-1040839)
arc1: completed archiving thread 1 sequence 27 (0-0)
rfs[62]: assigned to rfs process 6378
rfs[62]: identified database type as 'physical standby': client is arch pid 9099
rfs[63]: assigned to rfs process 6382
rfs[63]: identified database type as 'physical standby': client is foreground pid 9102
arc3: beginning to archive thread 1 sequence 28 (1040839-1040863)
arc3: completed archiving thread 1 sequence 28 (0-0)
rfs[64]: assigned to rfs process 6386
rfs[64]: identified database type as 'physical standby': client is arch pid 9099
rfs[65]: assigned to rfs process 6393
rfs[65]: identified database type as 'physical standby': client is arch pid 9095
arc0: beginning to archive thread 1 sequence 29 (1040863-1040945)
arc0: completed archiving thread 1 sequence 29 (0-0)
rfs[66]: assigned to rfs process 6395
rfs[66]: identified database type as 'physical standby': client is arch pid 9099
rfs[67]: assigned to rfs process 6400
rfs[67]: identified database type as 'physical standby': client is foreground pid 9102
arc1: beginning to archive thread 1 sequence 30 (1040945-1040959)
arc1: completed archiving thread 1 sequence 30 (0-0)
rfs[68]: assigned to rfs process 6404
rfs[68]: identified database type as 'physical standby': client is foreground pid 9102
rfs[69]: assigned to rfs process 6406
rfs[69]: identified database type as 'physical standby': client is foreground pid 9102
attempt to start background managed standby recovery process
mrp0: background managed standby recovery process started
managed standby recovery starting real time apply
media recovery log /usr/oracle/standby/app/flash_recovery_area/orcl_standby/archivelog/2015_12_30/o1_mf_1_27_c87pwj5t_.arc
media recovery log /usr/oracle/standby/app/flash_recovery_area/orcl_standby/archivelog/2015_12_30/o1_mf_1_28_c87px4hz_.arc
media recovery log /usr/oracle/standby/app/flash_recovery_area/orcl_standby/archivelog/2015_12_30/o1_mf_1_29_c87pzfbv_.arc
media recovery log /usr/oracle/standby/app/flash_recovery_area/orcl_standby/archivelog/2015_12_30/o1_mf_1_30_c87pzr9s_.arc
media recovery log /usr/oracle/standby/app/flash_recovery_area/orcl_standby/archivelog/2015_12_30/o1_mf_1_31_c87pzsk7_.arc
resetting standby activation id 1427590416 (0x55174d10)
mrp0: media recovery complete: end-of-redo
mrp0: background media recovery process shutdown
alter database switchover to primary
arc0: becoming the 'no srl' arch
arc0: becoming the 'no srl' arch
arc1: becoming the 'no srl' arch
rows selected.

11、查看是否有日志缺失

standby>select * from v$archive_gap;

no rows selected

主备库的起停和维护

dataguard关闭(先关主库再关备库)

dataguard开启(先开备库再开主库)

failover切换方法

(1)判断主数据库确实出现严重的硬件故障或其他原因导致主数据库无法启动。

(2)在物理备用数据库上检查是否有archive redo log gaps

standby>select thread#, low_sequence#, high_sequence# from v$archive_gap;

no rows selected

如果有则消除archive redo log gaps

从主数据库上或其他备份的地方把没有传到物理备用数据库的archive redo log传到物理备用数据库上,并注册到物理备用数据库的controlfile中。

sql> alter database register physical logfile 'archive redo log文件名称';

重复2,3步骤直到v$archive_gap视图无记录存在。

(3)查看归档文件是否完整

主库备库分别执行下面语句,取得各个线程已经归档的文件最大的序号,如果最大序号不同,则必须将多出的序号对应的归档文件复制到待转换的standby服务器(也有可能primary库已经无法打开了)

sql> select distinct  thread#, max(sequence#)  over  (partition by  thread#) from v$archived_log;

   thread#   max(sequence#)over(partitionbythread#)
---------- --------------------------------------
                      84

sql> select max(sequence#) from v$archived_log;

max(sequence#)
--------------
        84

(4)在物理备用数据库上发起failover操作

sql > alter database recover managed standby database finish force;

(5)把物理备用数据库转化成主用角色

sql> alter database commit to switchover to primary;

(6)把新的主用数据库重新启动

sql> shutdown immediate;

sql> startup;

(7)对新的主用数据库做全备份.