暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

搭建单机DG,一主一备

刘杰坤 2024-11-20
109

单机DG

记录个人学习DG实操的步骤,参考墨天轮https://www.modb.pro/doc/121891优质文档

一、操作前准备

主库实例:prod1
IP:192.168.162.22

备库实例:test1
IP:192.168.162.33

二、主库开归档

主库环境变量

[oracle@active:/home/oracle]$ cat .bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/.local/bin:$HOME/bin export PATH if [ -t 0 ]; then stty intr ^C fi PS1="[`whoami`@`hostname`:"'$PWD]$ ' export PS1 export ORACLE_SID=prod1 export ORACLE_BASE=/u02/app/oracle export ORACLE_HOME=/u02/app/oracle/product/19.0.0/db_1 export NLS_DATE_FORMAT="yyyy-mm-dd HH24:MI:SS" export PATH=$ORACLE_HOME/bin:$PATH:$HOME/bin export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$ORACLE_HOME/lib32:$ORACLE_HOME/rdbms/lib:/lib:/usr/lib export CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib export LANG=C umask 022
复制

设置归档路径

mkdir -p /u02/arch alter system set log_archive_dest_1='location=/u02/arch'; System altered. show parameter log_archive_dest_1; NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ log_archive_dest_1 string location=/u02/arch log_archive_dest_10 string log_archive_dest_11 string log_archive_dest_12 string log_archive_dest_13 string log_archive_dest_14 string log_archive_dest_15 string log_archive_dest_16 string log_archive_dest_17 string log_archive_dest_18 string log_archive_dest_19 string
复制

修改日志模式

shutdown immediate; Database closed. Database dismounted. ORACLE instance shut down. startup mount; ORACLE instance started. Total System Global Area 1593831928 bytes Fixed Size 8897016 bytes Variable Size 1241513984 bytes Database Buffers 335544320 bytes Redo Buffers 7876608 bytes Database mounted. alter database archivelog; Database altered. alter database open; Database altered. archive log list; Database log mode Archive Mode Automatic archival Enabled Archive destination /u02/arch Oldest online log sequence 1 Next log sequence to archive 2 Current log sequence 2
复制

设置FORCE LOGGING

select log_mode,force_logging from v$database; LOG_MODE FORCE_LOGGING ------------ --------------------------------------- ARCHIVELOG NO ALTER DATABASE FORCE LOGGING; Database altered. select log_mode,force_logging from v$database; LOG_MODE FORCE_LOGGING ------------ --------------------------------------- ARCHIVELOG YES
复制

三、修改 pfile 文件

shutdown immediate; Database closed. Database dismounted. ORACLE instance shut down. create pfile from spfile; File created. cd $ORACLE_HOME/dbs ll total 24 -rw-rw---- 1 oracle oinstall 1544 Nov 18 22:22 hc_prod1.dat -rw-r--r--. 1 oracle oinstall 3079 May 14 2015 init.ora -rw-r--r-- 1 oracle oinstall 1142 Nov 18 22:24 initprod1.ora -rw-r----- 1 oracle oinstall 24 Nov 18 19:08 lkPROD1 -rw-r----- 1 oracle oinstall 2048 Nov 18 19:14 orapwprod1 -rw-r----- 1 oracle oinstall 3584 Nov 18 22:22 spfileprod1.ora cp initprod1.ora initprod1bak.ora vi initprod1.ora
复制

编辑pfile文件

