主节点 192.168.46.61 oracleMaster
备节点 192.168.46.62 oracleSlave
如何安装oracle 11g 请参阅我的上一篇文章 linux centos7 静默安装 oracle 11g
登录到sysdba 用户
如果不说明则下面的操作都是在此用户下进行操作
sqlplus / as sysdba
SQL> select name, log_mode, force_logging from v$database;
NAME LOG_MODE FOR
--------- ------------ ---
ORCL NOARCHIVELOG NO
如显示 NO 则当前未启动强日志模式,NOARCHIVELOG 表示当前日志模式未未归档模式
SQL> alter database force logging;
Database altered.
需要重启实例至mount状态
## 关闭数据库
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.
Total System Global Area 1068937216 bytes
Fixed Size 2220200 bytes
Variable Size 318771032 bytes
Database Buffers 742391808 bytes
Redo Buffers 5554176 bytes
Database mounted.
## 开启归档模式
SQL> alter database archivelog;
Database altered.
## 打开数据库
SQL> alter database open;
Database altered.
## 检查一下归档是否已开启
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 9
Next log sequence to archive 11
Current log sequence 11
SQL> select thread#,group#,bytes/1024/1024 from v$log;
THREAD# GROUP# BYTES/1024/1024
---------- ---------- ---------------
1 1 50
1 2 50
1 3 50
当前实例日志组个数为3,所以需要至少需要创建(3+1)* 1 = 4组 standby redolog,大小均为50M。
SQL> col member for a50
SQL> select group#,member from v$logfile;
GROUP# MEMBER
---------- --------------------------------------------------
3 /data/oracle/oradata/orcl/redo03.log
2 /data/oracle/oradata/orcl/redo02.log
1 /data/oracle/oradata/orcl/redo01.log
路径根据上面的改写,因为每个人的安装位置不同
SQL
SQL> alter database add standby logfile group 4 '/data/oracle/oradata/orcl/standby_redo04.log' size 50M;
SQL> alter database add standby logfile group 5 '/data/oracle/oradata/orcl/standby_redo05.log' size 50M;
SQL> alter database add standby logfile group 6 '/data/oracle/oradata/orcl/standby_redo06.log' size 50M;
SQL> alter database add standby logfile group 7 '/data/oracle/oradata/orcl/standby_redo07.log' size 50M;
SQL> set pagesize 100
SQL> col member for a60
SQL> select group#,member from v$logfile order by group#;
GROUP# MEMBER
---------- ------------------------------------------------------------
1 /data/oracle/oradata/orcl/redo01.log
2 /data/oracle/oradata/orcl/redo02.log
3 /data/oracle/oradata/orcl/redo03.log
4 /data/oracle/oradata/orcl/standby_redo04.log
5 /data/oracle/oradata/orcl/standby_redo05.log
6 /data/oracle/oradata/orcl/standby_redo06.log
7 /data/oracle/oradata/orcl/standby_redo07.log
7 rows selected.
查询到的话则表示添加成功
主库
的 listener.ora 文件[oracle@oracleMaster dbs]$ cd $ORACLE_HOME/network/admin/
[oracle@oracleMaster admin]$ vim $ORACLE_HOME/network/admin/listener.ora
SID_LIST_LISTENER=
(SID_LIST=
(SID_DESC=
(GLOBAL_DBNAME= orcl.master)
(ORACLE_HOME=/data/oracle/product/11.2.0/db_01)
(SID_NAME=orcl)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = oracleMaster)(PORT = 1521))
)
)
ADR_BASE_LISTENER = /data/oracle
备库
的listener.ora 文件SID_LIST_LISTENER=
(SID_LIST=
(SID_DESC=
(GLOBAL_DBNAME= orcl.slave)
(ORACLE_HOME=/data/oracle/product/11.2.0/db_01)
(SID_NAME=orcl)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = oracleSlave)(PORT = 1521))
)
)
ADR_BASE_LISTENER = /data/oracle
由于我的主库和备库使用不同的 GLOBAL_DBNAME,所以此处和大多数的教程不同,需要根据你安装的 oracle 去修改对应的 GLOBAL_DBNAME、ORACLE_HOME、SID_NAME
LISTENER:
SID_LIST_LISTENER:
也可以通过sql 查询出来
## 查询GLOBAL_NAME
SQL> select * from global_name;
GLOBAL_NAME
--------------------------------------------------------------------------------
ORCL.MASTER
## 查询SID
SQL> show parameter instance_name;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
instance_name string orcl
glocal_name
对一个数据库的唯一标识,在创建数据库的时候决定,缺省值为db_name.db_domain。在之后对参数文件中db_name 和 db_domain 参数的任何修改都不影响 global_name的值,如果要修改 glocal_name,只能
SQL> alter database rename global_name to <db_name,db_domain>
来进行修改,然后修改相应的参数
service_name
在 oracle 的并行环境中,一个数据库对应多个实例,就需要多个网络服务名,设置比较繁琐。service_name 参数就是为了解决这个问题,该参数对应一个数据库,而不是一个实例,缺省值为 db_name.db_domain,即等于global_name。一个数据库可以对应多个 service_name.
oracle_sid
oracle_sid这个参数是操作系统中用到的,他是描述默认连接的数据库实例。instance_name是数据库参数。而oracle_sid 是操作系统的环境变量,oracle_sid 必须与 instance_name 的值一致。
[oracle@oracleMaster admin]$ lsnrctl reload
[oracle@oracleSlave admin]$ lsnrctl reload
lsnrctl status
vim $ORACLE_HOME/network/admin/tnsnames.ora
增加如下配置,原有配置删除修改为下面的配置
PRI =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.46.61)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl.master)
)
)
STY =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.46.62)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl.slave)
)
)
配置解释
select * from global_name;
将主库的配置 copy 过去即可
scp $ORACLE_HOME/network/admin/tnsnames.ora oracle@oracleSlave:$ORACLE_HOME/network/admin/tnsnames.ora
## 测试连接
[oracle@oracleMaster admin]$ tnsping STY
TNS Ping Utility for Linux: Version 11.2.0.1.0 - Production on 17-JUL-2023 04:02:58
Copyright (c) 1997, 2009, Oracle. All rights reserved.
Used parameter files:
/data/oracle/product/11.2.0/db_01/network/admin/sqlnet.ora
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = oracleSlave)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl.slave) (SID = orcl)))
OK (0 msec)
[oracle@oracleSlave admin]$ tnsping PRI
primary
pfile## 查看主库 spfile 文件位置
SQL> show parameter spfile;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
spfile string /data/oracle/product/11.2.0/db
_01/dbs/spfileorcl.ora
## 创建 pfile 文件用于修改
SQL> create pfile from spfile;
File created.
文件命名方式为initXX.ora XX 为数据库的 SID 通过下面的SQL 查询
具体的配置参数请参考博客 Oracle 11g Dataguard参数详解
show parameter instance_name;
[oracle@oracleMaster dbs]$ vim /$ORACLE_HOME/dbs/initorcl.ora
log_archive_dest_state_N
LOG_ARCHIVE_DEST_n配套使用的参数,默认值为enable
log_archive_dest_state_1=enable
log_archive_dest_1
log_archive_dest_(2-N)
DB_FILE_NAME_CONVERT
LOG_FILE_NAME_CONVERT
FAL_SERVER
向主库或者任意备库的TNS标识符列表
多个主库或者备库使用逗号间隔
fal_server=‘PRI,PRI1’
FAL_CLIENT
发起间隔请求的数据库的TNS名称,当前机器的TNS名称
fal_client=‘PRI’
STANDBY_FILE_MANAGEMENT
这个简单的参数只用于物理备库。该参数设置成AUTO的时候,主库中添加和删除数据文件的同时,备库中也会自动的进行相应的更改。
默认情况下,这个参数的值为MANUAL,这意味着备库上的应用进程不会创建新的数据文件,你需要手动创建它们。
standby_file_management=‘AUTO’
在 *.undo_tablespace='UNDOTBS1'
下面增加如下参数,文件中不能使用中文等特殊字符不然启动不了
*.db_unique_name='PRI'
*.log_archive_config='DG_CONFIG=(PRI,STY)'
*.log_archive_dest_1='location=USE_DB_RECOVERY_FILE_DEST valid_for=(ALL_LOGFILES,ALL_ROLES) db_unique_name=PRI'
*.log_archive_dest_2='service=STY SYNC REOPEN=15 NET_TIMEOUT=15 valid_for=(ALL_LOGFILES,ALL_ROLES) db_unique_name=STY'
*.standby_file_management='AUTO'
*.fal_client='PRI'
*.fal_server='STY'
scp initorcl.ora oracle@oracleSlave:/data/oracle/product/11.2.0/db_01/dbs
重新指定密码
orapwd file=orapworcl password=123456 force=y
将密码传输到 standby 端
scp orapworcl oracle@oracleSlave:$ORACLE_HOME/dbs/orapworcl
primary
设置数据库以 pfile启动[oracle@oracleMaster dbs]$ sqlplus / as sysdba
## 先停掉数据库
SQL> shutdown immediate
## 从刚才修改的 pfile 创建 spfile并重启数据库
SQL> create spfile from pfile='/data/oracle/product/11.2.0/db_01/dbs/initorcl.ora';
File created.
## 启动数据库,检查对pfile的修改已经生效:
SQL> startup
ORACLE instance started.
Total System Global Area 1068937216 bytes
Fixed Size 2220200 bytes
Variable Size 318771032 bytes
Database Buffers 742391808 bytes
Redo Buffers 5554176 bytes
Database mounted.
Database opened.
## 查看归档日志
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /data/oracle/oradata/orcl/archive
Oldest online log sequence 11
Next log sequence to archive 13
Current log sequence 13
上面的 Archive destination 的参数值 /data/oracle/oradata/orcl/archive 可以看出 pfile 已经生效
## 查看数据库是否用spfile启动
SQL> show parameter spfile;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
spfile string /data/oracle/product/11.2.0/db
_01/dbs/spfileorcl.ora
standby
pfilestandby
端初始化参数文件vim /$ORACLE_HOME/dbs/initorcl.ora
在 *.undo_tablespace='UNDOTBS1'
下面替换成如下参数
*.db_unique_name='STY'
*.log_archive_config='DG_CONFIG=(STY,PRI)'
*.log_archive_dest_1='location=USE_DB_RECOVERY_FILE_DEST valid_for=(ALL_LOGFILES,ALL_ROLES) db_unique_name=STY'
*.log_archive_dest_2='service=PRI SYNC REOPEN=15 NET_TIMEOUT=15 valid_for=(ALL_LOGFILES,ALL_ROLES) db_unique_name=PRI'
*.standby_file_management='AUTO'
*.fal_client='STY'
*.fal_server='PRI'
*.log_archive_dest_state_1=enable
*.log_archive_dest_state_2=enable
## 先停掉数据库
SQL> shutdown immediate
## 从刚才修改的 pfile 创建 spfile并重启数据库
SQL> create spfile from pfile='/data/oracle/product/11.2.0/db_01/dbs/initorcl.ora';
File created.
## 以 nomount 状态启动数据库
SQL> startup nomount;
ORACLE instance started.
Total System Global Area 830930944 bytes
Fixed Size 2217912 bytes
Variable Size 641730632 bytes
Database Buffers 180355072 bytes
Redo Buffers 6627328 bytes
[oracle@oracleSlave orcl]$ rman target sys/123456@PRI auxiliary sys/123456@STY
Recovery Manager: Release 11.2.0.1.0 - Production on Mon Jul 17 02:17:16 2023
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
connected to target database: ORCL (DBID=1669896784)
connected to auxiliary database: ORCL (not mounted)
执行恢复:这个命令可以直接恢复数据文件、standby日志文件和控制文件。
RMAN> duplicate target database for standby from active database nofilenamecheck;
退出
RMAN> quit
## 停止数据库
shutdown immediate
## 启动数据库,此时opne_mode 为READ ONLY WITH APPLY
startup
## 查看当前 openmode
select database_role,protection_mode,protection_level,open_mode from v$database;
## 查看归档日志进程
select process,client_process,sequence#,status from v$managed_standby;
## 开启实时日志应用
recover managed standby database using current logfile disconnect from session;
## 查看归档日志进程,此时多了一个进程 MRP0 N/A 20 APPLYING_LOG
select process,client_process,sequence#,status from v$managed_standby;
归档,当有部分日志还没有被应用,主要是因为日志还没归档,可以执行
alter system switch logfile;
查看当前序列号
archive log list;
主库操作
create table test_sync(id varchar2(100));
select count(*) from test_sync;
begin
for i in 1..10000 loop
insert into test_sync values ('ww');
end loop;
end;
/
commit;
从库查看数据是否同步过来
SQL> select count(*) from test_sync;
COUNT(*)
----------
10000
当前ADG配置完成
主库shutdown——>备库取消应用归档日志——>关闭备库——>关闭主库和备库的lsnrctl监听。
1:主库上:SQL> shutdown immediate
2:备库上:SQL> alter database recover managed standby database cancel;
3:备库上:SQL> shutdown immediate
4:主库和备库:
[oracle@oracleMaster ~]$ lsnrctl stop
[oracle@oracleSlave ~]$ lsnrctl stop
启动主库和备库lsnrctl监听——>启动备库——>启动主库——>切换主库日志
1:主库和备库:
[oracle@oracleMaster ~]$ lsnrctl start
[oracle@oracleSlave ~]$ lsnrctl start
2:启动备库:
SQL> startup nomount
SQL> alter database mount standby database;
SQL> alter database open read write;
SQL> alter database recover managed standby database using current logfile disconnect
3:启动主库:
SQL> startup
4:切换主库日志
SQL> alter system switch logfile;
备库将开始应用主库传输过来的归档日志。
select database_role,protection_mode,protection_level,open_mode from v$database;
DATABASE_ROLE PROTECTION_MODE PROTECTION_LEVEL OPEN_MODE
PHYSICAL STANDBY MAXIMUM PERFORMANCE MAXIMUM PERFORMANCE READ ONLY WITH APPLY
select process,client_process,sequence#,status from v$managed_standby;
PROCESS CLIENT_P SEQUENCE# STATUS
ARCH ARCH 0 CONNECTED
ARCH ARCH 0 CONNECTED
ARCH ARCH 18 CLOSING
ARCH ARCH 19 CLOSING
RFS N/A 0 IDLE
RFS UNKNOWN 0 IDLE
RFS UNKNOWN 0 IDLE
MRP0 N/A 20 APPLYING_LOG
RFS LGWR 20 IDLE9 rows selected.
select process,client_process,sequence#,status from v$managed_standby;
SQL> select process,client_process,sequence#,status from v$managed_standby;
PROCESS CLIENT_P SEQUENCE# STATUS
ARCH ARCH 0 CONNECTED
ARCH ARCH 0 CONNECTED
ARCH ARCH 13 CLOSING
ARCH ARCH 12 CLOSING
RFS UNKNOWN 0 IDLE
RFS UNKNOWN 0 IDLE
RFS UNKNOWN 0 IDLE
RFS LGWR 14 IDLE
MRP0 N/A 14 APPLYING_LOG9 rows selected.
show parameter log_archive_dest_2
NAME TYPE VALUE
log_archive_dest_2 string service=PRI SYNC REOPEN=15 NET
_TIMEOUT=15 valid_for=(ALL_LOG
FILES,ALL_ROLES) db_unique_nam
e=PRI
log_archive_dest_20 string
log_archive_dest_21 string
log_archive_dest_22 string
log_archive_dest_23 string
log_archive_dest_24 string
log_archive_dest_25 string
log_archive_dest_26 stringNAME TYPE VALUE
log_archive_dest_27 string
log_archive_dest_28 string
log_archive_dest_29 string
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 0
Current log sequence 20
SQL> recover managed standby database using current logfile disconnect from session;