ORACLE 19.3 ADG搭建及主备切换(A欧博SM+RAC复制到单机+文件系统)

文章正文
发布时间:2024-12-02 07:13

(一) 系统情况说明
系统信息如下:
编号 参数 信息
1 数据库版本 19.3 ,欧博也就是19c的基础版本,虚拟机资源有限
2 主机环境 虚拟机,生产:ASM+RAC、容灾:文件系统+单机
3 操作系统 Red Hat Enterprise Linux Server release 7.6 (Maipo)
4 数据库db_unique_name名称 生产:eoms,容灾dreoms

(这个编辑器无法复制表格)

注:容灾库在要把oracle软件装好,监听启动(没有要建一个)。容灾库无需装库,库是备份恢复过去的。
默认是最大性能模式。可以更改,最大性能模式也是生产容灾最常用的模式,可以根据需要更改数据库同步属性,这里不介绍了。

(二) 主库、容灾库关键参数规划表
因无法添加表格,截图:

image.png

(三) 确认主库处于归档及强制归档模式
主库上操作:
检查是否开归档:
archive log list;
设置归档目录,设置归档日志格式。
设置归档时标准设置,网上有方法很多,就不详细说了,推荐用ASM管理,一般归档目录设置为 +ARCH。
如果数据库没有开归档,则startup mount;
alter database archivelog;
alter database force logging;

(四) 备份主库参数文件
主库上操作:
SQL> create pfile=’/home/oracle/eomspfile.ora’ from spfile;

File created.

SQL> exit
Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0
lseoms01:/home/oracle@db> ls -lrt
total 134976
-rw-r–r-- 1 oracle asmadmin 1865 Aug 24 10:44 eomspfile.ora
这个参数文件很关键,因为容灾库要用这个参数文件。

(五) 设置主库参数
主库上操作:按照参数规划表设置,设置完成后生产库几个主要参数如下:
每个参数出现两次,第一次是生产,第二次是容灾,容灾是搭建完成后获取的,在这里供大家参考。
SQL> show parameter LOG_ARCHIVE_CONFIG

NAME TYPE VALUE

log_archive_config string dg_config=(eoms,dreoms)
SQL>

SQL> show parameter LOG_ARCHIVE_CONFIG

NAME TYPE VALUE

log_archive_config string DG_CONFIG=(eoms,dreoms)
SQL>

SQL> show parameter log_archive_dest_2

NAME TYPE VALUE

log_archive_dest_2 string service=DREOMS lgwr sync NOAFF
IRM delay=0 optional compressi
on=disable max_failure=0 max_c
onnections=1 reopen=300 valid_
for=(online_logfiles,primary_r
ole) db_unique_name=dreoms net
_timeout=30

SQL> show parameter log_archive_dest_2

NAME TYPE VALUE

log_archive_dest_2 string SERVICE=eoms LGWR ASYNC VALID
FOR=(ONLINE_LOGFILES,PRIMARY
ROLE) DB_UNIQUE_NAME=eoms
log_archive_dest_20 string

SQL> show parameter FAL_

NAME TYPE VALUE

fal_client string
fal_server string DREOMS
SQL>

SQL> show parameter FAL_

NAME TYPE VALUE

fal_client string
fal_server string eoms
SQL>

SQL> show parameter file_name_convert

NAME TYPE VALUE

db_file_name_convert string /oradata/DATAFILE/, +DATA/EOMS
/DATAFILE/, /oradata/TEMPFILE/
, +DATA/EOMS/TEMPFILE/
log_file_name_convert string /oradata/LOGFILE/, +DATA/EOMS
pdb_file_name_convert string
SQL>

SQL> show parameter file_name_convert

NAME TYPE VALUE

db_file_name_convert string +DATA/EOMS/DATAFILE/, /oradata
/DATAFILE/, +DATA/EOMS/TEMPFIL
E/, /oradata/TEMPFILE/
log_file_name_convert string +DATA/EOMS/ONLINELOG/, /oradat
a/
pdb_file_name_convert string
SQL>