DB_UNIQUE_NAME=prod1 LOG_ARCHIVE_CONFIG='DG_CONFIG=(prod1 ,test1)' LOG_ARCHIVE_DEST_1='LOCATION=/u02/arch VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=prod1 ' LOG_ARCHIVE_DEST_2='SERVICE=test1 ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=test1' LOG_ARCHIVE_DEST_STATE_1=ENABLE LOG_ARCHIVE_DEST_STATE_2=ENABLE LOG_ARCHIVE_FORMAT=%t_%s_%r.arc LOG_ARCHIVE_MAX_PROCESSES=3 FAL_SERVER=test1 DB_FILE_NAME_CONVERT='/u02/app/oracle/oradata/test1','/u02/app/oracle/oradata/prod1 ' LOG_FILE_NAME_CONVERT='/u02/app/oracle/oradata/test1','/u02/app/oracle/oradata/prod1 ' STANDBY_FILE_MANAGEMENT=AUTO prod1.__data_transfer_cache_size=0 prod1.__db_cache_size=520093696 prod1.__inmemory_ext_roarea=0 prod1.__inmemory_ext_rwarea=0 prod1.__java_pool_size=16777216 prod1.__large_pool_size=33554432 prod1.__oracle_base='/u02/app/oracle'#ORACLE_BASE set from environment prod1.__pga_aggregate_target=637534208 prod1.__sga_target=956301312 prod1.__shared_io_pool_size=50331648 prod1.__shared_pool_size=318767104 prod1.__streams_pool_size=0 prod1.__unified_pga_pool_size=0 *.audit_file_dest='/u02/app/oracle/admin/prod1/adump' *.audit_trail='db' *.compatible='19.0.0' *.control_files='/u02/app/oracle/oradata/PROD1/control01.ctl','/u02/app/oracle/recovery_area/PROD1/control02.ctl' *.db_block_size=8192 *.db_name='prod1' *.db_recovery_file_dest='/u02/app/oracle/recovery_area' *.db_recovery_file_dest_size=8256m *.diagnostic_dest='/u02/app/oracle' *.dispatchers='(PROTOCOL=TCP) (SERVICE=prod1XDB)' *.local_listener='LISTENER_PROD1' *.log_archive_dest_1='location=/u02/arch' *.memory_target=1508m *.nls_language='AMERICAN' *.nls_territory='AMERICA' *.open_cursors=300 *.processes=640 *.remote_login_passwordfile='EXCLUSIVE' *.undo_tablespace='UNDOTBS1'
复制

pfile起库测试

sqlplus / as sysdba startup nomount pfile=$ORACLE_HOME/dbs/initprod1.ora; ORACLE instance started. Total System Global Area 1593831928 bytes Fixed Size 8897016 bytes Variable Size 1241513984 bytes Database Buffers 335544320 bytes Redo Buffers 7876608 bytes show parameter name; NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ cdb_cluster_name string cell_offloadgroup_name string db_file_name_convert string /u02/app/oracle/oradata/test1, /u02/app/oracle/oradata/prod1 db_name string prod1 db_unique_name string prod1 global_names boolean FALSE instance_name string prod1 lock_name_space string log_file_name_convert string /u02/app/oracle/oradata/test1, NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ /u02/app/oracle/oradata/prod1 pdb_file_name_convert string processor_group_name string service_names string prod1
复制

创建spfile,重新起库

create spfile from pfile; File created. startup force; ORACLE instance started. Total System Global Area 1593831928 bytes Fixed Size 8897016 bytes Variable Size 1241513984 bytes Database Buffers 335544320 bytes Redo Buffers 7876608 bytes Database mounted. Database opened.
复制

四、备库参数文件、密码文件

主备配置互信scp

scp initprod1.ora 192.168.162.33:/u02/app/oracle/product/19.0.0/db_1/dbs/inittest1.ora initprod1.ora 100% 1766 501.8KB/s 00:00 scp orapwprod1 192.168.162.33:/u02/app/oracle/product/19.0.0/db_1/dbs/orapwtest1 orapwprod1 100% 2048 873.7KB/s 00:00
复制

修改备库参数文件

*.db_name='prod1’

vi inittest1.ora DB_UNIQUE_NAME=test1 LOG_ARCHIVE_CONFIG='DG_CONFIG=(test1 ,prod1)' LOG_ARCHIVE_DEST_1='LOCATION=/u02/arch VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=test1 ' LOG_ARCHIVE_DEST_2='SERVICE=prod1 ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=prod1' LOG_ARCHIVE_DEST_STATE_1=ENABLE LOG_ARCHIVE_DEST_STATE_2=ENABLE LOG_ARCHIVE_FORMAT=%t_%s_%r.arc LOG_ARCHIVE_MAX_PROCESSES=3 FAL_SERVER=prod1 DB_FILE_NAME_CONVERT='/u02/app/oracle/oradata/prod1','/u02/app/oracle/oradata/test1 ' LOG_FILE_NAME_CONVERT='/u02/app/oracle/oradata/prod1','/u02/app/oracle/oradata/test1 ' STANDBY_FILE_MANAGEMENT=AUTO test1.__data_transfer_cache_size=0 test1.__db_cache_size=520093696 test1.__inmemory_ext_roarea=0 test1.__inmemory_ext_rwarea=0 test1.__java_pool_size=16777216 test1.__large_pool_size=33554432 test1.__oracle_base='/u02/app/oracle'#ORACLE_BASE set from environment test1.__pga_aggregate_target=637534208 test1.__sga_target=956301312 test1.__shared_io_pool_size=50331648 test1.__shared_pool_size=318767104 test1.__streams_pool_size=0 test1.__unified_pga_pool_size=0 *.audit_file_dest='/u02/app/oracle/admin/test1/adump' *.audit_trail='db' *.compatible='19.0.0' *.control_files='/u02/app/oracle/oradata/TEST1/control01.ctl','/u02/app/oracle/recovery_area/TEST1/control02.ctl' *.db_block_size=8192 *.db_name='prod1' *.db_recovery_file_dest='/u02/app/oracle/recovery_area' *.db_recovery_file_dest_size=8256m *.diagnostic_dest='/u02/app/oracle' *.dispatchers='(PROTOCOL=TCP) (SERVICE=test1XDB)' *.local_listener='LISTENER_TEST1' *.log_archive_dest_1='location=/u02/arch' *.memory_target=1508m *.nls_language='AMERICAN' *.nls_territory='AMERICA' *.open_cursors=300 *.processes=640 *.remote_login_passwordfile='EXCLUSIVE' *.undo_tablespace='UNDOTBS1'
复制

