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

Duplicate复制数据库并创建物理StandBy(pfile版本)

程序员文章站 2022-06-14 23:41:54
1设定环境如下: Primary数据库 IP 172.17.22.16 SID orcl Standby数据库 IP 172.17.22.17 SID orcl 设置提示,以区分操作的位置 primary数据库 set SQLPROMPT Primary> standby数据库 set SQLPRO ......

1设定环境如下:

primary数据库

ip 172.17.22.16
sid orcl

standby数据库

ip 172.17.22.17
sid orcl

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

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>

开启强制日志

primary>select force_logging from v$database;

for
---
no

primary>alter database force logging;

database altered.

2、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/app/admin/orcl/adump
[oracle@oracledb ~]$ mkdir -p /usr/oracle/app/diag/rdbms/orcl/orcl/trace
[oracle@oracledb ~]$ mkdir -p /usr/oracle/app/oradata/orcl
[oracle@oracledb ~]$ mkdir -p /usr/oracle/app/flash_recovery_area/orcl
[oracle@oracledb ~]$ mkdir -p /usr/oracle/app/diag/rdbms/orcl/orcl/
[oracle@oracledb ~]$ mkdir -p /usr/oracle/app/diag/rdbms/orcl/orcl/trace

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

方法一: 直接从primary数据库复制密钥文件过来

[oracle@oracledb dbs]$ scp orapworcl 172.17.22.17:/usr/oracle/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]$

方法二: orapwd生成

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

4、生成standby端的pfile

在primary端根据spfile生产pfile

primary>create pfile from spfile;

file created.

修改primary端的pfile内容如下

orcl.__db_cache_size=1476395008
orcl.__java_pool_size=16777216
orcl.__large_pool_size=16777216
orcl.__oracle_base='/usr/oracle/app'#oracle_base set from environment
orcl.__pga_aggregate_target=1325400064
orcl.__sga_target=1979711488
orcl.__shared_io_pool_size=0
orcl.__shared_pool_size=436207616
orcl.__streams_pool_size=0
*.audit_file_dest='/usr/oracle/app/admin/orcl/adump'
*.audit_trail='db'
*.compatible='11.2.0.0.0'
*.control_files='/usr/oracle/app/oradata/orcl/control01.ctl','/usr/oracle/app/flash_recovery_area/orcl/control02.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='orcl'
*.db_recovery_file_dest='/usr/oracle/app/flash_recovery_area'
*.db_recovery_file_dest_size=4070572032
*.diagnostic_dest='/usr/oracle/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=db_primary
*.log_archive_config='dg_config=(db_primary,db_standby)'
*.log_archive_dest_2='service=tns_standby arch valid_for=(online_logfiles,primary_role) db_unique_name=db_standby' #arch表示同步已归档的日志
*.log_archive_dest_state_2=defer #表示归档目的地暂时不可用


*.fal_server=tns_standby
*.fal_client=tns_primary
*.standby_file_management=auto

primary端重新启动

primary>create spfile from pfile;

file created.

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>show parameter fal

name                     type            value
------------------------------------ ---------------------- ------------------------------
fal_client                 string            tns_primary  #tns_primary为主服务器的网络服务名
fal_server                 string            tns_standby  

复制生成的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/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]$

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

orcl.__db_cache_size=1476395008
orcl.__java_pool_size=16777216
orcl.__large_pool_size=16777216
orcl.__oracle_base='/usr/oracle/app'#oracle_base set from environment
orcl.__pga_aggregate_target=1325400064
orcl.__sga_target=1979711488
orcl.__shared_io_pool_size=0
orcl.__shared_pool_size=436207616
orcl.__streams_pool_size=0
*.audit_file_dest='/usr/oracle/app/admin/orcl/adump'
*.audit_trail='db'
*.compatible='11.2.0.0.0'
*.control_files='/usr/oracle/app/oradata/orcl/control01.ctl','/usr/oracle/app/flash_recovery_area/orcl/control02.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='orcl'
*.db_recovery_file_dest='/usr/oracle/app/flash_recovery_area'
*.db_recovery_file_dest_size=4070572032
*.diagnostic_dest='/usr/oracle/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=db_standby
*.log_archive_config='dg_config=(db_primary,db_standby)'
*.log_archive_dest_2='service=tns_primary arch valid_for=(online_logfiles,primary_role) db_unique_name=db_primary'  #arch为已归档日志
*.log_archive_dest_state_2=enable  #备用的连接到主的可以开启,因为此时主同步日志到备未启用