(六) 主库加standby日志组
主库操作:
为什么加了这么多,因为生产很多,欧博娱乐推荐是生产数量+1
alter database add standby logfile thread 1 group 11 ‘+DATA’ size 200m;
alter database add standby logfile thread 1 group 12 ‘+DATA’ size 200m;
alter database add standby logfile thread 1 group 13 ‘+data’ size 200m;
alter database add standby logfile thread 1 group 14 ‘+data’ size 200m;
alter database add standby logfile thread 1 group 15 ‘+data’ size 200m;
alter database add standby logfile thread 2 group 16 ‘+data’ size 200m;
alter database add standby logfile thread 2 group 17 ‘+data’ size 200m;
alter database add standby logfile thread 2 group 18 ‘+data’ size 200m;
alter database add standby logfile thread 2 group 19 ‘+data’ size 200m;
alter database add standby logfile thread 2 group 20 ‘+data’ size 200m;
alter database add standby logfile thread 2 group 21 ‘+data’ size 200m;

(七) 从主库创建容灾库的控制文件
主库操作:
SQL> SQL> alter database create standby controlfile as ‘/home/oracle/eomscontrol.ctl’;

Database altered.

SQL> exit
Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0
eoms01:/home/oracle@db> ls -lrt
total 134972
-rw-r–r--. 1 oracle oinstall 118408624 Jul 2 16:01 p6880880_122010_Linux-x86-64.zip
drwxr-xr-x. 7 oracle oinstall 182 Jul 15 10:38 sqlt_yxwh
-rw-r–r-- 1 oracle oinstall 14042 Jul 29 09:45 checkdb.log
-rwxr-xr-x 1 oracle oinstall 425 Aug 14 11:02 checkOSW.sh
-rw-r–r-- 1 oracle oinstall 31479 Aug 14 17:38 checkOSW.log
drwxr-xr-x. 3 oracle oinstall 4096 Aug 20 11:12 yz
-rw-r----- 1 oracle asmadmin 19742720 Aug 24 10:41 eomscontrol.ctl
eoms01:/home/oracle@db>

把这个控制文件scp到adg容灾数据库的指定目录下,例如我放在/oradata/control/目录下,这个位置后面要用到:
control_files=’/oradata/control/eomscontrol.ctl’

(八) 设置TNS及监听
主备库分别设置TNS及监听
eoms01:/u01/app/oracle/product/19c/db/network/admin@db> vi tnsnames.ora
EOMS =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.198.130)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = eoms)
)
)

DREOMS =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.198.133)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = dreoms)
)
)

每个节点都要做,包括容灾库。Oralce用户下。

把监听修改为静态的,所谓静态的,就是有LISTENER,还有一个SID_LIST_LISTENER,其他数据库想注册不行,是预防TNS投毒的好方法,扯远了,例子如下:
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(ORACLE_HOME = /u01/app/oracle/product/19c/db)
(SID_NAME = dreoms)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = eomsdr)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
设置完,重启一下监听,tnsping一下TNS测试一下。

(九) 设置密码文件
主备库分别设置自己的密码文件:
orapwd file=ORACLEHOME/dbs/orapweoms1password=xxxxxxxxentries=5;orapwdfile=ORACLE_HOME/dbs/orapweoms1 password=xxxx_xxxx entries=5; orapwd file=ORACLE_HOME/dbs/orapwdreoms password=xxxx_xxxx entries=5;
每个节点都要做,包括容灾库,这是sys的操作系统认证密码,文件名称是orapwsid,密码必须一致。