备库创建目录

mkdir -p /u02/app/oracle/admin/PROD1/adump [oracle@standby:/u02/app/oracle/admin]$ ls -ld /u02/app/oracle/oradata/PROD1 drwxr-xr-x 2 oracle oinstall 6 Nov 19 19:02 /u02/app/oracle/oradata/PROD1 ls -ld /u02/arch/ ls: cannot access /u02/arch/: No such file or directory mkdir -p /u02/app/oracle/oradata/PROD1 mkdir -p /u02/arch/
复制

备库启动实例到 nomount 查看参数确认

备库环境变量

[oracle@standby:/home/oracle]$ cat .bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/.local/bin:$HOME/bin export PATH if [ -t 0 ]; then stty intr ^C fi PS1="[`whoami`@`hostname`:"'$PWD]$ ' export PS1 export ORACLE_SID=test1 export ORACLE_BASE=/u02/app/oracle export ORACLE_HOME=/u02/app/oracle/product/19.0.0/db_1 export NLS_DATE_FORMAT="yyyy-mm-dd HH24:MI:SS" export PATH=$ORACLE_HOME/bin:$PATH:$HOME/bin export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$ORACLE_HOME/lib32:$ORACLE_HOME/rdbms/lib:/lib:/usr/lib export CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib export LANG=C umask 022
复制
sqlplus / as sysdba startup nomount pfile=$ORACLE_HOME/dbs/inittest1.ora; ORACLE instance started. Total System Global Area 1593831928 bytes Fixed Size 8897016 bytes Variable Size 1241513984 bytes Database Buffers 335544320 bytes Redo Buffers 7876608 bytes show parameter name; NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ cdb_cluster_name string cell_offloadgroup_name string db_file_name_convert string /u02/app/oracle/oradata/prod1, /u02/app/oracle/oradata/test1 db_name string test1 db_unique_name string test1 global_names boolean FALSE instance_name string test1 lock_name_space string log_file_name_convert string /u02/app/oracle/oradata/prod1, NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ /u02/app/oracle/oradata/test1 pdb_file_name_convert string processor_group_name string service_names string test1
复制

备库创建 spfile 重启实例到 nomount

create spfile from pfile; File created. startup force nomount; ORACLE instance started. Total System Global Area 1593831928 bytes Fixed Size 8897016 bytes Variable Size 1241513984 bytes Database Buffers 335544320 bytes Redo Buffers 7876608 bytes
复制

五、配置主、备库网络

主库静态监听

cd $ORACLE_HOME/network/admin cp listener.ora listenerbak.ora vi listener.ora LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = active)(PORT = 1521)) (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521)) ) ) SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (SID_NAME = prod1) (ORACLE_HOME = /u02/app/oracle/product/19.0.0/db_1) (GLOBAL_DBNAME=prod1) ) ) lsnrctl start 或 lsnrctl reload lsnrctl status
复制

image.png

备库静态监听

cd $ORACLE_HOME/network/admin cp listener.ora listenerbak.ora vi listener.ora LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = standby)(PORT = 1521)) (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521)) ) ) SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (SID_NAME = test1) (ORACLE_HOME = /u02/app/oracle/product/19.0.0/db_1) (GLOBAL_DBNAME=test1) ) ) lsnrctl start 或 lsnrctl reload lsnrctl status
复制

image.png

主备库 tnsnames.ora

