一、条件(主库完整库、备库只要软件就行!)
--数据库软件:linuxx64_12201_database.zip
--数据库补丁包:p27105253_122010_Linux-x86-64.zip
---Opatch升级包:p6880880_122010_Linux-x86-64.zip
---IP 地址分配
172.16.11.1 testdb
172.16.11.2 teststdydb
主库: testdb
备库: teststdydb
--安装主库软件并且创建数据库
--备库安装数据库软件
--升级主备库Opatch、打补丁包
二、监听、TNS
主库: listener.ora
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = testdb)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
ADR_BASE_LISTENER = /u01/app/oracle
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME =testdb)
(ORACLE_HOME=/u01/app/oracle/product/12.2.0.1/db_1)
(SID_NAME = testdb)
)
(SID_DESC =
(GLOBAL_DBNAME =teststdydb)
(ORACLE_HOME=/u01/app/oracle/product/12.2.0.1/db_1)
(SID_NAME = teststdydb)
)
)
--tnsnames.ora
testdb =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.11.1)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = testdb)
)
)
teststdydb =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.11.2)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = teststdydb)
)
)
--备库
--listener.ora
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = teststdydb)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
ADR_BASE_LISTENER = /u01/app/oracle
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME =testdb)
(ORACLE_HOME=/u01/app/oracle/product/12.2.0.1/db_1)
(SID_NAME = testdb)
)
(SID_DESC =
(GLOBAL_DBNAME =teststdydb)
(ORACLE_HOME=/u01/app/oracle/product/12.2.0.1/db_1)
(SID_NAME = teststdydb)
)
)
--tnsnames.ora
testdb =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.11.1)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = testdb)
)
)
teststdydb =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.11.2)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = teststdydb)
)
)
---重新启动主备库监听!!!
lsnrctl reload
---查询是否是手动注册
lsnrctl status
UNKNOWN
READY
--测试
tnsping testdb
tnsping teststdydb
sqlplus sys/oracle@testdb as sysdba
sqlplus sys/oracle@teststdydb as sysdba
三、修改主库必要参数(CDB)
select force_logging from v$database; --查询是否开启
alter database force logging;--开启强制记录日志
alter database archivelog;--开启归档
alter system set standby_file_management=auto;--手动
alter system set log_archive_dest_1='location=/u01/arch valid_for=(all_logfiles,all_roles) db_unique_name=testdb' scope=spfile;--开启本地归档
alter system set log_archive_dest_2='SERVICE=teststdydb LGWR ASYNC valid_for=(all_logfiles,all_roles) db_unique_name=teststdydb' scope=spfile;--开启远程归档
alter system set log_archive_config='dg_config=(testdb,teststdydb)';--开启DG功能
alter system set fal_client='testdb';--指定源端
alter system set fal_server='teststdydb';--指定目标端
alter system set log_archive_dest_state_2=enable;--启动应用
alter system set log_archive_dest_state_1=enable;--启动应用
alter system set log_archive_format='testdb%t_%s_%r.dbf' scope=spfile;--指定归档文件形式
--源端---先写备端地址,在写源端地址
alter system set db_file_name_convert='/u01/app/oracle/oradata/teststdydb/','/u01/app/oracle/oradata/testdb/' scope=spfile;
alter system set log_file_name_convert='/u01/app/oracle/oradata/teststdydb/','/u01/app/oracle/oradata/testdb/' scope=spfile;
select bytes/1024/1024 from v$log;--查询日志大小
select member from v$logfile;--查询日志位置
select GROUP#,BYTES/1024/1024/1024 G from v$log;--查询组的个数,以及大小
select group#,thread#,members,archived,status from v$log;--查询当前使用的是那个日志组
---添加 standby logfile
alter database add standby logfile group 6 ('/u01/app/oracle/oradata/testdb/standby06a.log',
'/u01/app/oracle/oradata/testdb/standby06b.log') size 1G ;
alter database add standby logfile group 7 ('/u01/app/oracle/oradata/testdb/standby07a.log',
'/u01/app/oracle/oradata/testdb/standby07b.log') size 1G ;
alter database add standby logfile group 8 ('/u01/app/oracle/oradata/testdb/standby08a.log',
'/u01/app/oracle/oradata/testdb/standby08b.log') size 1G ;
alter database add standby logfile group 9 ('/u01/app/oracle/oradata/testdb/standby09a.log',
'/u01/app/oracle/oradata/testdb/standby09b.log') size 1G ;
alter database add standby logfile group 10 ('/u01/app/oracle/oradata/testdb/standby10a.log',
'/u01/app/oracle/oradata/testdb/standby10b.log') size 1G ;
alter database add standby logfile group 11 ('/u01/app/oracle/oradata/testdb/standby11a.log',
'/u01/app/oracle/oradata/testdb/standby11b.log') size 1G ;
---重启主库数据库,使修改参数生效
shut immediate;
startup
---生成 pfile 文件
create pfile from spfile;
---传递密码文件
cd $ORACLE_HOME/dbs
scp orapwtestdb 172.16.11.2:/u01/app/oracle/product/12.2.0.1/db_1/dbs/orapwteststdydb
---传递参数文件
cd $ORACLE_HOME/dbs
scp inittestdb.ora 172.16.11.2:/u01/app/oracle/product/12.2.0.1/db_1/dbs/initteststdydb.ora
四、修改备库参数文件initteststdydb.ora相关参数
--创建audit_file_dest 目录(改成备库的)
mkdir -p /u01/app/oracle/admin/teststdydb/adump
---创建control_files 路径
mkdir -p /u01/app/oracle/oradata/teststdydb/
--修改db_file_name_convert、log_file_name_convert 路径
*.db_file_name_convert='/u01/app/oracle/oradata/testdb','/u01/app/oracle/oradata/teststdydb'
*.log_file_name_convert='/u01/app/oracle/oradata/testdb','/u01/app/oracle/oradata/teststdydb'
--添加db_unique_name参数
*.db_unique_name='teststdydb'
---修改下列参数
*.fal_client='teststdydb'
*.fal_server='testdb'
*.log_archive_dest_1='location=/u01/arch valid_for=(all_logfiles,all_roles) db_unique_name=teststdydb'
*.log_archive_dest_2=''
*.log_archive_format='teststdydb_%t_%s_%r.arc'
----启动备库到nomount
startup nomount;
五、灾备库执行恢复操作
rman target sys/oracle@testdb auxiliary sys/123456@teststdydb
duplicate target database for standby nofilenamecheck from active database;
---上面操作完成,备库就处于mount状态
alter database open;---起库
---备库应用日志
alter database recover managed standby database using current logfile disconnect from session;
alter database recover managed standby database cancel;---取消应用
--查询备库日志是否应用
select sequence#,applied from v$archived_log;
---查询备库进程是否正常
select process,client_process,sequence#,status from v$managed_standby;
SQL> select process, status, thread#,sequence#, block#, blocks from v$managed_standby;
PROCESS STATUS THREAD# SEQUENCE# BLOCK# BLOCKS
--------- ------------ ---------- ---------- ---------- ----------
ARCH CLOSING 1 530 1689600 595
DGRD ALLOCATED 0 0 0 0
DGRD ALLOCATED 0 0 0 0
ARCH CLOSING 1 528 1789952 17
ARCH CLOSING 1 529 1406976 1586
ARCH CLOSING 1 526 1939456 1946
RFS IDLE 0 0 0 0
RFS IDLE 0 0 0 0
RFS IDLE 1 531 1211169 1
LNS CONNECTED 0 0 0 0
LNS CONNECTED 0 0 0 0
RFS IDLE 0 0 0 0
MRP0 APPLYING_LOG 1 531 1211169 2097152
---如果进程不正常;或者数据没有同步如何处理?
select ERROR from v$archive_dest;
select dest_name,status,error from v$archive_dest where rownum<3;
---查询主备库状态
select DATABASE_ROLE,SWITCHOVER_STATUS from v$database;
六、验证
--创建一个测试表;进行增删改操作;在备库验证是否同步
七、修改主备库的模式
最高保护模式(Maximum Protection)
最高性能模式(Maximum Performance)
最高可用性模式(Maximum Availability)
---查询数据库现在是那种模式
select open_mode,PROTECTION_LEVEL,DATABASE_ROLE,SWITCHOVER_STATUS from v$database;
select name,dbid,database_role,protection_mode from v$database;
--切换成 最高可用
alter database set standby database to maximize availability;
--最高性能模式
alter database set standby database to maximize Performance;
--最大保护模式
alter database set standby database to maximize protection;