(十) 备库参数文件修改
上文生生产生成过一个参数文件,把主库的参数文件拷贝到容灾数据库,然后按照主库参数设置修改备库的dbs下的initsid.ora文件(sid注意更换):
注意,DEST路径里面的/一定要加完整,ADG不会自动补齐这个/
备库操作:
dreoms.__data_transfer_cache_size=0
dreoms.__db_cache_size=159383552
dreoms.__inmemory_ext_roarea=0
dreoms.__inmemory_ext_rwarea=0
dreoms.__java_pool_size=0
dreoms.__large_pool_size=4194304
dreoms.__oracle_base=’/u01/app/oracle’#ORACLE_BASE set from environment
dreoms.__pga_aggregate_target=281018368
dreoms.__sga_target=524288000
dreoms.__shared_io_pool_size=16777216
dreoms.__shared_pool_size=327155712
dreoms.__streams_pool_size=0
dreoms.unified_pga_pool_size=0
*.audit_file_dest=’/u01/app/oracle/admin/dreoms/adump’
*.audit_trail=‘db’
*.cluster_database=false
*.compatible=‘19.0.0’
*.control_files=’/oradata/control/eomscontrol.ctl’
*.db_block_size=8192
*.db_create_file_dest=’/oradata’
*.db_name=‘eoms’ ##关键,主备库两边的dbname必须一致
*.db_unique_name=‘dreoms’ ##
*.diagnostic_dest=’/u01/app/oracle’
*.dispatchers=’(PROTOCOL=TCP) (SERVICE=eomsXDB)’
family:dw_helper.instance_mode=‘read-only’
*.log_archive_dest_1=‘location=/oradata/arch’
*.log_archive_format=’%t%s%r.arc’
*.log_archive_max_processes=5
*.nls_language=‘AMERICAN’
*.nls_territory=‘AMERICA’
*.open_cursors=300
*.pga_aggregate_target=265m
*.processes=320
*.remote_login_passwordfile=‘exclusive’
*.resource_manager_plan=‘DEFAULT_PLAN’
*.sga_target=500m
*.standby_file_management=‘AUTO’
dreoms.undo_tablespace=‘UNDOTBS1’

*.LOG_ARCHIVE_CONFIG=‘DG_CONFIG=(dreoms,eoms)’
*.LOG_ARCHIVE_DEST_1=‘LOCATION=/oradata/arch VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=dreoms’
*.LOG_ARCHIVE_DEST_2=‘SERVICE=eoms LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=eoms’
*.FAL_SERVER=‘eoms’
*.DB_FILE_NAME_CONVERT=’+DATA/EOMS/DATAFILE/’,’/oradata/DATAFILE/’,’+DATA/EOMS/TEMPFILE/’,’/oradata/TEMPFILE/’
*.LOG_FILE_NAME_CONVERT=’+DATA/EOMS/ONLINELOG/’, ‘/oradata/’
*.standby_file_management=AUTO

用这个参数文件启动容灾数据库到nomount状态
startup pfile=‘xxxx/inixx.ora’ nomount;
实际上此时并没有数据库,占了内存及进程,等下文用rman把库复制过来。
**
(十一) rman开始备份恢复**
主库节点上操作:
rman target sys/xxxx_ xxxx@EOMS auxiliary sys/xxxx_xxxx @DREOMS

备库起到startup nomount;状态,否则:
RMAN-05500: the auxiliary database must be not mounted when issuing a DUPLICATE command
失败后要重新登录:

run
{
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
allocate AUXILIARY channel c4 type disk;
allocate AUXILIARY channel c5 type disk;
allocate AUXILIARY channel c6 type disk;
DUPLICATE TARGET DATABASE
FOR STANDBY
FROM ACTIVE DATABASE
DORECOVER
NOFILENAMECHECK;
release channel c1;
release channel c2;
release channel c3;
release channel c4;
release channel c5;
release channel c6;
}
备份恢复无报错即可。

(十二) 开启主备同步状态
备库操作:
SQL> select open_mode from v$database;

OPEN_MODE

MOUNTED

SQL> alter database open;

Database altered.

SQL> select open_mode from v$database;