cp tnsnames.ora tnsnames.ora.bak vi tnsnames.ora PROD1 = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = active)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = prod1) ) ) TEST1 = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = standby)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = test1) ) )
复制

测试网络

vi /etc/hosts 192.168.162.22 prod1 192.168.162.33 test1
复制

主备库都测试连接一下

ALTER USER sys IDENTIFIED BY oracle; sqlplus sys/oracle@prod1 as sysdba sqlplus sys/oracle@test1 as sysdba
复制

六、rman duplicate 创建物理备库

主备库打开告警日志

cd $ORACLE_BASE cd diag/rdbms/prod1/prod1/trace/ tail -200f alert_prod1.log cd $ORACLE_BASE/diag/rdbms/ cd test1/test1/trace/
复制

rman 登录主备库

主库open

SQL> select status from v$instance; STATUS ------------ OPEN
复制

备库nomount

SQL> select status from v$instance; STATUS ------------ STARTED
复制
rman target sys/oracle@prod1 auxiliary sys/oracle@test1 [oracle@active:/home/oracle]$ rman target sys/oracle@prod1 auxiliary sys/oracle@test1 Recovery Manager: Release 19.0.0.0.0 - Production on Tue Nov 19 17:53:41 2024 Version 19.2.0.0.0 Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved. connected to target database (not started) connected to auxiliary database: TEST1 (DBID=1562428841) RMAN>
复制

执行 rman duplicate

duplicate target database for standby nofilenamecheck from active database;
复制
contents of Memory Script: { sql clone 'alter database mount standby database'; } executing Memory Script sql statement: alter database mount standby database RMAN-00571: =========================================================== RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS =============== RMAN-00571: =========================================================== RMAN-03002: failure of Duplicate Db command at 11/19/2024 18:11:28 RMAN-05501: aborting duplication of target database RMAN-03015: error occurred in stored script Memory Script RMAN-03009: failure of sql command on clone_default channel at 11/19/2024 18:11:28 RMAN-11003: failure during parse/execution of SQL statement: alter database mount standby database ORA-01103: database name 'PROD1' in control file is not 'TEST1'
复制

https://www.cnblogs.com/KT-melvin/p/6792442.html

https://www.cnblogs.com/wcwen1990/p/7091099.html

没有使主库**open、备库nomount导致的错误:** Starting Duplicate Db at 2024-11-19 17:55:25 RMAN-00571: =========================================================== RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS =============== RMAN-00571: =========================================================== RMAN-03002: failure of Duplicate Db command at 11/19/2024 17:55:25 RMAN-05501: aborting duplication of target database RMAN-06403: could not obtain a fully authorized session ORA-01034: ORACLE not available ORA-27101: shared memory realm does not exist Linux-x86_64 Error: 2: No such file or directory Additional information: 4376 Additional information: -1082447029
复制

查看主备库状态

select name,database_role,protection_mode,open_mode from v$database;
复制
SQL> select name,database_role,protection_mode,open_mode from v$database; NAME DATABASE_ROLE PROTECTION_MODE OPEN_MODE --------- ---------------- -------------------- -------------------- PROD1 PRIMARY MAXIMUM PERFORMANCE READ WRITE SQL> select name,database_role,protection_mode,open_mode from v$database; NAME DATABASE_ROLE PROTECTION_MODE OPEN_MODE --------- ---------------- -------------------- -------------------- PROD1 PHYSICAL STANDBY MAXIMUM PERFORMANCE MOUNTED
复制

open 打开备库

alter database open; Database altered. select name,database_role,protection_mode,open_mode from v$database; SQL> select name,database_role,protection_mode,open_mode from v$database; NAME DATABASE_ROLE PROTECTION_MODE OPEN_MODE --------- ---------------- -------------------- -------------------- PROD1 PHYSICAL STANDBY MAXIMUM PERFORMANCE READ ONLY
复制

备库启动 MRP 进程 应用日志

alter database recover managed standby database disconnect from session; Database altered. select name,database_role,protection_mode,open_mode from v$database; NAME DATABASE_ROLE PROTECTION_MODE OPEN_MODE --------- ---------------- -------------------- -------------------- PROD1 PHYSICAL STANDBY MAXIMUM PERFORMANCE READ ONLY WITH APPLY
复制

七、测试

主库切归档

SQL> alter system switch logfile; System altered. SQL> select group#,sequence#,status from v$log; GROUP# SEQUENCE# STATUS ---------- ---------- ---------------- 1 13 CURRENT 2 11 INACTIVE 3 12 ACTIVE SQL> select group#,sequence#,status from v$log; GROUP# SEQUENCE# STATUS ---------- ---------- ---------------- 1 13 CURRENT 2 11 INACTIVE 3 12 INACTIVE
复制

