数据库 | 数据库SID | DB_UNIQUE_NAME |
---|---|---|
主库 | orcl | orcl |
备库1 | orcl | orcldg |
备库2 | orcl | orcldg2 |
一、Primary数据库配置和操作:
1 .确认主库处于归档模式
将Oracle数据库设置为归档模式
sql>select log_mode,force_logging from v$database;
1)sql>shutdown normal/immediate;
2)sql>startup mount;
3)sql>alter database archivelog;
4)sql>alter system set log_archive_dest_1='location=/u01/app/archivelog';
5)sql>alter database open;
6)sql>archive log list;
2. 将Primary数据库置为Force Logging模式
sql>Alter database force logging
3. 配置Primary数据库的初始化参数
alter database force logging;
alter system set db_unique_name=orcl scope=spfile;
alter system set LOG_ARCHIVE_CONFIG='DG_CONFIG=(orcl,orcldg,orcldg2)' scope=both;
alter system set log_archive_max_processes=10 scope=both;
alter system set LOG_ARCHIVE_DEST_1='LOCATION=/u01/app/archivelog VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=orcl' scope=both;
alter system set LOG_ARCHIVE_DEST_2='SERVICE=standby LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcldg' scope=both;
alter system set LOG_ARCHIVE_DEST_3='SERVICE=standby2 LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcldg2' scope=both;
alter system set LOG_ARCHIVE_DEST_STATE_1='ENABLE';
alter system set LOG_ARCHIVE_DEST_STATE_2='DEFER';
alter system set LOG_ARCHIVE_DEST_STATE_3='DEFER';
alter system set REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE scope=spfile;
--配置 standby 角色的参数用于角色转换
alter system set FAL_SERVER=standby,standby2;
alter system set FAL_CLIENT=primary;
alter system set DB_FILE_NAME_CONVERT='/u01/app/oracle/oradata/orcldg/','/u01/app/oracle/oradata/orcl/','/u01/app/oracle/oradata/orcldg2/','/u01/app/oracle/oradata/orcl/' scope=spfile;
alter system set LOG_FILE_NAME_CONVERT='/u01/app/oracle/oradata/orcldg/','/u01/app/oracle/oradata/orcl/','/u01/app/oracle/oradata/orcldg2/','/u01/app/oracle/oradata/orcl/' scope=spfile;
alter system set STANDBY_FILE_MANAGEMENT=AUTO;
DG_CONFIG=(orcl,orcldg) 中的orcl和orcldg指的是db_unique_name
LOG_ARCHIVE_DEST_2='SERVICE=standby';中的orcldg指的是tnsnames.ora中的。
4. 重启后验证,查看相关参数修改信息
Shutdown immediate;
startup;
select name, value from v$parameter
where name in ('db_name',
'db_unique_name',
'log_archive_config',
'log_archive_dest_1',
'log_archive_dest_2',
'log_archive_dest_3',
'log_archive_dest_state_1',
'log_archive_dest_state_2',
'log_archive_dest_state_3',
'remote_login_passwordfile',
'log_archive_format',
'log_archive_max_processes',
'fal_server',
'db_file_name_convert',
'log_file_name_convert',
'standby_file_management');
5. 配置主库的监听和网络服务名
oracle用户下cd $ORACLE_HOME/network/admin
vi listener.ora
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1)
(PROGRAM = extproc)
)
(SID_DESC =
(GLOBAL_DBNAME = orcl)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1)
(SID_NAME = orcl)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.57.15)(PORT = 14320))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC14320))
)
)
ADR_BASE_LISTENER = /u01/app/oracle
使得监听器生效
lsnrctl stop
lsnrctl status
lsnrctl start
oracle用户下
vi $ORACLE_HOME/network/admin/tnsname.ora
primary=
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.57.15)(PORT = 14320))
)
(CONNECT_DATA =
(ORACLE_SID= orcl)
)
)
standby=
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.57.16)(PORT = 14320))
)
(CONNECT_DATA =
(ORACLE_SID= orcl)
)
)
standby=
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.57.17)(PORT = 14320))
)
(CONNECT_DATA =
(ORACLE_SID= orcl)
)
)
6. 创建Standby Redologs
配置细节:确保Standby Redologs的文件大小与Primary数据库的Online Redologs文件大小一致。
一般而言,standbyredo 日志文件组数要比 primary 数据库的 online redo 日志文件组数至少多一个 。
推荐 standbyredo 日志组数量基于 primary 数据库的线程数(这里的线程数可以理解为 rac 结构中的 rac节点数)。
有一个推荐的公式可以做参考:(每线程的日志组数+1)*最大线程数
创建命令:
alter database add standby logfile group 4 ('/u01/app/oracle/oradata/orcl/standby04a.log','/u01/app/oracle/oradata/orcl/standby04b.log') size 50M;
alter database add standby logfile group 5 ('/u01/app/oracle/oradata/orcl/standby05a.log','/u01/app/oracle/oradata/orcl/standby05b.log') size 50M;
alter database add standby logfile group 6 ('/u01/app/oracle/oradata/orcl/standby06a.log','/u01/app/oracle/oradata/orcl/standby06b.log') size 50M;
alter database add standby logfile group 7 ('/u01/app/oracle/oradata/orcl/standby07a.log','/u01/app/oracle/oradata/orcl/standby07b.log') size 50M;
二、STANDBY数据库配置和操作:
1.创建standby实例
set oracle_sid = orcl
2. 再配置对应的监听和tnsname.ora文件
oracle 用户下修改listener.ora
vi $ORACLE_HOME/network/admin/listener.ora
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1)
(PROGRAM = extproc)
)
(SID_DESC =
(GLOBAL_DBNAME = orcl)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1)
(SID_NAME = orcl)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.57.16)(PORT = 14320))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC14320))
)
)
ADR_BASE_LISTENER = /u01/app/oracle
oracle用户修改tnsnames.ora
vi $ORACLE_HOME/network/admin/tnsnames.ora
primary=
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.57.15)(PORT = 14320))
)
(CONNECT_DATA =
(ORACLE_SID= orcl)
)
)
standby=
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.57.16)(PORT = 14320))
)
(CONNECT_DATA =
(ORACLE_SID= orcl)
)
)
standby2=
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.57.17)(PORT = 14320))
)
(CONNECT_DATA =
(ORACLE_SID= orcl)
)
)
listener_standby=
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.57.16)(PORT = 14320))
)
)
查看备库监听状态
![7917568-a85d06463a54dd8c.png](https://upload-images.jianshu.io/upload_images/7917568-a85d06463a54dd8c.png)
测试主库和备库直接监听器是否正常,可以用如下命令:
tnsping primary
tnsping standby;
3. 创建Standby的pfile
主库执行create pfile='/home/oracle/initorcldg.ora' from spfile;
修改initorcldg.ora
*.dispatchers='(PROTOCOL=TCP) (SERVICE=orclXDB)'
*.db_unique_name=orcldg
*.log_archive_config='dg_config=(orcl,orcldg,orcldg2)'
*.log_archive_dest_1='LOCATION=/u01/app/archivelog LGWR VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=orcldg'
*.db_file_name_convert='/u01/app/oracle/oradata/orcl/','/u01/app/oracle/oradata/orcldg/','/u01/app/oracle/oradata/orcldg2/','/u01/app/oracle/oradata/orcldg/'
*.log_file_name_convert='/u01/app/oracle/oradata/orcl/','/u01/app/oracle/oradata/orcldg/','/u01/app/oracle/oradata/orcldg2/','/u01/app/oracle/oradata/orcldg/'
*.log_archive_dest_state_1=enable
*.local_listener=listener_standby; --非常重要,配置该参数会使监听处于ready状态
---下列参数用于角色切换
*.log_archive_dest_2='SERVICE=primary ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcl'
*.log_archive_dest_3='SERVICE=standby2 ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcldg2'
*.log_archive_dest_state_2=defer
*.log_archive_dest_state_3=defer
*.standby_file_management='AUTO'
*.remote_login_passwordfile=EXCLUSIVE
*.fal_server=primary
*.fal_client=standby
4. 创建Standby2的pfile
主库执行create pfile='/home/oracle/initorcldg.ora' from spfile;
修改initorcldg.ora
*.dispatchers='(PROTOCOL=TCP) (SERVICE=orclXDB)'
*.db_unique_name=orcldg2
*.log_archive_config='dg_config=(orcl,orcldg,orcldg2)'
*.log_archive_dest_1='LOCATION=/u01/app/archivelog LGWR VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=orcldg2'
*.db_file_name_convert='/u01/app/oracle/oradata/orcl/','/u01/app/oracle/oradata/orcldg2/','/u01/app/oracle/oradata/orcldg/','/u01/app/oracle/oradata/orcldg2/'
*.log_file_name_convert='/u01/app/oracle/oradata/orcl/','/u01/app/oracle/oradata/orcldg2/','/u01/app/oracle/oradata/orcldg/','/u01/app/oracle/oradata/orcldg2/'
*.log_archive_dest_state_1=enable
*.local_listener=listener_standby; --非常重要,配置该参数会使监听处于ready状态
---下列参数用于角色切换
*.log_archive_dest_2='SERVICE=primary ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcl'
*.log_archive_dest_3='SERVICE=standby ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcldg'
*.log_archive_dest_state_2=enable
*.log_archive_dest_state_3=defer
*.standby_file_management='AUTO'
*.remote_login_passwordfile=EXCLUSIVE
*.fal_server=primary
*.fal_client=standby2
4. 启动备库到nomount
set oracle_sid = orcl
$ sqlplus "/as sysdba"
SQL> startup nomount pfile='/home/oracle/initorcldg.ora'
SQL>create spfile from pfile='/home/oracle/initorcldg.ora'
注意事项:如果启动的时候出现问题,不能识别到参数文件。可以SQL>startup pfile='/home/oracle/initorcldg.ora'
5. 创建对应的文件目录
6. 创建密钥文件
把主库的 /u01/app/oracle/product/11.2.0/db_1/dbs/orapworcl 复制到 192.168.1.92 备库 /u01/app/oracle/product/11.2.0/db_1/dbs/目录下。使用chown修改权限。
192.168.1.92执行scp -P 50022 [email protected]:/u01/app/oracle/product/11.2.0/db_1/dbs/orapworcl /u01/app/oracle/product/11.2.0/db_1/dbs/
7. 开始使用rman创建standby
7.1 先对Primary数据库进行归档
alter system switch logfile
7.2 备份主库(primary操作)
run{
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
allocate channel c4 type disk;
backup as compressed backupset database format '/u01/app/bak/database_%d_%T_%U.bak';
sql 'alter system switch logfile';
backup as compressed backupset archivelog all format '/u01/app/bak/archivelog_%d_%T_%U.bak' ;
release channel c1;
release channel c2;
release channel c3;
release channel c4;
}
主库执行创建物理备库控制文件RMAN>backup device type disk format '/u01/app/bak/standby_%U.ctl' current controlfile for standby;
将备份传到备库
7.3 RMAN 恢复备库控制文件,启动到mount状态
$rman target /
RMAN> restore standby controlfile from '/rman_backup/standby_0gnpvd65_1_1.ctl';
在这里可以查看一下standby online log
select GROUP#,TYPE,MEMBER FROM V$LOGFILE;
alter database drop standby logfile group 4;
alter database drop standby logfile group 5;
alter database drop standby logfile group 6;
alter database drop standby logfile group 7;
alter database add standby logfile group 4 ('+DATA/orcl/standby04a.log','+DATA/orcl/standby04b.log') size 50M;
alter database add standby logfile group 5 ('+DATA/orcl/standby05a.log','+DATA/orcl/standby05b.log') size 50M;
alter database add standby logfile group 6 ('+DATA/orcl/standby06a.log','+DATA/orcl/standby06b.log') size 50M;
alter database add standby logfile group 7 ('+DATA/orcl/standby07a.log','+DATA/orcl/standby07b.log') size 50M;
7.4 RMAN restore 物理备库
RMAN>run {
allocate channel c2 type disk;
allocate channel c1 type disk;
allocate channel c3 type disk;
allocate channel c4 type disk;
catalog start with '/u01/bak/';
restore database;
release channel c1;
release channel c2;
release channel c3;
release channel c4;
}
7.5 检测主库备库连接
sqlplus sys/oracle@standby as sysdba
sqlplus sys/oracle@primary as sysdba
8. 恢复成功后,开始开启archive_dest_state_2进程(primary操作)
Alter system set log_archive_dest_state_2=enable
;
9.将备库置于自动恢复状态
开始应用redo日志SQL〉recover managed standby database disconnect from session;
恢复数据完毕后,你可以关闭Redo应用SQL>alter database recover managed standby database cancel;
然后以只读的方式打开数据库SQL>alter database open read only
这可是新功能哦,可以一边在打开的模式下,一边接受redo日志。但是你必须保证有standby redolog这个文件。要不然可是打不开的
alter database recover managed standby database using current logfile disconnect from session;
10.到这里大功告成。你可以选择喝杯咖啡,验证后续的正常情况。
(1)查询当前库的角色和保护模式:
SQL>select name,database_role,db_unique_name,open_mode,protection_mode,protection_level,switchover_status,supplemental_log_data_pk,supplemental_log_data_ui from v$database;
(2)查看进程的活动状态:SQL> select process,client_process,sequence#,status from v$managed_standby;
(3)检查REDO应用进度:select dest_name,archived_thread#,archived_seq#,applied_thread#,applied_seq#,db_unique_name from v$archive_dest_status where status='VALID';
(4)查看同步情况select MAX(SEQUENCE#) FROM V$ARCHIVED_LOG;
select sequence#,applied from v$archived_log;
(5)查看online&standby日志select GROUP#,TYPE,MEMBER FROM V$LOGFILE;
(6)dataguard事件select dest_id,error,status,log_sequence,applied_scn from v$archive_dest where dest_id=2;
select message from v$dataguard_status;
(7)启动顺序
先启动备库,再启动主库,如果顺序相反,主库实例会自动中断,数据库无法启动,并会在alert.log文件中留下如下的信息:
关库时,先关主库,再关备库
11.日志应用
- 日志应用
--将备库置与应用日志模式状态
SQL> alter database recover managed standby database disconnect from session;
--取消备库的自动恢复
SQL> alter database recover managed standby database cancel;
--Open只读模式(Dataguard只能启动到readonly模式)
SQL> alter database open;
--打开实时应用状态模式
SQL> alter database recover managed standby database using current logfile disconnect;