OPEN_MODE

READ ONLY

SQL> alter database recover managed standby database disconnect from session using current logfile;

Database altered.

SQL> select open_mode from v$database;

OPEN_MODE

READ ONLY WITH APPLY

SQL>

(十三) 备库同步启动关闭
备库操作:
关闭日志应用:alter database recover managed standby database cancel;

启动日志应用:alter database recover managed standby database disconnect from session using current logfile;

平日不用,有时候无法同步了,有这两个命令在备库重启一下观察。
(十四) 同步测试
主库上建表blogtest:
– Create table
create table BLOGTEST
(
test1 CLOB,
test2 BLOB,
test3 NCLOB
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
)
compress for all operations;

表的字段选择在数据导入导出等情况下容易出问题的各类LOG字段,并且对表进行压缩,然后向表中复制一行数据,如下图:

image.png

在容灾数据库查看:
表已经在容灾库建立并同步了数据:

image.png

(十五) ADG中谁是TNS、谁是DB_UNIQUE_NAME
几乎所有的ADG,大家都愿意把TNS的名字和DB_UNIQUE_NAME设置成一致的,因此在出现fal_server、log_archive_config、log_archive_dest_2三个都出现DB_UNIQUE_NAME的地方,出现TNS与DB_UNIQUE_NAME的混淆。
看看官方怎么说的:

1.124 FAL_SERVER ##参数前面的编号是oracle对参数的编号,忽略即可。
The value is an Oracle Net service name, which is assumed to be configured properly on the standby database system to point to the desired FAL server.
URL:https://docs.oracle.com/en/database/oracle/oracle-database/19/refrn/FAL_SERVER.html#GUID-4F034B79-AE2A-44E3-8485-E055AA2DDD29

毋容置疑:这个参数是TNS,就这一个吗?不着急,继续向下看。

log_archive_config
1.173 LOG_ARCHIVE_CONFIG
LOG_ARCHIVE_CONFIG enables or disables the sending of redo logs to remote destinations and the receipt of remote redo logs, and specifies the unique database names (DB_UNIQUE_NAME) for each database in the Data Guard configuration.
URL:
https://docs.oracle.com/en/database/oracle/oracle-database/19/refrn/LOG_ARCHIVE_CONFIG.html#GUID-4DABDBE9-04B6-44D2-B93D-DAB15EA71427
可见,LOG_ARCHIVE_CONFIG中,用的是DB_UNIQUE_NAME,最多支持30个容灾库。

1.67 log_archive_dest_2
log_archive_dest_2='service=< TNS alias source_prm> async valid_for=(online_logfiles,primary_role) db_unique_name=source_prm
可见,'service后面跟着的参数,这也是个TNS,但是db_unique_name参数后面跟着的,就是db_unique_name,这是肯定的。

(十六) 主备切换测试
主库执行:
关闭RAC的其他节点,保留一个节点,在主库执行:
SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO STANDBY WITH SESSION SHUTDOWN;
执行完整个命令,主库自动停机,需要启动起来。
SQL> startup nomount;
SQL> alter database mount standby database;
SQL> ALTER DATABASE RECOVER MANAGED standby DATABASE DISCONNECT FROM SESSION;
再继续做下去:
SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-10456: cannot open standby database; media recovery session may be in
Progress
这时候想打开就不行了,如何解决,后面有详细过程。

备库执行:
主库执行完switchover后,备库的可切换状态就变化了,有NOT ALLOWED自动变成TO PRIMARY了,表示可以变成主库了,执行:
ALTER DATABASE COMMIT TO SWITCHOVER TO primary;
同样,数据库切换后自己关闭了,需要启动一下:
SQL> ALTER DATABASE OPEN;
查询状态:
SQL> select switchover_status from v$database;

SWITCHOVER_STATUS

TO STANDBY
SQL> select inst_id,database_role,OPEN_MODE from gv$database;

ID DATABASE_ROLE OPEN_MODE