建表插数据测试

CREATE TABLE EMP (EMPNO NUMBER(4) CONSTRAINT PK_EMP PRIMARY KEY, ENAME VARCHAR2(10), JOB VARCHAR2(9), MGR NUMBER(4), HIREDATE DATE, SAL NUMBER(7,2), COMM NUMBER(7,2), DEPTNO NUMBER(2) ); INSERT INTO EMP VALUES (7369,'SMITH','CLERK',7902,to_date('17-12-1980','dd-mm-yyyy'),800,NULL,20); INSERT INTO EMP VALUES (7499,'ALLEN','SALESMAN',7698,to_date('20-2-1981','dd-mm-yyyy'),1600,300,30); INSERT INTO EMP VALUES (7521,'WARD','SALESMAN',7698,to_date('22-2-1981','dd-mm-yyyy'),1250,500,30); INSERT INTO EMP VALUES (7566,'JONES','MANAGER',7839,to_date('2-4-1981','dd-mm-yyyy'),2975,NULL,20); INSERT INTO EMP VALUES (7654,'MARTIN','SALESMAN',7698,to_date('28-9-1981','dd-mm-yyyy'),1250,1400,30); INSERT INTO EMP VALUES (7698,'BLAKE','MANAGER',7839,to_date('1-5-1981','dd-mm-yyyy'),2850,NULL,30); INSERT INTO EMP VALUES (7782,'CLARK','MANAGER',7839,to_date('9-6-1981','dd-mm-yyyy'),2450,NULL,10); INSERT INTO EMP VALUES (7788,'SCOTT','ANALYST',7566,to_date('12-06-1987','dd-mm-yyyy')-85,3000,NULL,20); INSERT INTO EMP VALUES (7839,'KING','PRESIDENT',NULL,to_date('17-11-1981','dd-mm-yyyy'),5000,NULL,10); INSERT INTO EMP VALUES (7844,'TURNER','SALESMAN',7698,to_date('8-9-1981','dd-mm-yyyy'),1500,0,30); INSERT INTO EMP VALUES (7876,'ADAMS','CLERK',7788,to_date('13-06-1987','dd-mm-yyyy')-51,1100,NULL,20); INSERT INTO EMP VALUES (7900,'JAMES','CLERK',7698,to_date('3-12-1981','dd-mm-yyyy'),950,NULL,30); INSERT INTO EMP VALUES (7902,'FORD','ANALYST',7566,to_date('3-12-1981','dd-mm-yyyy'),3000,NULL,20); INSERT INTO EMP VALUES (7934,'MILLER','CLERK',7782,to_date('23-1-1982','dd-mm-yyyy'),1300,NULL,10); commit; select count(*) from emp; SQL> select count(*) from emp; COUNT(*) ---------- 14
复制

备库查看

SQL> select count(*) from sys.emp; select count(*) from sys.emp * ERROR at line 1: ORA-00942: table or view does not exist
复制

当前模式下,没有启用 Real time apply,默认只对归档日志进行应用。
主库切日志组,备库再次查看。

SQL> alter system switch logfile; System altered.
复制
SQL> select count(*) from sys.emp; COUNT(*) ---------- 14
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
1人已赞赏
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论

刘杰坤
关注
暂无图片
获得了6次点赞
暂无图片
内容获得2次评论
暂无图片
获得了30次收藏
目录
  • 单机DG
    • 一、操作前准备
    • 二、主库开归档
      • 主库环境变量
      • 设置归档路径
      • 修改日志模式
      • 设置FORCE LOGGING
    • 三、修改 pfile 文件
      • 编辑pfile文件
      • pfile起库测试
      • 创建spfile,重新起库
    • 四、备库参数文件、密码文件
      • 修改备库参数文件
      • 备库创建目录
      • 备库启动实例到 nomount 查看参数确认
      • 备库创建 spfile 重启实例到 nomount
    • 五、配置主、备库网络
      • 主库静态监听
      • 备库静态监听
      • 主备库 tnsnames.ora
      • 测试网络
    • 六、rman duplicate 创建物理备库
      • 主备库打开告警日志
      • rman 登录主备库
      • 执行 rman duplicate
      • 查看主备库状态
      • open 打开备库
      • 备库启动 MRP 进程 应用日志
    • 七、测试
      • 主库切归档
      • 建表插数据测试