ORACLE 19C ADG的搭建:
所需环境:两台服务器
主库:192.168.100.19 主机名:oracle19c 预装了oracle19c-db软件 监听和库都是正常的
备库:192.168.100.20 主机名:oracle19c-dg 预装了oracle19c-db软件 (无监听,无数据库)
0 最重要的一步,关闭主备库所在操作系统的防火墙和SELINUX
1 修改/etc/hosts文件,将主从的ip和主机名添加进去(主备两边都要做)
192.168.100.19 oracle19c
192.168.100.20 oracle19c-dg
1.1 ping对方主机名看看是否连通
主:ping oracle19c-dg
备:ping oracle19c
2 主库启动到FORCE LOGGING(强制日志)
简单点来说:强制日志就是对数据库中所有的操作都产生日志信息,并将该信息写入到联机重做日志文件中(ONLINE REDO LOG)。FORCE LOGGING默认是不开启的。如果你要是配置ADG的话,那么强制日志必须处于开启状态。
alter database force logging;
select force_logging from v$database;
FORCE_LOGGING
---------------------------------------
YES
看到YES就成功了
如何关闭强制日志:
alter database no force logging;
select force_logging from v$database;
FORCE_LOGGING
---------------------------------------
NO
3 检查主库是否在归档模式下
archive log list;
如果不是的话就设置到归档模式:
使用oracle用户执行下面的创建目录命令
mkdir -p /u01/app/oracle/oradata/ORCL/arch
进入到ORACLE数据库
sqlplus / as sysdba
设置归档目录
alter system set log_archive_dest_1='location=/u01/app/oracle/oradata/ORCL/arch' scope=both;
关闭数据库
shutdown immediate
将数据库启动到mount状态
startup mount
开启归档模式
alter database archivelog;
启动数据库
alter database open;
再次检查归档状态
archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /u01/app/oracle/oradata/ORCL/arch
Oldest online log sequence 3
Next log sequence to archive 5
Current log sequence 5
看到上面信息证明已经开启了。
3.1 给主库添加stand by备用日志组 ,要比现有的日志组多一组。
select group#, members, bytes from v$log;
GROUP# MEMBERS BYTES
---------- ---------- ----------
1 1 209715200
2 1 209715200
3 1 209715200
现在我有三组,按照上面的说法那么我的standby日志组就要有四组。standby日志组是给切换以后的备库用的。
select member from v$logfile;(找到你的redo的位置)
MEMBER
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/ORCL/redo03.log
/u01/app/oracle/oradata/ORCL/redo02.log
/u01/app/oracle/oradata/ORCL/redo01.log
添加standby日志
alter database add standby logfile '/u01/app/oracle/oradata/ORCL/stdredo01.log' size 200M;
alter database add standby logfile '/u01/app/oracle/oradata/ORCL/stdredo02.log' size 200M;
alter database add standby logfile '/u01/app/oracle/oradata/ORCL/stdredo03.log' size 200M;
alter database add standby logfile '/u01/app/oracle/oradata/ORCL/stdredo04.log' size 200M;
select member from v$logfile;
MEMBER
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/ORCL/redo03.log
/u01/app/oracle/oradata/ORCL/redo02.log
/u01/app/oracle/oradata/ORCL/redo01.log
/u01/app/oracle/oradata/ORCL/stdredo01.log
/u01/app/oracle/oradata/ORCL/stdredo02.log
/u01/app/oracle/oradata/ORCL/stdredo03.log
/u01/app/oracle/oradata/ORCL/stdredo04.log
添加完成
4 为了让ADG库起到更好的作用,我们需要修改LINUX内核参数。
将这两个参数加入到sysctl.conf文件中并sysctl -p生效(用ROOT用户)
vi /etc/sysctl.conf
net.ipv4.tcp_rmem = 4096 87380 6291456
net.ipv4.tcp_wmem = 4096 16384 4194304
添加后生效
sysctl -p
注:net.ipv4.tcp_r(w)mem含义为:自动优化所使用的接收缓冲区
5 将监听修改为静态监听(强烈推荐。因为我们要加入网络优化参数,如果你不加参数也可以)
切换到监听所在目录(主备库都要做)
cd $ORACLE_HOME/network/admin
vi listener.ora(创建静态监听)这是主库的
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = /u01/app/oracle/product/19c/db_1/)
(PROGRAM = extproc)
)
(SID_DESC =
(SDU=32767)
(SID_NAME = orcl)
(GLOBAL_DBNAME = orcl)
(ORACLE_HOME = /u01/app/oracle/product/19c/db_1)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(SEND_BUF_SIZE=1406250)
(RECV_BUF_SIZE=1406250)
(ADDRESS = (PROTOCOL = TCP)(HOST = oracle19c)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
ADR_BASE_LISTENER = /u01/app/oracle
修改完了以后重新加载监听 lsnrctl reload
然后检查监听状态 lsnrctl status
vi listener.ora(创建静态监听)这是备库的
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = /u01/app/oracle/product/19c/db_1/)
(PROGRAM = extproc)
)
(SID_DESC =
(SDU=32767)
(SID_NAME = orcl)
(GLOBAL_DBNAME = orcl)
(ORACLE_HOME = /u01/app/oracle/product/19c/db_1)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(SEND_BUF_SIZE=1406250)
(RECV_BUF_SIZE=1406250)
(ADDRESS = (PROTOCOL = TCP)(HOST = oracle19c-dg)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
ADR_BASE_LISTENER = /u01/app/oracle
6 修改tnsname.ora文件 主备库都添加下面的内容
注释:这是添加主备库的别名,其中MD代表MASETDATABASE 主库别名;
SD代表SLAVEDATABASE 备库别名。
vi tnsnames.ora(先修改主库的,里面有东西的话不用管直接添加下列内容)
MD =
(DESCRIPTION =
(SDU=32767)
(SEND_BUF_SIZE=1406250)
(RECV_BUF_SIZE=1406250)
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = oracle19c)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
SD =
(DESCRIPTION =
(SDU=32767)
(SEND_BUF_SIZE=1406250)
(RECV_BUF_SIZE=1406250)
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = oracle19c-dg)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
vi tnsnames.ora(修改备库的,里面有东西的话不用管直接添加下列内容)
MD =
(DESCRIPTION =
(SDU=32767)
(SEND_BUF_SIZE=1406250)
(RECV_BUF_SIZE=1406250)
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = oracle19c)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
SD =
(DESCRIPTION =
(SDU=32767)
(SEND_BUF_SIZE=1406250)
(RECV_BUF_SIZE=1406250)
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = oracle19c-dg)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
下列连通性实验必须保证主备库监听都是开启的:
主库在oracle用户下使用:tnsping SD
[oracle@oracle19c admin]$ tnsping SD
TNS Ping Utility for Linux: Version 19.0.0.0.0 - Production on 11-FEB-2022 14:47:33
Copyright (c) 1997, 2019, Oracle. All rights reserved.
Used parameter files:
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (SDU=32767) (SEND_BUF_SIZE=1406250) (RECV_BUF_SIZE=1406250) (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = oracle19c-dg)(PORT = 1521))) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl)))
OK (10 msec)
看到0k就代表成功
备库在oracle用户下使用:tnsping MD
[oracle@oracle19c-dg admin]$ tnsping md
TNS Ping Utility for Linux: Version 19.0.0.0.0 - Production on 11-FEB-2022 14:47:43
Copyright (c) 1997, 2019, Oracle. All rights reserved.
Used parameter files:
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (SDU=32767) (SEND_BUF_SIZE=1406250) (RECV_BUF_SIZE=1406250) (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = oracle19c)(PORT = 1521))) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl)))
OK (0 msec)
看到OK就代表成功
7 复制密码文件(从主库到备库):
cd $ORACLE_HOME/dbs
scp orapworcl oracle@oracle19c-dg:$ORACLE_HOME/dbs
(传输过程出现这玩意:Are you sure you want to continue connecting (yes/no)? 必须 输入yes)
拷贝完成后测试一下:
sqlplus sys/oracle@MD as sysdba
SQL*Plus: Release 19.0.0.0.0 - Production on Fri Feb 11 15:57:31 2022
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0
SQL> exit
8 主库添加下列参数:
alter system set job_queue_processes=10 scope=spfile; --作业进程限制 可加可不加看你具体需求
alter system set db_unique_name='orclmd' scope=spfile; (由于我们使用的是一个高可用集群,所以集群下每个节点都需要有一个唯一名) orclmd=orcl(实例名)md(masterdatabase)
本地监听只会注册你规定好的别名:
alter system set local_listener='MD' scope=spfile;
alter system set global_names=true scope=both; --开启全局名称
ALTER DATABASE RENAME GLOBAL_NAME TO orcl; --设置你的全局名称 要和非DG集群时你的实例名保持一致
--db_file_name_convert :在使用数据复制时,指定你的主备库的数据复制路径
alter system set db_file_name_convert='/u01/app/oracle/oradata/ORCL','/u01/app/oracle/oradata/ORCL' scope=spfile;
alter system set db_file_name_convert='/u01/app/oracle/oradata/ORCL','/u01/app/oracle/oradata/ORCL' scope=spfile; (主库) (备库)
如果你的数据文件有多个路径(两个路径:/u01/app/oracle/oradata/ORCL和/u01/app/oracle/oradata/ORCL1)
主库第一个路径 对应的备库的第一路径
alter system set db_file_name_convert='/u01/app/oracle/oradata/ORCL','/u01/app/oracle/oradata/ORCL' ,'/u01/app/oracle/oradata/ORCL1','/u01/app/oracle/oradata/ORCL1'scope=spfile;
主库第二路径 对应备库第二路径
--log_file_name_convert:在使用数据复制时,指定你的主备库的日志复制路径(online REDO和standby redo)
alter system set log_file_name_convert='/u01/app/oracle/oradata/ORCL','/u01/app/oracle/oradata/ORCL/redo' scope=spfile;
--FAL_CLIENT 设定FAL的客户端名称,一般为本地在tnsnames.ora中的别名,改参数只在备库角色时有效,但是ORACLE建议你还是在两边都是要设置,为了方便角色切换。
alter system set fal_client='MD' scope=spfile;
--这个参数设定备库从哪里获取到归档日志,一般设定为对方(如果是主库的就相对于是备库,如果备库的话就相对于是主库)在tnsnames.ora文件里的别名。
alter system set fal_server='SD' scope=spfile;
--启动db接收或发送redo data,包括所有库的db_unique_name
alter system set log_archive_config = 'DG_CONFIG=(orclmd,orclsd)' scope=spfile;
重新指定归档目录
alter system set log_archive_dest_1='location=/u01/app/oracle/oradata/ORCL/arch valid_for=(all_logfiles,all_roles) db_unique_name=orclmd' scope=spfile;
alter system set log_archive_dest_2='service=SD LGWR SYNC AFFIRM valid_for=(all_logfiles,primary_role) db_unique_name=orclsd' scope=spfile;
-归档目录的状态
alter system set LOG_ARCHIVE_DEST_STATE_1='ENABLE';
alter system set LOG_ARCHIVE_DEST_STATE_2='ENABLE';
--该参数控制是否自动在备库中建立主库新建的数据文件
注意以下两点:
1 他不会自动创建日志文件
2 如果数据文件重名会覆盖原有的数据文件
alter system set standby_file_management='AUTO' scope=spfile;
--设置一个叫做orcl的服务
alter system set service_names='orcl' scope=spfile;
--设置归档的进程数据量
alter system set log_archive_max_processes=4 scope=spfile;
9 修改备库参数
9.1 在主库中把spfile备份出一个pfile出来,方便我们修改
cd $ORACLE_HOME/dbs
strings spfileorcl.ora > orcl_init.ora
scp orcl_init.ora oracle@oracle19c-dg:$ORACLE_HOME/dbs
9.2 进入到备库的$ORACLE_HOME/dbs下
cd $ORACLE_HOME/dbs
vi orcl_init.ora(把这种orcl.__的统统删除)
(修改下列参数)
*.db_unique_name='orclsd'
*.global_names=TRUE
*.local_listener='SD'
*.fal_client='SD'
*.fal_server='MD'
*.log_archive_config='DG_CONFIG=(orclmd,orclsd)' --这个位置不需要修改了
*.log_archive_dest_1='location=/u01/app/oracle/oradata/ORCL/arch valid_for=(all_logfiles,all_roles) db_unique_name=orclsd'
*.log_archive_dest_2='service=MD LGWR SYNC AFFIRM valid_for=(all_logfiles,primary_role) db_unique_name=orclmd'
*.log_archive_format='ARC_%t_%S_%r.arc' (我主库在开归档时没有改,你们按需修改归档命名格式)
创建归档目录()
mkdir -p /u01/app/oracle/oradata/ORCL/arch
创建adump目录
mkdir -p /u01/app/oracle/admin/orcl/adump
创建redo目录
mkdir -p /u01/app/oracle/oradata/ORCL/redo
备库的参数文件整体信息:
*.audit_file_dest='/u01/app/oracle/admin/orcl/adump'
*.audit_trail='db'
*.compatible='19.0.0'
*.control_files='/u01/app/oracle/oradata/ORCL/control01.ctl','/u01/app/oracle/oradata/ORCL/control02.ctl'#Restore Controlfile
*.db_block_size=8192
*.db_file_name_convert='/u01/app/oracle/oradata/ORCL','/u01/app/oracle/oradata/ORCL'
*.db_name='orcl'
*.db_unique_name='orclsd'
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=orclXDB)'
*.fal_client='SD'
*.fal_server='MD'
*.global_names=TRUE
*.local_listener='SD'
*.log_archive_config='DG_CONFIG=(orclmd,orclsd)'
*.log_archive_dest_1='location=/u01/app/oracle/oradata/ORCL/arch valid_for=(all_logfiles,all_roles) db_unique_name=orclsd'
*.log_archive_dest_2='service=MD LGWR SYNC AFFIRM valid_for=(all_logfiles,primary_role) db_unique_name=orclmd'
*.log_archive_max_processes=4
*.log_file_name_convert='/u01/app/oracle/oradata/ORCL','/u01/app/oracle/oradata/ORCL/redo'
*.LOG_ARCHIVE_DEST_STATE_1='ENABLE'
*.LOG_ARCHIVE_DEST_STATE_2='ENABLE'
*.nls_language='AMERICAN'
*.nls_territory='AMERICA'
*.open_cursors=300
*.pga_aggregate_target=200m
*.processes=300
*.remote_login_passwordfile='EXCLUSIVE'
*.service_names='orcl'
*.sga_target=700m
*.standby_file_management='AUTO'
*.undo_tablespace='UNDOTBS1'
10 重启主备库监听
11 主备库测试连接(如果你之前已经做过了,那么这一步你也没必要做了)
--主
sqlplus sys/oracle@SD as sysdba
--备
sqlplus sys/oracle@MD as sysdba
12 启动备库到nomount状态(用我们刚才做的那个pfile,如果你之前做完了 这一步就不用再做了)
########################################################
startup nomount pfile='/u01/app/oracle/product/19c/db_1/dbs/orcl_init.ora'
数据库正常启动以后创建spfile
create spfile from pfile='/u01/app/oracle/product/19c/db_1/dbs/orcl_init.ora';
关闭数据库
shutdown abort
########################################################
这次再将备库启动到nomount状态 spfile文件创建完成
startup nomount
执行这个命名alter system set standby_file_management='MANUAL';防止redo复制时出错
用该命令检查一下参数:SHOW PARAMETER STANDBY_FILE_MANAGEMENT
NAME TYPE
------------------------------------ ---------------------------------
VALUE
------------------------------
standby_file_management string
MANUAL(看到这玩意就是成功了)
13 在主库通过Rman Duplicate创建备库
主库上执行
rman target sys/oracle@MD auxiliary sys/oracle@SD nocatalog ORACLE用户下执行的
然后开始复制数据库:
duplicate target database for standby from active database nofilenamecheck;
14 在备库执行此语句代表文件全部传输到此
select member from v$logfile;
MEMBER
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/ORCL/redo/redo03.log
/u01/app/oracle/oradata/ORCL/redo/redo02.log
/u01/app/oracle/oradata/ORCL/redo/redo01.log
/u01/app/oracle/oradata/ORCL/redo/stdredo01.log
/u01/app/oracle/oradata/ORCL/redo/stdredo02.log
/u01/app/oracle/oradata/ORCL/redo/stdredo03.log
/u01/app/oracle/oradata/ORCL/redo/stdredo04.log
SQL> select name from v$datafile;
NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/ORCL/system01.dbf
/u01/app/oracle/oradata/ORCL/sysaux01.dbf
/u01/app/oracle/oradata/ORCL/undotbs01.dbf
/u01/app/oracle/oradata/ORCL/users01.dbf
备库复制完之后已经就是mount状态了
15 到备库开启实时日志应用
alter database open;
alter database recover managed standby database using current logfile disconnect from session;
set pagesize 100
归档日志的应用状态
select sequence# ,applied from v$archived_log;
SEQUENCE# APPLIED
---------- ---------------------------
11 YES
12 YES
13 YES
14 IN-MEMORY(不用去关心)
上述语句执行完毕后,把我们这个老朋友给改回来
alter system set standby_file_management='AUTO';
standby_file_management --->备库文件管理“AUTO-自动模式,MANUAL-手动模式”
16 主库建表验证是否备库中存在(后面再做。。。。)
17 关闭备库后重新启动
关闭备库:
shutdown immediate
开启备库:
startup
查看备库状态:
select database_role,protection_mode,open_mode from v$database;
SQL> select database_role,protection_mode,open_mode from v$database;
DATABASE_ROLE PROTECTION_MODE OPEN_MODE
---------------- -------------------- --------------------
PHYSICAL STANDBY MAXIMUM PERFORMANCE READ ONLY
看到这个状态就说明你成功了
recover managed standby database using current logfile disconnect from session; (备库应用日志进行介质恢复)
SQL> recover managed standby database using current logfile disconnect from session;
Media recovery complete.
SQL> select database_role,protection_mode,open_mode from v$database;
DATABASE_ROLE PROTECTION_MODE OPEN_MODE
---------------- -------------------- --------------------
PHYSICAL STANDBY MAXIMUM PERFORMANCE READ ONLY WITH APPLY (实时应用日志开启成功)
至此 ADG配置完成