*.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.


#红色字体为静态监听,后面duplicate需要用到静态监听注册
sid_list_listener =
  (sid_list =
    (sid_desc =
      (global_dbname = standby)
      (oracle_home =/usr/oracle/app/product/11.2.0/dbhome_1)
      (sid_name = orcl)
    )
  )

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/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/app/product/11.2.0/dbhome_1/network/admin/listener.ora
log messages written to /usr/oracle/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/app/product/11.2.0/dbhome_1/network/admin/listener.ora
listener log file         /usr/oracle/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 = db_primary)
    )
  )


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

tns ping utility for linux: version 11.2.0.1.0 - production on 23-dec-2015 15:48:44

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

used parameter files:
/usr/oracle/app/product/11.2.0/dbhome_1/network/admin/sqlnet.ora


used tnsnames adapter to resolve the alias
attempting to contact (description = (address = (protocol = tcp)(host = 172.17.22.16)(port = 1521)) (connect_data = (server = dedicated) (service_name = orcl)))
ok (0 msec)
[oracle@oracledb admin]$ tnsping tns_standby

tns ping utility for linux: version 11.2.0.1.0 - production on 23-dec-2015 15:48:49

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

used parameter files:
/usr/oracle/app/product/11.2.0/dbhome_1/network/admin/sqlnet.ora


used tnsnames adapter to resolve the alias
attempting to contact (description = (address = (protocol = tcp)(host = 172.17.22.17)(port = 1521)) (connect_data = (server = dedicated) (service_name = orcl)))
ok (0 msec)

7、duplicate standby

rman连接两个数据库

[oracle@oracledb admin]$ rman target sys/primary_password@tns_primary auxiliary sys/standby_password@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端切换下日志,重写执行rman duplicate即可

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; #standby端应用redo

database altered.

standby>alter database recover managed standby database cancel; #取消应用redo

database altered.

standby>alter database open;  #打开数据库

database altered.

standby>select open_mode from v$database;   #standby是只读状态

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

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

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.

同理在standby端做相同的操作

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

database altered.

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

database altered.

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

database altered.

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

database altered.
standby>select * from v$logfile;

group# status      type         member                                                  is_rec
------ ---------- ---------- ---------------------------------------------------------------------------------------------------- ------
         online     /usr/oracle/app/flash_recovery_area/db_standby/onlinelog/o1_mf_3_c7np1p97_.log              yes
         online     /usr/oracle/app/flash_recovery_area/db_standby/onlinelog/o1_mf_2_c7np1osm_.log              yes
         online     /usr/oracle/app/flash_recovery_area/db_standby/onlinelog/o1_mf_1_c7np1od8_.log              yes
         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

更改primary和standby端的log_archive_dest_2

primary>show parameter log_archive_dest_2

name                     type    value
------------------------------------ ---------- ------------------------------
log_archive_dest_2             string    service=tns_standby arch valid
                        _for=(online_logfiles,primary_
                        role) db_unique_name=db_standb
                        y
log_archive_dest_20             string
log_archive_dest_21             string
log_archive_dest_22             string
log_archive_dest_23             string
log_archive_dest_24             string
log_archive_dest_25             string
log_archive_dest_26             string
log_archive_dest_27             string
log_archive_dest_28             string
log_archive_dest_29             string
primary>alter system set log_archive_dest_2='service=tns_standby lgwr valid_for=(online_logfiles,primary_role) db_unique_name=db_standby'; #以前此处为arch, system altered. primary>show parameter log_archive_dest_2 name type value ------------------------------------ ---------- ------------------------------ log_archive_dest_2 string service=tns_standby lgwr valid _for=(online_logfiles,primary_ role) db_unique_name=db_standb y log_archive_dest_20 string log_archive_dest_21 string log_archive_dest_22 string log_archive_dest_23 string log_archive_dest_24 string log_archive_dest_25 string log_archive_dest_26 string log_archive_dest_27 string log_archive_dest_28 string log_archive_dest_29 string primary>

更改standby端的log_archive_dest_2

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

启用实时standby端redo应用

standby>alter database recover managed standby database using current logfile  disconnect from session;   #因为更改成了lgwr来传递,所以需要使用实时

验证:

首先在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; #取消redo应用

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;  #将当前primary角色切换到物理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>

物理standby切换为primary

standby>select switchover_status from v$database;

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

standby> alter database commit to switchover to primary;   #standby切换到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>

常用查询

 

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: