楔子:在一卡通行业里面做了许久,见过了很多因硬件故障导致数据库问题的案例。想了很久,是时候写一下自己自己安装dg的经验了。为了全面的巩固自己的技能,为了能让对这方面有需求的X,也为了纪录自己在这方面的经历,特写下此文
说起oracle数据库的容灾,就不得不说oracle的dg。
什么是dg?
为什么要使用dg?
怎么才能安装oracle数据库的dg?
答(1):dg全名成为Data Gurad。是oracle数据库的一种灾备工具。在Data Gurad 环境中,至少有两个数据库,一个处于Open 状态对外提供服务,这个数据库叫作Primary Database。 第二个处于恢复状态,叫作Standby Database。 运行时primary Database 对外提供服务,用户在Primary Database 上进行操作,操作被记录在联机日志和归档日志中,这些日志通过网络传递给Standby Database。 这个日志会在Standby Database 上重演,从而实现Primary Database 和Standby Database 的数据同步。
答(2):其实第一个回答就能说明情况了。为了数据的安全,一个数据库作为主数据库,提供增删改差作用。另一个备库,是只读模式。为了能达到实时备份数据的作用。以防发生了硬件故障导致数据的丢失。(备库处于只读模式,也可以实现业务的读功能)
答(3):详细见下文
oralce data gurad实现原理图:
下面进入正题:oracle安装dg操作步骤
查看主库是否归档。不是归档模式的话改成归档模式。
SYS@PROD1> archive log list;
SYS@PROD1> show parameter recovery;
改成归档模式:建立归档文件路径/u01/arch
SYS@PROD1> alter system set db_recovery_file_dest='/home/oracle/flash' scope=spfile;
SYS@PROD1> shutdown immediate;
SYS@PROD1> startup mount;
SYS@PROD1> alter database archivelog;
SYS@PROD1> alter database open;
SYS@PROD1> archive log list;
主库:开启强制写日志功能.配置静态监听, 编辑/u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora:
SYS@PROD1> select force_logging from v$database;
SYS@PROD1> alter database force logging;
SYS@PROD1> select force_logging from v$database;
编辑/u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora:
主库配置tnsnames 文件:(SBDB1为备库、PROD1为主库)
主库增加 standby logfile 文件
SYS@PROD1> set linesize 200;
SYS@PROD1> col member for a60
SYS@PROD1> select group#, member from v$logfile;
SYS@PROD1> select bytes/1024/1024 "Size MB" from v$log;
新增加的standby logfile文件需要比redo日志多一组,且大小跟redo日志大小一样。
SYS@PROD1>alter database add standby logfile '/u01/app/oracle/oradata/PROD1/redo04.log' size 50m;
alter database add standby logfile '/u01/app/oracle/oradata/PROD1/redo05.log' size 50m;
alter database add standby logfile '/u01/app/oracle/oradata/PROD1/redo06.log' size 50m;
alter database add standby logfile '/u01/app/oracle/oradata/PROD1/redo07.log' size 50m;
SYS@PROD1> select group#, member, type from v$logfile where type='STANDBY';
主库: 修改参数文件, 将其下面内容添加到 initPROD1.ora 文件中
SYS@PROD1> create pfile from spfile;---默认在$ORACLE_HOME/dbs文件下面
vim initPROD1.ora
附上主库的参数文件:
PROD1.__java_pool_size=4194304
PROD1.__large_pool_size=4194304
PROD1.__oracle_base='/u01/app/oracle'#ORACLE_BASE set from environment
PROD1.__shared_io_pool_size=0
PROD1.__streams_pool_size=0
*.audit_file_dest='/u01/app/oracle/admin/PROD1/adump'
*.audit_trail='db'
*.compatible='11.2.0.0.0'
*.control_files='/u01/app/oracle/oradata/PROD1/control01.ctl','/u01/app/oracle/oradata/PROD1/control02.ctl'
*.db_block_size=8192
*.db_name='PROD1'
*.db_recovery_file_dest='/home/oracle/flash'
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=PROD1XDB)'
*.memory_max_target=943718400
*.memory_target=943718400
*.open_cursors=300
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.undo_tablespace='UNDOTBS1'
DB_UNIQUE_NAME=PROD1
LOG_ARCHIVE_CONFIG='DG_CONFIG=(PROD1,SBDB1)'
LOG_ARCHIVE_DEST_1=
'LOCATION=/home/oracle/flash
VALID_FOR=(ALL_LOGFILES,ALL_ROLES)
DB_UNIQUE_NAME=PROD1'
LOG_ARCHIVE_DEST_2=
'SERVICE=SBDB1 ASYNC
VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE)
DB_UNIQUE_NAME=SBDB1'
LOG_ARCHIVE_DEST_STATE_1=ENABLE
LOG_ARCHIVE_DEST_STATE_2=ENABLE
REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE
LOG_ARCHIVE_FORMAT=%t_%s_%r.arc
LOG_ARCHIVE_MAX_PROCESSES=30
FAL_SERVER=SBDB1
DB_FILE_NAME_CONVERT='SBDB1','PROD1'
LOG_FILE_NAME_CONVERT='SBDB1','PROD1'
STANDBY_FILE_MANAGEMENT=AUTO
[oracle@edbjr2p1 Skillset]$ cd /u01/app/oracle/product/11.2.0/dbhome_1/dbs/
[oracle@edbjr2p1 dbs]$ scp initPROD1.ora
oracle@192.0.2.12:/u01/app/oracle/product/11.2.0/db_1/dbs/initSBDB1.ora
[oracle@edbjr2p1 dbs]$ scp orapwPROD1
oracle@192.0.2.12:/u01/app/oracle/product/11.2.0/db_1/dbs/orapwSBDB1
备库: 配置静态监听: 编辑
/u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
备库: 配置 tnsnames 文件, 编辑
/u01/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora
在备库上操作修改参数文件:(将原始的 PROD1 和 SBDB1 位置进行调换)
验证一下主库跟备库的连接:
[oracle@edbjr2p2 admin]$ cd /u01/app/oracle/product/11.2.0/db_1/dbs
[oracle@edbjr2p2 dbs]$ vim initSBDB1.ora
备库参数文件:
SBDB1.__java_pool_size=4194304
SBDB1.__large_pool_size=4194304
SBDB1.__oracle_base='/u01/app/oracle'#ORACLE_BASE set from environment
SBDB1.__shared_io_pool_size=0
SBDB1.__streams_pool_size=0
*.audit_file_dest='/u01/app/oracle/admin/SBDB1/adump'
*.audit_trail='db'
*.compatible='11.2.0.0.0'
*.control_files='/u01/app/oracle/oradata/SBDB1/control01.ctl','/u01/app/oracle/oradata/SBDB1/control02.ctl'
*.db_block_size=8192
*.db_name='PROD1'
*.db_recovery_file_dest='/home/oracle/flash'
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=SBDB1XDB)'
*.memory_max_target=943718400
*.memory_target=943718400
*.open_cursors=300
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.undo_tablespace='UNDOTBS1'
DB_UNIQUE_NAME=SBDB1
LOG_ARCHIVE_CONFIG='DG_CONFIG=(SBDB1,PROD1)'
LOG_ARCHIVE_DEST_1=
'LOCATION=/home/oracle/flash
VALID_FOR=(ALL_LOGFILES,ALL_ROLES)
DB_UNIQUE_NAME=SBDB1'
LOG_ARCHIVE_DEST_2=
'SERVICE=PROD1 ASYNC
VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE)
DB_UNIQUE_NAME=PROD1'
LOG_ARCHIVE_DEST_STATE_1=ENABLE
LOG_ARCHIVE_DEST_STATE_2=ENABLE
REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE
LOG_ARCHIVE_FORMAT=%t_%s_%r.arc
LOG_ARCHIVE_MAX_PROCESSES=30
FAL_SERVER=PROD1
DB_FILE_NAME_CONVERT='PROD1','SBDB1'
LOG_FILE_NAME_CONVERT='PROD1','SBDB1'
STANDBY_FILE_MANAGEMENT=AUTO
[oracle@edbjr2p2 dbs]$ cd
[oracle@edbjr2p2 ~]$ mkdir -p /u01/app/oracle/oradata/SBDB1/
[oracle@edbjr2p2 ~]$ mkdir -p /u01/app/oracle/admin/SBDB1/adump
[oracle@edbjr2p2 ~]$ mkdir flash
[oracle@edbjr2p2 ~]$ export ORACLE_SID=SBDB1
[oracle@edbjr2p2 ~]$ sqlplus "/as sysdba
SYS@SBDB1> create spfile from pfile;
File created.
SYS@SBDB1> startup nomount
主库: 通过 rman duplicate 方式进行备库恢复
[oracle@edbjr2p1 dbs]$ export ORACLE_SID=PROD1
[oracle@edbjr2p1 dbs]$ rman target / auxiliary sys/oracle@sbdb1
RMAN> duplicate target database for standby from active database;
验证是否搭建成功:
[oracle@edbjr2p1 dbs]$ export ORACLE_SID=PROD1;sqlplus / as sysdba
SYS@PROD1> select DATABASE_ROLE from v$database;
[oracle@edbjr2p2 dbs]$ export ORACLE_SID=SBDB1;sqlplus / as sysdb
SYS@SBDB1> select open_mode from v$database;
OPEN_MODE
--------------------
MOUNTED
SYS@SBDB1> select DATABASE_ROLE from v$database;
DATABASE_ROLE
----------------
PHYSICAL STANDBY
[oracle@edbjr2p2 dbs]$ cd /u01/app/oracle/oradata/SBDB1/
[oracle@edbjr2p2 SBDB1]$ ls
主库查看归档日志后,做几次切换工作,看一下备库归档是否传输到备库。
[oracle@edbjr2p1 dbs]$ export ORACLE_SID=PROD1;sqlplus / as sysdba
SYS@PROD1> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /home/oracle/flash
Oldest online log sequence 11
Next log sequence to archive 13
Current log sequence 13
SYS@PROD1> alter system switch logfile;
System altered.
SYS@PROD1> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /home/oracle/flash
Oldest online log sequence 12
Next log sequence to archive 14
Current log sequence 14
SYS@PROD1> alter system switch logfile;
System altered.
SYS@PROD1> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /home/oracle/flash
Oldest online log sequence 13
Next log sequence to archive 15
Current log sequence 15
[oracle@edbjr2p2 dbs]$ export ORACLE_SID=SBDB1;sqlplus / as sysdb
SYS@SBDB1> select process, pid, status, client_process from v$managed_standby;
PROCESS PID STATUS CLIENT_P
--------- ---------- ------------ --------
ARCH 14671 CLOSING ARCH
ARCH 14674 CONNECTED ARCH
ARCH 14676 CONNECTED ARCH
ARCH 14678 CONNECTED ARCH
RFS 14996 IDLE ARCH
RFS 14943 IDLE UNKNOWN
RFS 14945 IDLE UNKNOWN
RFS 14948 IDLE LGWR
RFS 14950 IDLE UNKNOWN
SYS@SBDB1> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /home/oracle/flash
Oldest online log sequence 12
Next log sequence to archive 0
Current log sequence 14
SYS@SBDB1> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /home/oracle/flash
Oldest online log sequence 13
Next log sequence to archive 0
Current log sequence 15
[oracle@edbjr2p2 dbs]$ export ORACLE_SID=SBDB1;sqlplus / as sysdb
SYS@SBDB1> SELECT PROTECTION_MODE, PROTECTION_LEVEL,DATABASE_ROLE,SWITCHOVER_STATUS FROM V$DATABASE;
SYS@SBDB1> recover managed standby database using current logfile disconnect from
session;
Media recovery complete.
SYS@SBDB1> recover managed standby database cancel;
Media recovery complete.
SYS@SBDB1> alter database open;
Database altered.
SYS@SBDB1> recover managed standby database using current logfile disconnect from
session;
Media recovery complete.
看一下备库的状态:
此时oracle数据库dg的模式是最大性能模式:
在Maximum performance,主库把归档的 archived log通过arch进程传递给从库,在这种方式下,主库运行性能最高,但是不能保证数据不丢失,且丢失的数据受redo log的大小影响。在redo log过大的情况下,可能一天都没有归档一个日志,可以通过手工切换日志的方式来减小数据的丢失。
oracle的dg共有三种模式:
– Maximum protection
– Maximum availability
– Maximum performance
在Maximum protection下, 可以保证从库和主库数据完全一样,做到zero data loss.事务同时在主从两边提交完成,才算事务完成。如果从库宕机或者网络出现问题,主从库不能通讯,主库也立即宕机。在这种方式下,具有最高的保护等级。但是这种模式对主库性能影响很大,要求高速的网络连接。
在Maximum availability模式下,如果和从库的连接正常,运行方式等同Maximum protection模式,事务也是主从库同时提交。如果从库和主库失去联系,则主库自动切换到Maximum performance模式下运行,保证主库具有最大的可用性。
为数据的安全,也为了不影响主库的运行:可将数据库改成
Maximum availability模式
SYS@PROD1> select name,database_role,protection_mode,open_mode from v$database;
NAME DATABASE_ROLE PROTECTION_MODE OPEN_MODE
--------- ---------------- -------------------- --------------------
PROD1 PRIMARY MAXIMUM PERFORMANCE READ WRITE
SYS@PROD1> alter database set standby database to maximize availability;
Database altered.
SYS@PROD1> select name,database_role,protection_mode,open_mode from v$database;
NAME DATABASE_ROLE PROTECTION_MODE OPEN_MODE
--------- ---------------- -------------------- --------------------
PROD1 PRIMARY MAXIMUM AVAILABILITY READ WRITE
SYS@PROD1>insert into scott.emp1 select * from scott.emp1;
SYS@PROD1>commit;
SYS@PROD1>select count(*) from scott.emp1;
备库查询
SYS@SBDB1>select count(*) from scott.emp1;
恭喜你,oracle中的dg已被你征服!!!
领取专属 10元无门槛券
私享最新 技术干货