本实施方案主要对Oracle DataGuard实施部署作相应的说明。以便实施人员能根据当前业务特点,规划、建设符合高可用、高可靠的数据库集群系统。具体由Oracle DG环境拓扑、Oracle单机数据库规划部分构成!
数据库服务器采用DATAGUARD灾备模式,可以满足对可用性有特殊需求的应用,具备以下特点:
1、 需要冗余的服务器设备。该模式需要有冗余的服务器硬件。硬件成本较高。
2、 需要冗余的存储设备。主机和备机都需要同样的存储空间,成本较高。
3、 安装配置比较复杂。该模式比单节点、单实例的模式配置复杂一些,需要更多的配置步骤。
4、 管理维护成本高。该模式对维护人员的要求较高,维护成本高。
5、 具备一定的容灾特性。当主机整个数据库系统不可用并短期内无法恢复时,可以把数据库系统切换到备机上,具备容灾的功能。
6、 备机可以用作只读查询。备机可以切换到只读状态供报表之类的查询操作,减轻主机的压力。
在主机1上安装数据库软件,并建监听和实例,在主机2上安装数据库软件,并建监听,但不建实例。
主机1(主库) | 主机2(备库) | |
---|---|---|
操作系统 | Centos7.6 64位 | Centos7.6 64位 |
主机名 | davyprimary | davystandbyIP |
地址 | 192.168.1.140 | 192.168.1.141 |
数据库软件版本 | oracle 11.2.0.4 | oracle 11.2.0.4 |
ORACLE_BASE | /u01/app/oracle | /u01/app/oracle |
ORACLE_HOME | /u01/app/oracle/product/11.2.0.4/dbhome_1 | /u01/app/oracle/product/11.2.0.4/dbhome_1 |
ORACLE_SID | LHR11G | LHR11GS |
闪回区 | 开启 | |
归档 | 开启 |
主备库均为归档模式,并且force logging
-- 查看数据库是否运行在归档模式:
SYS@LHR11G> archive log list;
备注:如果数据库已经开启归档,下面的操纵可以忽略。
SYS@LHR11G> shutdown immediate #关闭数据库
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS@LHR11G> startup mount #启动到mount状态
ORACLE instance started.
Total System Global Area 325685248 bytes
Fixed Size 2252944 bytes
Variable Size 171970416 bytes
Database Buffers 146800640 bytes
Redo Buffers 4661248 bytes
Database mounted.
SYS@LHR11G> alter database archivelog; #开启归档
Database altered.
SYS@LHR11G> alter database open; #open数据库
Database altered.
SYS@LHR11G> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 19
Next log sequence to archive 21
Current log sequence 21
SYS@LHR11G> select flashback_on from v$database;
FLASHBACK_ON
NO
备注:如果数据库已经开启flashback,那么下面步骤可忽略。如上显示,该数据库未开启flashback,可按下面方法开启。
[root@oracleprimary /]# mkdir -p /data/LHR11G/recoverydest #创建闪回去路径
目录
[root@oracleprimary data]# chown oracle:dba /data/LHR11G/recoverydest/
SYS@LHR11G> alter system set db_recovery_file_dest='/data/LHR11G/recoverydest'; #设置闪回路径
System altered.
SYS@LHR11G> alter system set db_recovery_file_dest_size='5G';
System altered.
SYS@LHR11G> shutdown immediate; #关闭数据库
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS@LHR11G> startup mount;
ORACLE instance started.
Total System Global Area 325685248 bytes
Fixed Size 2252944 bytes
Variable Size 171970416 bytes
Database Buffers 146800640 bytes
Redo Buffers 4661248 bytes
Database mounted.
SYS@LHR11G> alter database flashback on; #开启闪回
Database altered.
SYS@LHR11G> alter database open; #open数据库
Database altered.
SYS@LHR11G> select flashback_on from v$database; #验证是否开启
FLASHBACK_ON
------------------------------------
YES
设置force logging,修改数据库为强制记日志,这是必须的操作,主库的每一步操作都得记录到日志中去。
-- 查看数据库归档模式是否强制归档:
SYS@LHR11G> select name,log_mode,force_logging from v$database;
NAME LOG_MODE FORCE_
------------------ ------------------------ ------
LHR11G ARCHIVELOG NO
备注:如果数据库已经开启force logging,那么下面步骤可忽略。如上可以看出数据库未开启,则按下面步骤执行
SYS@LHR11G> alter database force logging; #开启force logging
Database altered.
SYS@LHR11G> select name,log_mode,force_logging from v$database; #验证
NAME LOG_MODE FORCE_
------------------ ------------------------ ------
LHR11G ARCHIVELOG YES
在备库,当RFS进程接受到日志后,就将其写入Standby日志文件里,备库的Standby日志文件可以看做是主库在线日志文件的一个镜像,当主库做日志切换时,备库的Standby日志也做相应的切换,切换后的Standby日志由备库的ARCH进程归档。
Oracle规定备库的Standby日志文件大小不能小于主库在线日志文件最大的一个,一般情况下,为了管理方便,最好把所有的在线日志和Standby日志大小设为一样。
-- 通过下面语句可以查询主库在线日志的大小和组数
SYS@LHR11G> select group#,bytes/1024/1024 from v$log;
GROUP# BYTES/1024/1024
---------- ---------------
1 50
2 50
3 50
-- 通过下面的语句可以查询备库Standby日志的大小和组数:
SYS@LHR11G> select group#,bytes/1024/1024 from v$standby_log;
no rows selected
-- 创建standby logfile
[root@oracleprimary data]# mkdir -p /data/LHR11G/onlinelog
[root@oracleprimary data]# chown oracle:dba /data/LHR11G/onlinelog
SYS@LHR11G> alter database add standby logfile group 11 '/data/LHR11G/onlinelog/redo11_stb01.log' size 50M;
SYS@LHR11G> alter database add standby logfile group 11 '/data/LHR11G/onlinelog/redo11_stb01.log' size 50M;
Database altered.
SYS@LHR11G> alter database add standby logfile group 12 '/data/LHR11G/onlinelog/redo12_stb01.log'size 50M;
Database altered.
SYS@LHR11G> alter database add standby logfile group 13 '/data/LHR11G/onlinelog/redo13_stb01.log'size 50M;
Database altered.
SYS@LHR11G> alter database add standby logfile group 14 '/data/LHR11G/onlinelog/redo14_stb01.log'size 50M;
Database altered.
SYS@LHR11G> select group#,bytes/1024/1024 from v$standby_log;
GROUP# BYTES/1024/1024
---------- ---------------
11 50
12 50
13 50
14 50
-- 10g的DATA GUARD的一个主要特点就是引入了log_archive_config参数,如果缺少这个参数,可能会导致归档路径被禁用。
SYS@LHR11G> alter system set log_archive_config="dg_config=(LHR11G,LHR11GS)" scope=both;
System altered.
-- 设置归档路径
[root@oracleprimary data]# mkdir -p /data/LHR11G/archivelog
[root@oracleprimary data]# chown oracle:dba /data/LHR11G/archivelog
-- 设置归档路径1
SYS@LHR11G> alter system set log_archive_dest_1='location=/data/LHR11G/archivelog/ valid_for=(all_logfiles,all_roles) db_unique_name=LHR11G';
System altered.
SYS@LHR11G> alter system set log_archive_dest_state_1="enable";
System altered.
-- 设置归档路径2 远程归档
-- 远程目标的Oracle Net别名,由关键字SERVICE指定。指定的服务名通过使用本地的tnsnames.ora文件进行解析,以标识远程数据库。
SYS@LHR11G> alter system set log_archive_dest_2="service=LHR11GS lgwr async valid_for=(online_logfile,primary_role) db_unique_name=LHR11GS" scope=both;
System altered.
SYS@LHR11G> alter system set log_archive_dest_state_2="enable";
System altered.
-- 主数据库和备用数据库的数据文件转换目录映射
SYS@LHR11G> alter system set db_file_name_convert="/data/LHR11GS/datafile","/data/LHR11G/datafile" scope=spfile;
System altered.
-- 指明主数据库和备用数据库的log文件转换目录映射。
SYS@LHR11G> alter system set log_file_name_convert="/data/LHR11GS/onlinelog","/data/LHR11G/onlinelog" scope=spfile;
-- 方便switchover或failover时primary库转变为standby角色
System altered.
-- FAL_SERVER指定一个Oracle Net service name,standby数据库使用这个参数连接到FAL server,这个参数适用于standby站点。
-- 比如,FAL_SERVER = PrimaryDB,此处PrimaryDB是一个TNS name,指向primary库
SYS@LHR11G> alter system set fal_server=LHR11GS scope=both;
System altered.
-- FAL_CLIENT指定一个FAL客户端的名字,以便FAL Server可以引用standby库,这也是一个TNS name,primary库必须适当配置此TNS name指向stanby库。
-- 这个参数也是在standby库端设置。比如,FAL_CLIENT = StandbyDB,StandbyDB是standby库的TNS name。
SYS@LHR11G> alter system set fal_client=LHR11G scope=both;
System altered.
SYS@LHR11G> alter system set standby_file_management=auto scope=both;
System altered.
在一定的条件下,或者因为网络失败,或者因为资源紧张,会在primary和standby之间产生裂隙,也就是有些归档日志没有及时的传输并应用到standby库。因为MRP(managed recovery process)/LSP(logical standby process)没有与primary直接通讯的能力来获取丢失的归档日志。因此这些gaps通过FAL客户和服务器来解决,由初始化参数定义FAL_CLIENT和FAL_SERVER。
FAL_SERVER指定一个Oracle Net service name,standby数据库使用这个参数连接到FAL server,这个参数适用于standby站点。比如,FAL_SERVER = PrimaryDB,此处PrimaryDB是一个TNS name,指向primary库。
FAL_CLIENT指定一个FAL客户端的名字,以便FAL Server可以引用standby库,这也是一个TNS name,primary库必须适当配置此TNS name指向stanby库。这个参数也是在standby库端设置。比如,FAL_CLIENT = StandbyDB,StandbyDB是standby库的TNS name。
FAL_CLIENT和FAL_SERVER应该成对设置或改变。
这两个参数只需在standby库设置,但也可以在primary库设置这两个参数,以方便switchover或failover时primary库转变为standby角色
SYS@LHR11G> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS@LHR11G> startup
ORACLE instance started.
Total System Global Area 325685248 bytes
Fixed Size 2252944 bytes
Variable Size 171970416 bytes
Database Buffers 146800640 bytes
Redo Buffers 4661248 bytes
Database mounted.
Database opened.
[oracle@oracleprimary bin]$ vim $ORACLE_HOME/network/admin/listener.ora
-- 追加如下内容:
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = LHR11G)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0.4/dbhome_1)
(SID_NAME = LHR11G)
)
)
[oracle@oracleprimary admin]$ lsnrctl reload
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 20-SEP-2022 07:18:42
Copyright (c) 1991, 2013, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))
The command completed successfully
------确认新增加的静态监听有效:
[oracle@oracleprimary admin]$ lsnrctl status
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 20-SEP-2022 07:18:52
Copyright (c) 1991, 2013, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date 20-SEP-2022 04:03:03
Uptime 0 days 3 hr. 15 min. 48 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /u01/app/oracle/product/11.2.0.4/dbhome_1/network/admin/listener.ora
Listener Log File /u01/app/oracle/diag/tnslsnr/oracleprimary/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=oracleprimary)(PORT=1521)))
Services Summary...
Service "LHR11G" has 2 instance(s).
Instance "LHR11G", status UNKNOWN, has 1 handler(s) for this service...
Instance "LHR11G", status READY, has 1 handler(s) for this service...
Service "LHR11GXDB" has 1 instance(s).
Instance "LHR11G", status READY, has 1 handler(s) for this service...
The command completed successfully
注意:如果主库上的监听之前已经配置过,这里可以忽略该步骤。
[oracle@oracleprimary admin]$ vim $ORACLE_HOME/network/admin/tnsnames.ora
---编辑内容如下
LHR11G =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.140)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = LHR11G)
)
)
LHR11GS =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.141)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = LHR11GS)
)
)
[oracle@oracleprimary admin]$ tnsping LHR11G
[oracle@oracleprimary admin]$ tnsping LHR11GS
[oracle@oracleprimary admin]$ lsnrctl stop
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 20-SEP-2022 09:54:24
Copyright (c) 1991, 2013, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))
The command completed successfully
[oracle@oracleprimary admin]$ lsnrctl start
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 20-SEP-2022 09:54:30
Copyright (c) 1991, 2013, Oracle. All rights reserved.
Starting /u01/app/oracle/product/11.2.0.4/dbhome_1/bin/tnslsnr: please wait...
TNSLSNR for Linux: Version 11.2.0.4.0 - Production
System parameter file is /u01/app/oracle/product/11.2.0.4/dbhome_1/network/admin/listener.ora
Log messages written to /u01/app/oracle/diag/tnslsnr/oracleprimary/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=oracleprimary)(PORT=1521)))
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date 20-SEP-2022 09:54:30
Uptime 0 days 0 hr. 0 min. 0 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /u01/app/oracle/product/11.2.0.4/dbhome_1/network/admin/listener.ora
Listener Log File /u01/app/oracle/diag/tnslsnr/oracleprimary/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=oracleprimary)(PORT=1521)))
Services Summary...
Service "LHR11G" has 1 instance(s).
Instance "LHR11G", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
-- 创建参数文件,修改后用于备库
SYS@LHR11G> create pfile='/tmp/initLHR11GS.ora' from spfile;
File created.
-- 将pfile拷贝到备库、
-- 先从主库容器拷贝到备库的宿主机中
[oracle@oracleprimary tmp]$ scp /tmp/initLHR11GS.ora [email protected]:/tmp
[email protected]'s password:
initLHR11GS.ora 100% 1581 1.5KB/s 00:00
-- 在从备库的宿主机拷贝到备库容器
[oracle@oraclestandby dbs]$ scp [email protected]:/tmp/initLHR11GS.ora /u01/app/oracle/product/11.2.0.4/dbhome_1/dbs
The authenticity of host '192.168.1.141 (192.168.1.141)' can't be established.
RSA key fingerprint is b2:73:bc:7a:76:33:cc:2d:a5:de:03:c9:e5:db:2b:f9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.141' (RSA) to the list of known hosts.
[email protected]'s password:
initLHR11GS.ora 100% 1581 1.5KB/s 00:00
[oracle@oracleprimary ~]$ cd $ORACLE_HOME/dbs
-- 知道的密码的,可以直接远程复制 scp orapwLHR11G 192.168.1.141:/u01/app/oracle/product/11.2.0.4/dbhome_1/dbs
-- 先从主库容器拷贝到备库的宿主机中
[oracle@oracleprimary dbs]$ scp orapwLHR11G [email protected]:/tmp/orapwLHR11G_BAK
[email protected]'s password:
orapwLHR11G 100% 1536 1.5KB/s 00:00
-- 再从备库的宿主机拷贝到备库容器
[oracle@oraclestandby dbs]$ scp [email protected]:/tmp/orapwLHR11G_BAK /u01/app/oracle/product/11.2.0.4/dbhome_1/dbs
[email protected]'s password:
orapwLHR11G_BAK 100% 1536 1.5KB/s 00:00
主要是用于本地恢复,对于网络情况不是很好或者文件比较大的情况是不错的选择
--- 创建控制文件备份目录
[oracle@oracleprimary dbs]$ mkdir -p /home/oracle/LHR11G_BACKUP/controlfile/
[oracle@oracleprimary dbs]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Fri Sep 23 16:48:24 2022
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
--- 创建控制文件到目录
SYS@LHR11G> alter database create standby controlfile as '/home/oracle/LHR11G_BACKUP/controlfile/LHR11GS.ctl';
Database altered.
SYS@LHR11G> exit
--- 创建数据文件备份目录
[oracle@oracleprimary controlfile]$ mkdir -p /home/oracle/LHR11G_BACKUP/datafile
[oracle@oracleprimary controlfile]$ rman target /
Recovery Manager: Release 11.2.0.4.0 - Production on Fri Sep 23 16:51:25 2022
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
connected to target database: LHR11G (DBID=2007947551)
--- 创建数据文件到目录
RMAN> backup database format '/home/oracle/LHR11G_BACKUP/datafile/full_%U.bak' plus archivelog format '/home/oracle/LHR11G_BACKUP/datafile/arch_%U.bak';
--- ..........
这里省略,主要观察有没有错误,没有的话就是数据文件备份成功了-----------
--- 检查备份文件是否都生成了
[oracle@oracleprimary datafile]$ cd /home/oracle/LHR11G_BACKUP/
[oracle@oracleprimary LHR11G_BACKUP]$ ll
total 0
drwxr-xr-x 2 oracle oinstall 46 Sep 23 16:49 controlfile
drwxr-xr-x 2 oracle oinstall 122 Sep 23 16:52 datafile
[oracle@oracleprimary LHR11G_BACKUP]$ cd controlfile/
[oracle@oracleprimary controlfile]$ ll
total 19040
-rw-r----- 1 oracle oinstall 9748480 Sep 23 16:49 LHR11GS.ctl
[oracle@oracleprimary controlfile]$ cd ../datafile/
[oracle@oracleprimary datafile]$ ll
total 1327740
-rw-r----- 1 oracle oinstall 162791936 Sep 23 16:52 arch_0g18evih_1_1.bak
-rw-r----- 1 oracle oinstall 8192 Sep 23 16:52 arch_0j18evis_1_1.bak
-rw-r----- 1 oracle oinstall 1186971648 Sep 23 16:52 full_0h18evii_1_1.bak
-rw-r----- 1 oracle oinstall 9830400 Sep 23 16:52 full_0i18eviq_1_1.bak
-- -- 先从主库容器拷贝到备库的宿主机中
[oracle@oracleprimary ~]$ scp -r /home/oracle/LHR11G_BACKUP [email protected]:/tmp/LHR11G_BACKUP
[email protected]'s password: 100% 9520KB 9.3MB/s 00:00
LHR11GS.ctl 100% 9520KB 9.3MB/s 00:00
arch_0g18evih_1_1.bak 100% 155MB 155.3MB/s 00:01
full_0h18evii_1_1.bak 100% 1132MB 141.5MB/s 00:08
full_0i18eviq_1_1.bak 100% 9600KB 9.4MB/s 00:00
arch_0j18evis_1_1.bak
-- 再从备库的宿主机拷贝到备库容器
[oracle@oraclestandby controlfile]$ scp -r [email protected]:/tmp/LHR11G_BACKUP /home/oracle/LHR11G_BACKUP
[oracle@oraclestandby dbs]$ vim $ORACLE_HOME/dbs/initLHR11GS.ora
注意红部分 |
---|
LHR11GS .__db_cache_size=188743680 |
LHR11GS .__java_pool_size=4194304
LHR11GS .__large_pool_size=8388608
LHR11GS .__oracle_base=’/u01/app/oracle’#ORACLE_BASE set from environment
LHR11GS .__pga_aggregate_target=83886080
LHR11GS .__sga_target=327155712
LHR11GS .__shared_io_pool_size=0
LHR11GS .__shared_pool_size=117440512
LHR11GS.__streams_pool_size=0
*.audit_file_dest=’/u01/app/oracle/admin/LHR11GS/adump’
*.audit_trail=‘db’
*.compatible=‘11.2.0.4.0’
*.control_files=’/data/LHR11GS/controlfile/control01.ctl’,’/data/LHR11GS/controlfile/control02.ctl’
*.db_block_size=8192
*.db_domain=’’
*.db_file_name_convert=’/data/LHR11G/datafile’,’/data/LHR11GS/datafile’
*.db_name=‘LHR11G’
*.db_recovery_file_dest=’/data/LHR11GS/recoverydest’
*.db_recovery_file_dest_size=5368709120
*.diagnostic_dest=’/u01/app/oracle’
*.dispatchers=’(PROTOCOL=TCP) (SERVICE=LHR11GSXDB)’
*.fal_client=‘LHR11GS’
*.fal_server=‘LHR11G’
*.job_queue_processes=1000
*.log_archive_config=‘dg_config=(LHR11GS,LHR11G)’
*.log_archive_dest_1=‘location=/data/LHR11GS/archivelog/ valid_for=(all_logfiles,all_roles) db_unique_name=LHR11GS’
*.log_archive_dest_2=‘service=LHR11G lgwr async valid_for=(online_logfile,primary_role) db_unique_name=LHR11G’
*.log_archive_dest_state_1=‘enable’
*.log_archive_dest_state_2=‘enable’
*.log_file_name_convert=’/data/LHR11G/onlinelog’,’/data/LHR11GS/onlinelog’
*.open_cursors=300
*.pga_aggregate_target=81788928
*.processes=150
*.remote_login_passwordfile=‘EXCLUSIVE’
*.sga_target=327155712
*.standby_file_management=‘AUTO’
*.undo_tablespace=‘UNDOTBS1’
修改之前从主库拷贝过来的密码文件,具体如下:
[oracle@oraclestandby dbs]$ cd $ORACLE_HOME/dbs
[oracle@oraclestandby dbs]$ mv orapwLHR11G_BAK orapwLHR11GS
[oracle@oraclestandby dbs]$ mkdir -p /u01/app/oracle/admin/LHR11GS/adump #审计文件目标目录audit_file_dest
[root@oraclestandby /]# mkdir -p /data/LHR11GS/
[root@oraclestandby /]# chown oracle:dba /data/LHR11GS/
[root@oraclestandby /]# su - oracle
[oracle@oraclestandby ~]$ mkdir -p /data/LHR11GS/recoverydest #设置闪回路径
[oracle@oraclestandby ~]$ mkdir -p /data/LHR11GS/archivelog #设置归档路径
[oracle@oraclestandby ~]$ mkdir -p /data/LHR11GS/datafile #主备数据文件转换目录 db_file_name_convert
[oracle@oraclestandby ~]$ mkdir -p /data/LHR11GS/onlinelog #主备日志文件转换目录 log_file_name_convert
[oracle@oraclestandby ~]$ mkdir -p /data/LHR11GS/controlfile
[oracle@oraclestandby ~]$ vim $ORACLE_HOME/network/admin/listener.ora
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = oraclestandby)(PORT = 1521))
)
)
ADR_BASE_LISTENER = /u01/app/oracle
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = LHR11GS)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0.4/dbhome_1)
(SID_NAME = LHR11GS)
)
)
[oracle@oraclestandby ~]$ vim $ORACLE_HOME/network/admin/tnsnames.ora
LHR11G =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST =192.168.1.140)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = LHR11G)
)
)
LHR11GS =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST =192.168.1.141)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = LHR11GS)
)
)
[oracle@oraclestandby 11.2.0.4]$ lsnrctl stop
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 20-SEP-2022 11:57:51
Copyright (c) 1991, 2013, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))
The command completed successfully
[oracle@oraclestandby 11.2.0.4]$ lsnrctl start
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 20-SEP-2022 11:58:08
Copyright (c) 1991, 2013, Oracle. All rights reserved.
Starting /u01/app/oracle/product/11.2.0.4/dbhome_1/bin/tnslsnr: please wait...
TNSLSNR for Linux: Version 11.2.0.4.0 - Production
System parameter file is /u01/app/oracle/product/11.2.0.4/dbhome_1/network/admin/listener.ora
Log messages written to /u01/app/oracle/diag/tnslsnr/oraclestandby/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=oraclestandby)(PORT=1521)))
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date 20-SEP-2022 11:58:08
Uptime 0 days 0 hr. 0 min. 0 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /u01/app/oracle/product/11.2.0.4/dbhome_1/network/admin/listener.ora
Listener Log File /u01/app/oracle/diag/tnslsnr/oraclestandby/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=oraclestandby)(PORT=1521)))
Services Summary...
Service "LHR11GS" has 1 instance(s).
Instance "LHR11GS", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
[oracle@oraclestandby onlinelog]$ export ORACLE_SID=LHR11GS
[oracle@oraclestandby onlinelog]$ sas
SQL*Plus: Release 11.2.0.4.0 Production on Wed Sep 21 05:16:29 2022
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to an idle instance.
SYS@LHR11GS> startup nomount pfile='/u01/app/oracle/product/11.2.0.4/dbhome_1/dbs/initLHR11GS.ora';
ORACLE instance started.
Total System Global Area 325685248 bytes
Fixed Size 2252944 bytes
Variable Size 171970416 bytes
Database Buffers 146800640 bytes
Redo Buffers 4661248 bytes
SYS@LHR11GS> create spfile from pfile;
File created.
[oracle@oracleprimary ~]$ sqlplus sys/sys@LHR11G as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Wed Sep 21 06:48:48 2022
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SYS@LHR11G> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@oracleprimary ~]$ sqlplus sys/sys@LHR11GS as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Wed Sep 21 06:48:54 2022
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SYS@LHR11GS> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@oraclestandby dbs]$ sqlplus sys/sys@LHR11GS as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Wed Sep 21 06:50:12 2022
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SYS@LHR11GS> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@oraclestandby dbs]$ sqlplus sys/sys@LHR11G as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Wed Sep 21 06:50:18 2022
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SYS@LHR11G> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
这里采用在线恢复
[oracle@oracleprimary ~]$ rman target sys/sys@LHR11G auxiliary sys/sys@LHR11GS
Recovery Manager: Release 11.2.0.4.0 - Production on Sat Sep 24 13:50:24 2022
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
connected to target database: LHR11G (DBID=2007947551)
connected to auxiliary database: LHR11G (not mounted)
RMAN> duplicate target database for standby from active database nofilenamecheck;
-----。。。。。---------
Finished Duplicate Db at 2022-09-24 13:51:11
RMAN> exit
完成了上述的步骤,备库还没有打开
SYS@LHR11GS> shutdown immediate;
ORA-01109: database not open
Database dismounted.
ORACLE instance shut down.
SYS@LHR11GS> startup
ORACLE instance started.
Total System Global Area 325685248 bytes
Fixed Size 2252944 bytes
Variable Size 171970416 bytes
Database Buffers 146800640 bytes
Redo Buffers 4661248 bytes
Database mounted.
Database opened.
--- 开启实时同步
SYS@LHR11GS> alter database recover managed standby database using current logfile disconnect from session;
Database altered.
上面DataGuard搭建好之后,千万不要忘了把备库的flashback打开,具体如下:
---- 取消实时同步
SYS@LHR11GS> alter database recover managed standby database cancel;
Database altered.
-- 关闭数据库
SYS@LHR11GS> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
-- 打开到mount状态
SYS@LHR11GS> startup mount
ORACLE instance started.
Total System Global Area 325685248 bytes
Fixed Size 2252944 bytes
Variable Size 171970416 bytes
Database Buffers 146800640 bytes
Redo Buffers 4661248 bytes
Database mounted.
--- 开启flashback
SYS@LHR11GS> alter database flashback on;
Database altered.
-- open数据库
SYS@LHR11GS> alter database open;
Database altered.
-- 开启实时同步
SYS@LHR11GS> alter database recover managed standby database using current logfile disconnect from session;
Database altered.
上面已经把DataGurad搭建完,下面介绍如何验证DataGuard是否能实时同步
[oracle@oracleprimary ~]$ sas
SQL*Plus: Release 11.2.0.4.0 Production on Sat Sep 24 14:17:24 2022
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SYS@LHR11G> col dest_name format a30
SYS@LHR11G> col error format a20
SYS@LHR11G> select dest_name,error from v$archive_dest;
DEST_NAME ERROR
------------------------------ --------------------
LOG_ARCHIVE_DEST_1
LOG_ARCHIVE_DEST_2
LOG_ARCHIVE_DEST_3
LOG_ARCHIVE_DEST_4
LOG_ARCHIVE_DEST_5
LOG_ARCHIVE_DEST_6
LOG_ARCHIVE_DEST_7
LOG_ARCHIVE_DEST_8
LOG_ARCHIVE_DEST_9
LOG_ARCHIVE_DEST_10
LOG_ARCHIVE_DEST_11
LOG_ARCHIVE_DEST_12
LOG_ARCHIVE_DEST_13
LOG_ARCHIVE_DEST_14
LOG_ARCHIVE_DEST_15
LOG_ARCHIVE_DEST_16
LOG_ARCHIVE_DEST_17
LOG_ARCHIVE_DEST_18
LOG_ARCHIVE_DEST_19
LOG_ARCHIVE_DEST_20
LOG_ARCHIVE_DEST_21
LOG_ARCHIVE_DEST_22
LOG_ARCHIVE_DEST_23
LOG_ARCHIVE_DEST_24
LOG_ARCHIVE_DEST_25
LOG_ARCHIVE_DEST_26
LOG_ARCHIVE_DEST_27
LOG_ARCHIVE_DEST_28
LOG_ARCHIVE_DEST_29
LOG_ARCHIVE_DEST_30
LOG_ARCHIVE_DEST_31
31 rows selected.
注意:上面显示没有报错
--------主库上执行:
SYS@LHR11G> select max(sequence#) from v$archived_log;
MAX(SEQUENCE#)
--------------
65
--------备库上执行:
SYS@LHR11GS> select max(sequence#) from v$archived_log;
MAX(SEQUENCE#)
--------------
65
---- 主库上尝试执行日志切换,查看归档序号的变化
SYS@LHR11G> alter system archive log current;
--- 主库上执行,查看最大归档序号
System altered.
SYS@LHR11G> select max(sequence#) from v$archived_log;
MAX(SEQUENCE#)
--------------
66
---备库上再次验证:
SYS@LHR11GS> select max(sequence#) from v$archived_log;
MAX(SEQUENCE#)
--------------
66
----- 主库上执行:
SYS@LHR11G> select switchover_status,database_role from v$database;
SWITCHOVER_STATUS DATABASE_ROLE
---------------------------------------- --------------------------------
SESSIONS ACTIVE PRIMARY
----- 备库上执行:
SYS@LHR11GS> select switchover_status,database_role from v$database;
SWITCHOVER_STATUS DATABASE_ROLE
---------------------------------------- --------------------------------
NOT ALLOWED PHYSICAL STANDBY
--- 主库上执行:
SYS@LHR11G> create table dg(id number);
Table created.
SYS@LHR11G> insert into dg values(1);
1 row created.
SYS@LHR11G> commit;
Commit complete.
SYS@LHR11G> select * from dg;
ID
----------
1
---- 备库上执行
SYS@LHR11GS> select * from dg;
ID
----------
1
上面说明DG是同步的。
我们配置DG的目的就是为了在主库出现故障时,备库能够提供服务,保证业务的正常运行。DG的故障切换分为switchover和failover两种:
switchover是用户有计划的进行停机切换,能够保证不丢失数据,下面我们来看下switchover是怎样操作的:
SYS@LHR11G> select switchover_status,database_role from v$database;
SWITCHOVER_STATUS DATABASE_ROLE
---------------------------------------- --------------------------------
SESSIONS ACTIVE PRIMARY
#######################-注意:上面查询结果为TO STANDBY 或 SESSIONS ACTIVE表明可以进行切换备库
---- 切换数据库模式为 PHYSICAL STANDBY
SYS@LHR11G> alter database commit to switchover to physical standby;
Database altered.
SYS@LHR11G> startup mount
ORACLE instance started.
Total System Global Area 325685248 bytes
Fixed Size 2252944 bytes
Variable Size 171970416 bytes
Database Buffers 146800640 bytes
Redo Buffers 4661248 bytes
Database mounted.
SYS@LHR11G> select database_role from v$database;
DATABASE_ROLE
--------------------------------
PHYSICAL STANDBY
SYS@LHR11GS> select switchover_status,database_role from v$database;
SWITCHOVER_STATUS DATABASE_ROLE
---------------------------------------- --------------------------------
TO PRIMARY PHYSICAL STANDBY
###### 注意:上面查询结果显示为TO PRIMARY 或 SESSIONS ACTIVE表明可以切换成主库;
---- 现在可以把备库切换成主库:
SYS@LHR11GS> alter database commit to switchover to primary with session shutdown;
Database altered.
SYS@LHR11GS> alter database open;
Database altered.
SYS@LHR11GS> select switchover_status,database_role,open_mode from v$database;
SWITCHOVER_STATUS DATABASE_ROLE OPEN_MODE
---------------------------------------- -------------------------------- ----------------------------------------
RESOLVABLE GAP PRIMARY READ WRITE
记住:这时候需要在现在的备库(原先的主库)开启实时同步
SYS@LHR11G> alter database open;
Database altered.
--- 开启实时同步
SYS@LHR11G> alter database recover managed standby database using current logfile disconnect from session;
Database altered.
SYS@LHR11G> select switchover_status,database_role from v$database;
SWITCHOVER_STATUS DATABASE_ROLE
---------------------------------------- --------------------------------
NOT ALLOWED PHYSICAL STANDBY
SYS@LHR11GS> select switchover_status,database_role,open_mode from v$database;
SWITCHOVER_STATUS DATABASE_ROLE OPEN_MODE
---------------------------------------- -------------------------------- ----------------------------------------
TO STANDBY PRIMARY READ WRITE
failover是当主库真正出现严重系统故障,如数据库宕机,软硬件故障导致主库不能支持服务,从而进行的切换动作。
注意:为了能够在failover后能够恢复DG,需要在主库上开启flashback,如果不开启flashback的话,DG就可能需要重新搭建
以上步骤主备库已经切换了
[root@davystandby ~]# docker stop dbstandby
dbst
由于主库已经不可访问,下面所有的操作都在备库完成:
----停止实时同步
SYS@LHR11G> alter database recover managed standby database cancel;
Database altered.
SYS@LHR11G> select switchover_status,database_role,open_mode from v$database;
SWITCHOVER_STATUS DATABASE_ROLE OPEN_MODE
---------------------------------------- -------------------------------- ----------------------------------------
NOT ALLOWED PHYSICAL STANDBY READ ONLY
SYS@LHR11G> alter database recover managed standby database finish force;
Database altered.
SYS@LHR11G> select switchover_status,database_role,open_mode from v$database;
SWITCHOVER_STATUS DATABASE_ROLE OPEN_MODE
---------------------------------------- -------------------------------- ----------------------------------------
TO PRIMARY PHYSICAL STANDBY READ ONLY
SYS@LHR11G> alter database commit to switchover to primary;
Database altered.
SYS@LHR11G> alter database open;
Database altered.
SYS@LHR11G> select switchover_status,database_role,open_mode from v$database;
SWITCHOVER_STATUS DATABASE_ROLE OPEN_MODE
---------------------------------------- -------------------------------- ----------------------------------------
RESOLVABLE GAP PRIMARY READ WRITE
至此failover操作完成,原来的备库已经切换为主库,可以给业务提供服务了。
上面提到了failover,这种情形是当主库真正出现异常之后,才会执行的操作,那么我们执行过failover 之后,如何在重新构建DG,这里我们利用flashback database来重构,具体方法如下:
SYS@LHR11G> select switchover_status,database_role,open_mode from v$database;
SWITCHOVER_STATUS DATABASE_ROLE OPEN_MODE
---------------------------------------- -------------------------------- ----------------------------------------
RESOLVABLE GAP PRIMARY READ WRITE
SYS@LHR11G> select to_char(standby_became_primary_scn) from v$database;
TO_CHAR(STANDBY_BECAME_PRIMARY_SCN)
--------------------------------------------------------------------------------
1383514
[root@davystandby ~]# docker start dbstandby
dbstandby
[root@davystandby tmp]# docker exec -it dbstandby bash
[oracle@oraclestandby ~]$ lsnrctl start
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 24-SEP-2022 15:42:11
Copyright (c) 1991, 2013, Oracle. All rights reserved.
Starting /u01/app/oracle/product/11.2.0.4/dbhome_1/bin/tnslsnr: please wait...
TNSLSNR for Linux: Version 11.2.0.4.0 - Production
System parameter file is /u01/app/oracle/product/11.2.0.4/dbhome_1/network/admin/listener.ora
Log messages written to /u01/app/oracle/diag/tnslsnr/oraclestandby/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=oraclestandby)(PORT=1521)))
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))
STATUS of the LISTENER
Alias LISTENER
Version TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date 24-SEP-2022 15:42:13
Uptime 0 days 0 hr. 0 min. 0 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /u01/app/oracle/product/11.2.0.4/dbhome_1/network/admin/listener.ora
Listener Log File /u01/app/oracle/diag/tnslsnr/oraclestandby/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=oraclestandby)(PORT=1521)))
Services Summary...
Service "LHR11G" has 1 instance(s).
Instance "LHR11G", status UNKNOWN, has 1 handler(s) for this service...
Service "LHR11GS" has 1 instance(s).
Instance "LHR11GS", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
[oracle@oraclestandby ~]$ sas
SQL*Plus: Release 11.2.0.4.0 Production on Sat Sep 24 15:42:28 2022
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
------启动数据库为mount状态
SYS@LHR11GS> startup mount
ORACLE instance started.
Total System Global Area 325685248 bytes
Fixed Size 2252944 bytes
Variable Size 171970416 bytes
Database Buffers 146800640 bytes
Redo Buffers 4661248 bytes
Database mounted.
---执行闪回,这个值为在新主库上查询到的SCN值
SYS@LHR11GS> flashback database to scn 1383514;
Flashback complete.
SYS@LHR11GS> alter database convert to physical standby;
Database altered.
SYS@LHR11GS> shutdown immediate
ORA-01507: database not mounted
ORACLE instance shut down.
SYS@LHR11GS> startup
ORACLE instance started.
Total System Global Area 325685248 bytes
Fixed Size 2252944 bytes
Variable Size 171970416 bytes
Database Buffers 146800640 bytes
Redo Buffers 4661248 bytes
Database mounted.
Database opened.
--开启实时同步
SYS@LHR11GS> alter database recover managed standby database using current logfile disconnect from session;
Database altered.
SYS@LHR11GS> select switchover_status,database_role,open_mode from v$database;
SWITCHOVER_STATUS DATABASE_ROLE OPEN_MODE
---------------------------------------- -------------------------------- ----------------------------------------
SWITCHOVER PENDING PHYSICAL STANDBY READ ONLY WITH APPLY
到此failover 恢复已经完成,关于如何验证DG方法如上,这里不在介绍。