一、环境说明
主库:实例cc,db_unique_name=cc62,ip10.128.80.62
备库:实例cc,db_unique_name=cc63,ip10.128.80.63
操作系统平台:
主机:
[oracle@prora ~]$ cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
备机:
[oracle@drora cc]$ cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
二、环境准备
1、安装主库oracle12.2软件,并创建数据库。
注意:如果是手工建库,别忘了最后要跑脚本,否则rman连接主库的时候会报一些包不可用。
sqlplus / as sysdba
@?/rdbms/admin/catalog.sql 创建数据字典
@?/rdbms/admin/catproc.sql 创建存储过程和包
@?/rdbms/admin/catoctk.sql 创建需要使用PL/ SQL加密工具的接口
@?/rdbms/admin/owminst.plb 安装SQL*Plus的表PRODUCT_USER_PROFILE, 好像是与数据仓库有关的东西
@?/rdbms/admin/utlrp.sql 重新编译失效对象
conn system/oracle
@?/sqlplus/admin/pupbld.sql 不运行的话,普通用户无法登陆数据库
catblock.sql 创建oracle中锁机制的一些视图
2、安装备库的12.2的软件,不需要建库。
三、初始化参数
1、主库初始化参数
[oracle@prora dbs]$ cat initcc.ora
cc.__data_transfer_cache_size=0
cc.__db_cache_size=520093696
cc.__inmemory_ext_roarea=0
cc.__inmemory_ext_rwarea=0
cc.__java_pool_size=4194304
cc.__large_pool_size=8388608
cc.__oracle_base='/oracle'#ORACLE_BASE set from environment
cc.__pga_aggregate_target=268435456
cc.__sga_target=734003200
cc.__shared_io_pool_size=0
cc.__shared_pool_size=184549376
cc.__streams_pool_size=0
*.audit_file_dest='/oracle/admin/cc/adump'
*.audit_trail='db'
*.compatible='12.1.0'
*.control_files='/oradata/cc/control01.ctl','/oradata/cc/control02.ctl'
*.db_block_size=8192
*.DB_FILE_NAME_CONVERT='cc','cc'
*.db_name='cc'
*.db_securefile='PERMITTED'
*.DB_UNIQUE_NAME='cc62'
*.diagnostic_dest='/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=ccXDB)'
*.LOG_ARCHIVE_CONFIG='DG_CONFIG=(cc62,cc63)'
*.LOG_ARCHIVE_DEST_1='LOCATION=/oradata/cc/arch
VALID_FOR=(ALL_LOGFILES,ALL_ROLES)
DB_UNIQUE_NAME=cc62'
*.log_archive_dest_2='SERVICE=cc63 ASYNC
VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE)
DB_UNIQUE_NAME=cc63'
*.LOG_ARCHIVE_DEST_STATE_1='ENABLE'
*.LOG_ARCHIVE_DEST_STATE_2='ENABLE'
*.LOG_ARCHIVE_FORMAT='%t_%s_%r.arc'
*.LOG_ARCHIVE_MAX_PROCESSES=4
*.LOG_FILE_NAME_CONVERT='cc','cc'
*.nls_language='AMERICAN'
*.nls_territory='AMERICA'
*.open_cursors=300
*.pga_aggregate_target=256M
*.processes=100
*.REMOTE_LOGIN_PASSWORDFILE='EXCLUSIVE'
*.sga_max_size=700M
*.sga_target=700M
*.STANDBY_FILE_MANAGEMENT='AUTO'
*.undo_tablespace='UNDOTBS1'
注意飘红的是后续添加的,可以在建库的时候就按照这个参数添加。
如果主库在搭建从库的时候已经创建了,可以使用
alter system set parameter_name='' scope=spfile ;
设置。
2、从库初始化参数设置
[oracle@drora dbs]$ cat initcc.ora
cc.__data_transfer_cache_size=0
cc.__db_cache_size=520093696
cc.__inmemory_ext_roarea=0
cc.__inmemory_ext_rwarea=0
cc.__java_pool_size=4194304
cc.__large_pool_size=8388608
cc.__oracle_base='/oracle'#ORACLE_BASE set from environment
cc.__pga_aggregate_target=268435456
cc.__sga_target=734003200
cc.__shared_io_pool_size=0
cc.__shared_pool_size=184549376
cc.__streams_pool_size=0
*.audit_file_dest='/oracle/admin/cc/adump'
*.audit_trail='db'
*.compatible='12.1.0'
*.control_files='/oradata/cc/control01.ctl','/oradata/cc/control02.ctl'
*.db_block_size=8192
*.DB_FILE_NAME_CONVERT='cc','cc'
*.db_name='cc'
*.DB_UNIQUE_NAME='cc63'
*.diagnostic_dest='/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=ccXDB)'
*.LOG_ARCHIVE_CONFIG='DG_CONFIG=(cc63,cc62)'
*.LOG_ARCHIVE_DEST_1='LOCATION=/oradata/cc/arch
VALID_FOR=(ALL_LOGFILES,ALL_ROLES)
DB_UNIQUE_NAME=cc63'
*.LOG_ARCHIVE_DEST_2='SERVICE=cc62 ASYNC
VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE)
DB_UNIQUE_NAME=cc62'
*.LOG_ARCHIVE_DEST_STATE_1='ENABLE'
*.LOG_ARCHIVE_DEST_STATE_2='ENABLE'
*.LOG_ARCHIVE_FORMAT='%t_%s_%r.arc'
*.LOG_ARCHIVE_MAX_PROCESSES=4
*.LOG_FILE_NAME_CONVERT='cc','cc'
*.nls_language='AMERICAN'
*.nls_territory='AMERICA'
*.open_cursors=300
*.pga_aggregate_target=256M
*.processes=100
*.REMOTE_LOGIN_PASSWORDFILE='EXCLUSIVE'
*.sga_max_size=700M
*.sga_target=700M
*.STANDBY_FILE_MANAGEMENT='AUTO'
*.undo_tablespace='UNDOTBS1'
注意飘红的部分,其中注意db_name此次主备都是一样的cc,但是主备的db_unique_name必须主备不一样。
LOG_ARCHIVE_CONFIG,LOG_ARCHIVE_DEST_1,LOG_ARCHIVE_DEST_2三个参数主备不一样,注意不要写错了。
四、主备监听
1、主库
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = cc)
(GLOBAL_DBNAME = cc)
(ORACLE_HOME = /oracle/product/122)
)
)
LISTENER =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.128.80.62)(PORT = 1521))
)
2、备库
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = cc)
(GLOBAL_DBNAME = cc)
(ORACLE_HOME = /oracle/product/122)
)
)
LISTENER =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.128.80.63)(PORT = 1521))
)
3、主备库tns
cc62 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.128.80.62)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = cc)
)
)
cc63 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.128.80.63)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = cc)
)
)
注意主备库的tns配置一样的,都需要在主备库上配置主备两台机器的tns。
五、备库的nomount阶段
备库的nomount阶段是通过拷贝主库的参数实现的:
在主库上:
scp /oracle/product/122/dbs/initcc.ora oracle@备库:/oracle/product/122/dbs/
scp /oracle/product/122/dbs/orapwcc oracle@备库:/oracle/product/122/dbs/
然后在备库上修改主库上传过来的初始化参数文件,修改成3.2节中的内容。
然后就可以通过备库的初始化参数文件把备库启动到nomount阶段。
六、主库需要到open阶段
七、主备库的监听
按照第四节中的监听配置,启动主备库的监听。
lsnrctl start
lsnrctl status
需要分别验证是否可以使用监听登陆
1、主库验证(按照第四章第三节的tns配置)
sqlplus sys/oracle@cc62 as sysdba
sqlplus sys/oracle@cc63 as sysdba
2、备库验证(按照第四章第三节的tns配置)
sqlplus sys/oracle@cc62 as sysdba
sqlplus sys/oracle@cc63 as sysdba
确保上述在sys用户名密码正确的前提下能够正常登陆,否则后续rman复制会出问题。
八、确保以上内容均完成后
1、主库处于open状态,且监听起着,并能正常通过监听登陆。
2、备库处于nomount状态,且监听起着,并能正常通过监听登陆。
3、在主库上
[oracle@prora ~]$ rman target sys/oracle@cc62 auxiliary sys/oracle@cc63
RMAN> duplicate target database for standby from active database nofilenamecheck ;
九、上面第八步两个常见错误汇总:
1、12c登陆target库未加用户名密码
==================================================================================
[oracle@prora ~]$ rman target / auxiliary sys/oracle@cc63
Recovery Manager: Release 12.2.0.1.0 - Production on Sun May 5 21:57:02 2019
Copyright (c) 1982, 2017, Oracle and/or its affiliates. All rights reserved.
connected to target database: CC (DBID=2218976812)
connected to auxiliary database: CC (not mounted)
RMAN> duplicate target database for standby from active database;
Starting Duplicate Db at 05-MAY-19
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of Duplicate Db command at 05/05/2019 22:01:08
RMAN-05501: aborting duplication of target database
RMAN-05609: Must specify a username for target connection when using active duplicate
====================================================================================
对于12c主备连接的时候都要加用户名和密码,否则在执行duplicate target database for standby from active database;的时候就报上面的错
2、主库备库数据文件目录和名称都未变的时候未使用nofilenamecheck选项。
========================================================================================
RMAN> duplicate target database for standby from active database;
...
contents of Memory Script:
{
sql clone 'alter database mount standby database';
}
executing Memory Script
sql statement: alter database mount standby database
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of Duplicate Db command at 05/05/2019 22:02:21
RMAN-05501: aborting duplication of target database
RMAN-05001: auxiliary file name /oradata/cc/users01.dbf conflicts with a file used by the target database
RMAN-05001: auxiliary file name /oradata/cc/undotbs01.dbf conflicts with a file used by the target database
RMAN-05001: auxiliary file name /oradata/cc/sysaux01.dbf conflicts with a file used by the target database
RMAN-05001: auxiliary file name /oradata/cc/system01.dbf conflicts with a file used by the target database
==============================================================================================================
如果在RMAN恢复时不指定 nofilenamecheck 参数,则在数据文件相同文件名恢复时会出现RMAN-05501错误。
当主库,备库的数据库文件目录是一样的时候,必须使用 nofilenamecheck参数告诉rman主库和被创建的备份库拥有一样的文件目录和文件名。
十、第八步执行完后
1、备库在mount阶段
在第八步顺利执行完后,备库的状态应该是在mount阶段。
SQL> select database_role, open_mode from v$database;
DATABASE_ROLE OPEN_MODE
---------------- --------------------
PHYSICAL STANDBY MOUNTED
SQL> select status from v$instance;
STATUS
------------
MOUNTED
SQL>
2、此时主库状态
此时主库状态,处于OPEN,可读写,且在默认的最大性能模式:
SQL> select database_role, open_mode from v$database;
DATABASE_ROLE OPEN_MODE
---------------- --------------------
PRIMARY READ WRITE
SQL> select status from v$instance;
STATUS
------------
OPEN
SQL> select PROTECTION_MODE from v$database;
PROTECTION_MODE
--------------------
MAXIMUM PERFORMANCE
SQL>
十一、去备库继续操作
1、备库上
export ORACLE_SID=cc
sqlplus / as sysdba
SQL> recover managed standby database using current logfile disconnect from session;
Media recovery complete.
SQL> recover managed standby database cancel;
Media recovery complete.
SQL> alter database open read only;
Database altered.
SQL> select database_role, open_mode from v$database;
DATABASE_ROLE OPEN_MODE
---------------- --------------------
PHYSICAL STANDBY READ ONLY
其中disconnect from session表示后台执行,类似于shell的&。可以看出现在备库处于READ ONLY状态,不再是处于MOUNT不可使用的状态,即现在可以执行只读操作了。
SQL> recover managed standby database using current logfile disconnect from session;
Media recovery complete.
SQL> select database_role, open_mode from v$database;
DATABASE_ROLE OPEN_MODE
---------------- --------------------
PHYSICAL STANDBY READ ONLY WITH APPLY
其中disconnect from session表示后台执行,类似于shell的&。可以看出现在备库处于READ ONLY状态,不再是处于MOUNT不可使用的状态,即现在可以执行只读操作了。
十二、在十一步中的错误
SQL> select database_role, open_mode from v$database;
DATABASE_ROLE OPEN_MODE
---------------- --------------------
PHYSICAL STANDBY MOUNTED
SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-10458: standby database requires recovery
ORA-01152: file 1 was not restored from a sufficiently old backup
ORA-01110: data file 1: '/oradata/cc/system01.dbf'
在去备库执行启动到备库到open的状态时,报了上面的需要recovery。
后来检查是主库的log_archive_dest_2参数配置的有问题:
log_archive_dest_2 string SERVICE=cc63 ASYNC
VALID_FOR=(ONLINE_LOGFILES,P
RIMARY_ROLE)
DB_UNIQUE_NAME=cc63
service和db_unique_name要一致。之前service配置成cc了,跟实例的名字一样了。
同理,备库的参数也错了,也需要改:
log_archive_dest_2 string SERVICE=cc62 ASYNC
VALID_FOR=(ONLINE_LOGFILES,P
RIMARY_ROLE)
DB_UNIQUE_NAME=cc62
要改成service和db_unique_name一致即可。原来也是把service配置成了cc。
十三、关于归档
log_archive_dest_1 string LOCATION=/oradata/cc/arch
VALID_FOR=(ALL_LOGFILES,ALL_
ROLES)
DB_UNIQUE_NAME=cc62
在修改了log_archive_dest_1参数文件以后,归档就被认为是在此参数配置的位置里面了。
主备adg搭建完成后,归档均由此参数控制
SQL> archive log list
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /oradata/cc/arch
Oldest online log sequence 35
Next log sequence to archive 37
Current log sequence 37
SQL>
默认情况下,归档是在
SQL> show parameter recovery
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest string
db_recovery_file_dest_size big integer 0
recovery_parallelism integer 0
remote_recovery_file_dest string
SQL>
在不配置log_archive_log参数的情况下,是由快速恢复目录决定的。
详细步骤可以参见我之前写的文章:
https://blog.csdn.net/kadwf123/article/details/81509002
十四、备库的重启
1、方式一
SQL> shutdown immediate ;
SQL> startup
ORACLE instance started.
Total System Global Area 734003200 bytes
Fixed Size 8625032 bytes
Variable Size 239076472 bytes
Database Buffers 478150656 bytes
Redo Buffers 8151040 bytes
Database mounted.
Database opened.
SQL> select database_role, open_mode from v$database;
DATABASE_ROLE OPEN_MODE
---------------- --------------------
PHYSICAL STANDBY READ ONLY
SQL> recover managed standby database using current logfile disconnect from session;
Media recovery complete.
SQL> select database_role, open_mode from v$database;
DATABASE_ROLE OPEN_MODE
---------------- --------------------
PHYSICAL STANDBY READ ONLY WITH APPLY
2、方式二
SQL> shutdown immediate ;
SQL> startup nomount
SQL> alter database mount standby database ;
SQL> alter database open read only;
SQL> select database_role, open_mode from v$database;
DATABASE_ROLE OPEN_MODE
---------------- --------------------
PHYSICAL STANDBY READ ONLY
SQL> recover managed standby database using current logfile disconnect from session;
Media recovery complete.
SQL> select database_role, open_mode from v$database;
DATABASE_ROLE OPEN_MODE
---------------- --------------------
PHYSICAL STANDBY READ ONLY WITH APPLY
十五、首次搭建dg成功后,刚起来验证数据未同步
可以在主库上切换一次归档日志
SQL> alter system archive log current;
后续就可以完全在备库上实时查看到数据变化了。
十六、启停
1、启动
起主库-->起主库监听-->起备库-->起备库监听
2、停止
停备库-->停备库监听-->停主库-->停主库监听