1 PRIMARY READ WRITE
上述信息表示该库目前为PRIMARY主库,可以进行TO STANDBY操作。

开始验证数据同步:
在备库上写入一行数据:

image.png

第一次切换,原主库处于应用模式是打不开数据库的:
SQL> select inst_id,database_role,OPEN_MODE from gv$database;

INST_ID DATABASE_ROLE OPEN_MODE

1 PHYSICAL STANDBY MOUNTED

SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-10456: cannot open standby database; media recovery session may be in
Progress
如果想验证数据是否同步回来,需要停止同步,然后打开库:
SQL> select inst_id,database_role,OPEN_MODE from gv$database;

INST_ID DATABASE_ROLE OPEN_MODE

1 PHYSICAL STANDBY MOUNTED

SQL> alter database recover managed standby database cancel;

Database altered.

SQL> alter database open;

Database altered.

SQL> select a.*,a.rowid from nxsoft_yg.mytest a where a.test1=‘xxx’;

TEST1 TEST2-------------------- ------------------------------TEST3 ROWID------------------------------ ------------------
xxx xxx xxx AAAR4GAACAAAAWgAAA
可以看到,xxx这一行数据,已经同步回来了。
查询完成后,继续改回应用模式:
SQL> alter database recover managed standby database disconnect from session using current logfile;

Database altered.

SQL> select inst_id,database_role,OPEN_MODE from gv$database;

INST_ID DATABASE_ROLE OPEN_MODE

1 PHYSICAL STANDBY READ ONLY WITH APPLY

状态改回来以后,继续做主备同步测试:
原备库(现主库)增加一条xxx1的数据库(过程略),在原主库(现备库)上查询:
SQL> alter database recover managed standby database disconnect from session using current logfile;

Database altered.

SQL> select inst_id,database_role,OPEN_MODE from gv$database;

INST_ID DATABASE_ROLE OPEN_MODE

1 PHYSICAL STANDBY READ ONLY WITH APPLY

SQL> select a.* from nxsoft_yg.mytest a where a.test1=‘xxx1’;

TEST1 TEST2 TEST3

xxx1 xxx1 xxx1

数据也同步过来的
说明切换前后同步正常。

(十七) 切换回来
主库(原来的备库)
SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO STANDBY WITH SESSION SHUTDOWN;
执行完整个命令,主库自动停机,需要启动起来。
SQL> startup nomount;
SQL> alter database mount standby database;
SQL> ALTER DATABASE RECOVER MANAGED standby DATABASE DISCONNECT FROM SESSION;

备库(原来的主库):

SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO primary;

Database altered.

SQL> ALTER DATABASE OPEN;

Database altered.

SQL>

用命令查询一下:
select inst_id,database_role,OPEN_MODE,SWITCHOVER_STATUS from gvdatabase; 原来生产变成主了: SQL> select inst_id,database_role,OPEN_MODE,SWITCHOVER_STATUS from gvdatabase;

INST_ID DATABASE_ROLE OPEN_MODE SWITCHOVER_STATUS

1 PRIMARY READ WRITE TO STANDBY

备库:
SQL> select inst_id,database_role,OPEN_MODE,SWITCHOVER_STATUS from gv$database;

ID DATABASE_ROLE OPEN_MODE SWITCHOVER_STATUS

1 PHYSICAL STANDBY MOUNTED NOT ALLOWED

打开备库:
SQL> alter database recover managed standby database cancel;

Database altered.

SQL> alter database open;

Database altered.

SQL> alter database recover managed standby database disconnect from session using current logfile;

Database altered.

SQL> select inst_id,database_role,OPEN_MODE,SWITCHOVER_STATUS from gv$database;

ID DATABASE_ROLE OPEN_MODE SWITCHOVER_STATUS

1 PHYSICAL STANDBY READ ONLY WITH APPLY NOT ALLOWED

首页
评论
分享
Top