使用 RMAN 创建备用实例

使用 Oracle Recovery Manager (RMAN ) 可以从主数据库创建备用云实例。

创建归档日志

检查内部部署 Oracle 服务器的日志模式。如果它处于 NOARCHIVELOG 模式,则将其更改为 ARCHIVELOG 模式以创建归档日志。

  1. 查看内部部署服务器的日志模式。
    SQL> select log_mode from v$database;
    当日志模式处于 NOARCHIVELOG 模式时,输出将类似于以下内容:
    LOG_MODE
    ------------
    NOARCHIVELOG
  2. 关闭内部部署实例。
    SQL> shutdown immediate;
    输出看起来类似于以下内容:
    Database closed.
    Database dismounted.
    ORACLE instance shut down.
  3. 装载内部部署数据库。
    SQL> startup mount;
    输出看起来类似于以下内容:
    ORACLE instance started.
    
    Total System Global Area 4966054520 bytes
    Fixed Size		    9144952 bytes
    Variable Size		  922746880 bytes
    Database Buffers	 4026531840 bytes
    Redo Buffers		    7630848 bytes
    Database mounted.
  4. 变更数据库归档日志,然后打开数据库。
    SQL> alter database archivelog;
    SQL> alter database open;
  5. 将日志模式更改为 ARCHIVELOG
    SQL> select log_mode from v$database;
    LOG_MODE
    ------------
    ARCHIVELOG
  6. 显示归档日志列表。
    SQL> archive log list;
    输出看起来类似于以下内容:
    Database log mode	       Archive Mode
    Automatic archival	       Enabled
    Archive destination	       /opt/oracle/product/19c/dbhome_1/dbs/arch
    Oldest online log sequence     10
    Next log sequence to archive   12
    Current log sequence	       12
  7. 归档所有日志。
    SQL> archive log all;
    ORA-00271:there are no logs that need archiving

在 OCI 上实例化备用数据库

使用 Oracle Recovery Manager (RMAN ) 复制 Oracle Cloud Infrastructure (OCI) 上的活动主数据库。此外,还可以从主数据库的备份中复制。

  1. 使用 srvctl用程序停止数据库或使用 SQL*Plus 关闭数据库。
    • $ srvctl stop database -d standby DB_UNIQUE_NAME -o immediate
    • Shutdown immediate;
  2. 连接到数据库。
    $ rman target /
    RMAN> startup nomount;
  3. 从主服务还原备用控制文件。
    在此示例中,ORCLCDB 是内部部署的主数据库。
    RMAN> restore standby controlfile from service 'ORCLCDB';
  4. 如果出现以下错误消息,请重新连接到数据库。
    RMAN-00571: ===========================================================
    RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
    RMAN-00571: ===========================================================
    RMAN-03002: failure of restore command at 09/24/2020 21:10:13
    RMAN-06496: must use the TO clause when the database is mounted or open
    1. 关闭数据库
      RMAN> shutdown;
    2. 连接到数据库
      RMAN> startup nomount;
  5. 使用数据库控制文件而非恢复目录进行还原。
    RMAN> restore standby controlfile from service 'ORCLCDB';
    输出看起来类似于以下内容:
    Starting restore at DD-MM-YY
    using target database control file instead of recovery catalog
    allocated channel: ORA_DISK_1
    channel ORA_DISK_1: SID=182 device type=DISK 
    channel ORA_DISK_1: starting datafile backup set restore
    channel ORA_DISK_1: using network backup set from service ORCLCDB
    channel ORA_DISK_1: restoring control file
    channel ORA_DISK_1: restore complete, elapsed time: 00:00:02
    output file name=/u02/app/oracle/oradata/ORCLCDB_iad1s3/control01.ctl
    output file name=/u03/app/oracle/fast_recovery_area/ORCLCDB_IAD1S3/control02.ctl
    Finished restore at DD-MM-YY
  6. 装载数据库。
    RMAN> alter database mount;
  7. 如果显示类似于以下内容的错误消息,则重新连接到数据库。
    如果未收到错误消息,请转至步骤 8。
    Released channel: ORA_DISK_1
    RMAN-00571: ===========================================================
    RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
    RMAN-00571: ===========================================================
    RMAN-03002: failure of sql statement command at 09/24/2020 21:13:41
    RMAN-06189: current DBID 2822997440 does not match target mounted database (2823031995)
    1. 关闭数据库
      RMAN> shutdown immediate;
      输出看起来类似于以下内容:
      database dismounted
      Oracle instance shutdown
    2. 连接到数据库
      RMAN> startup nomount;
      输出看起来类似于以下内容:
      connected to target database (not started)
      Oracle instance startedTotal System Global Area 14495511760 bytesFixed Size 9151696 bytes
      Variable Size 1912602624 bytes
      Database Buffers 12549357568 bytes
      Redo Buffers 24399872 bytes
    3. 设置数据库标识符。
      RMAN> set DBID=2823031995;
      输出看起来类似于以下内容:
      executing command: SET DBID
  8. 以 OCI 实例的 root 用户身份,将 Oracle 目录的权限更改为打开 (chmod 777)。
    [root@drdb opc]# sudo su
    [root@drdb opc]# cd /opt/
    [root@drdb opt]# ls -lrt
    total 8
    drwx------ 7 root root 4096 Sep 12 00:39 zookeeper
    drwxr-xr-x 6 root root 4096 Sep 12 00:51 oracle
    [root@drdb opt]# chmod 777 oracle/
    [root@drdb opt]# ls -lrt
    total 8
    drwx------ 7 root root 4096 Sep 12 00:39 zookeeper
    drwxrwxrwx 6 root root 4096 Sep 12 00:51 oracle
  9. 从主数据库 (ORCLCDB) 还原数据库。
    RMAN> restore database from service 'ORCLCDB' section size 5G;
    完成交叉检查备份和交叉检查副本后,输出中将显示目录文件列表,如下所示:
    Starting restore at 01-SEP-20
    Starting implicit crosscheck backup at 01-SEP-20
    allocated channel: ORA_DISK_1
    channel ORA_DISK_1: SID=184 device type=DISK
    Finished implicit crosscheck backup at 01-SEP-20
    
    Starting implicit crosscheck copy at 01-SEP-20
    using channel ORA_DISK_1
    Finished implicit crosscheck copy at 01-SEP-20
    
    searching for all files in the recovery area
    cataloging files...
    cataloging done
    
    List of Cataloged Files
    =======================
    File Name: /u03/app/oracle/fast_recovery_area/ORCLCDB_IAD1S3/archivelog/2020_08_27/o1_mf_1_3_hngo1gl6_.arc
    File Name: /u03/app/oracle/fast_recovery_area/ORCLCDB_IAD1S3/archivelog/2020_08_27/o1_mf_1_1_hngnvtgb_.arc
    File Name: /u03/app/oracle/fast_recovery_area/ORCLCDB_IAD1S3/archivelog/2020_08_27/o1_mf_1_2_hngnwdfd_.arc
    
    using channel ORA_DISK_1
    
    channel ORA_DISK_1: starting datafile backup set restore
    channel ORA_DISK_1: using network backup set from service ORCLCDB
    channel ORA_DISK_1: specifying datafile(s) to restore from backup set
    channel ORA_DISK_1: restoring datafile 00001 to /opt/oracle/oradata/ORCLCDB/system01.dbf
    channel ORA_DISK_1: restoring section 1 of 1
    channel ORA_DISK_1: restore complete, elapsed time: 00:00:26
    channel ORA_DISK_1: starting datafile backup set restore
    channel ORA_DISK_1: using network backup set from service ORCLCDB
    channel ORA_DISK_1: specifying datafile(s) to restore from backup set
    channel ORA_DISK_1: restoring datafile 00003 to /opt/oracle/oradata/ORCLCDB/sysaux01.dbf
    channel ORA_DISK_1: restoring section 1 of 1
    channel ORA_DISK_1: restore complete, elapsed time: 00:00:15
    channel ORA_DISK_1: starting datafile backup set restore
    channel ORA_DISK_1: using network backup set from service ORCLCDB
    channel ORA_DISK_1: specifying datafile(s) to restore from backup set
    channel ORA_DISK_1: restoring datafile 00004 to /opt/oracle/oradata/ORCLCDB/undotbs01.dbf
    channel ORA_DISK_1: restoring section 1 of 1
    channel ORA_DISK_1: restore complete, elapsed time: 00:00:08
    channel ORA_DISK_1: starting datafile backup set restore
    channel ORA_DISK_1: using network backup set from service ORCLCDB
    channel ORA_DISK_1: specifying datafile(s) to restore from backup set
    channel ORA_DISK_1: restoring datafile 00005 to /opt/oracle/oradata/ORCLCDB/pdbseed/system01.dbf
    channel ORA_DISK_1: restoring section 1 of 1
    channel ORA_DISK_1: restore complete, elapsed time: 00:00:07
    channel ORA_DISK_1: starting datafile backup set restore
    channel ORA_DISK_1: using network backup set from service ORCLCDB
    channel ORA_DISK_1: specifying datafile(s) to restore from backup set
    channel ORA_DISK_1: restoring datafile 00006 to /opt/oracle/oradata/ORCLCDB/pdbseed/sysaux01.dbf
    channel ORA_DISK_1: restoring section 1 of 1
    channel ORA_DISK_1: restore complete, elapsed time: 00:00:08
    channel ORA_DISK_1: starting datafile backup set restore
    channel ORA_DISK_1: using network backup set from service ORCLCDB
    channel ORA_DISK_1: specifying datafile(s) to restore from backup set
    channel ORA_DISK_1: restoring datafile 00007 to /opt/oracle/oradata/ORCLCDB/users01.dbf
    channel ORA_DISK_1: restoring section 1 of 1
    channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
    channel ORA_DISK_1: starting datafile backup set restore
    channel ORA_DISK_1: using network backup set from service ORCLCDB
    channel ORA_DISK_1: specifying datafile(s) to restore from backup set
    channel ORA_DISK_1: restoring datafile 00008 to /opt/oracle/oradata/ORCLCDB/pdbseed/undotbs01.dbf
    channel ORA_DISK_1: restoring section 1 of 1
    channel ORA_DISK_1: restore complete, elapsed time: 00:00:04
    channel ORA_DISK_1: starting datafile backup set restore
    channel ORA_DISK_1: using network backup set from service ORCLCDB
    channel ORA_DISK_1: specifying datafile(s) to restore from backup set
    channel ORA_DISK_1: restoring datafile 00009 to /opt/oracle/oradata/ORCLCDB/ORCLPDB1/system01.dbf
    channel ORA_DISK_1: restoring section 1 of 1
    channel ORA_DISK_1: restore complete, elapsed time: 00:00:07
    channel ORA_DISK_1: starting datafile backup set restore
    channel ORA_DISK_1: using network backup set from service ORCLCDB
    channel ORA_DISK_1: specifying datafile(s) to restore from backup set
    channel ORA_DISK_1: restoring datafile 00010 to /opt/oracle/oradata/ORCLCDB/ORCLPDB1/sysaux01.dbf
    channel ORA_DISK_1: restoring section 1 of 1
    channel ORA_DISK_1: restore complete, elapsed time: 00:00:08
    channel ORA_DISK_1: starting datafile backup set restore
    channel ORA_DISK_1: using network backup set from service ORCLCDB
    channel ORA_DISK_1: specifying datafile(s) to restore from backup set
    channel ORA_DISK_1: restoring datafile 00011 to /opt/oracle/oradata/ORCLCDB/ORCLPDB1/undotbs01.dbf
    channel ORA_DISK_1: restoring section 1 of 1
    channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
    channel ORA_DISK_1: starting datafile backup set restore
    channel ORA_DISK_1: using network backup set from service ORCLCDB
    channel ORA_DISK_1: specifying datafile(s) to restore from backup set
    channel ORA_DISK_1: restoring datafile 00012 to /opt/oracle/oradata/ORCLCDB/ORCLPDB1/users01.dbf
    channel ORA_DISK_1: restoring section 1 of 1
    channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
    Finished restore at 01-SEP-20
  10. 关闭数据库。
    RMAN> Shutdown immediate;
  11. 使用 Server Control (srvctl) 实用程序或 SQL*Plus 重新启动备用数据库。
    • 使用 srvctl 实用程序启动数据库:
      srvctl start instance -d standby DB_UNIQUE_NAME -i standby instance name -o mount
    • 使用 SQL*Plus 启动数据库:
      $ ./sqlplus / as sysdba
  12. 装载数据库。
    SQL> startup mount
    输出看起来类似于以下内容:
    ORACLE instance started.
    
    Total System Global Area 1.4496E+10 bytes
    Fixed Size		    9151696 bytes
    Variable Size		 1912602624 bytes
    Database Buffers	 1.2549E+10 bytes
    Redo Buffers		   24399872 bytes
    Database mounted.
  13. 变更数据库。
    SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
    Database altered.
  14. 查看数据库的角色、线程编号、序列号和操作。
    SQL> SELECT ROLE, THREAD#, SEQUENCE#, ACTION FROM V$DATAGUARD_PROCESS;
    输出看起来类似于以下内容:
    ROLE			    THREAD#  SEQUENCE# ACTION
    ------------------------ ---------- ---------- ------------
    post role transition		  0	     0 IDLE
    recovery apply slave		  0	     0 IDLE
    recovery apply slave		  0	     0 IDLE
    recovery apply slave		  0	     0 IDLE
    recovery apply slave		  0	     0 IDLE
    recovery logmerger		  1	     7 WAIT_FOR_LOG
    managed recovery		  0	     0 IDLE
    archive redo			  0	     0 IDLE
    archive local			  0	     0 IDLE
    redo transport timer		  0	     0 IDLE
    gap manager			  0	     0 IDLE
    archive redo			  0	     0 IDLE
    archive redo			  0	     0 IDLE
    redo transport monitor	0	     0 IDLE
    log writer			  0	     0 IDLE
    
    15 rows selected.
  15. (可选)查看数据库名称和角色。
    SQL> select name, database_role from v$database;
    输出看起来类似于以下内容:
    NAME	  DATABASE_ROLE
    --------- ----------------
    ORCLCDB   PHYSICAL STANDBY
  16. 作为 sysdba,清除所有联机备用 redo 日志
    $ sqlplus “/ as sysdba” 
    SQL> alter system set db_create_online_log_dest_1=<DATA Disk group>; 
    SQL> set pagesize 0 feedback off linesize 120 trimspool on 
    SQL> spool /tmp/clearlogs.sql 
    SQL> select distinct 'alter database clear logfile group '||group#||';' from v$logfile; 
    SQL> spool off 
    SQL> @/tmp/clearlogs.sql 
    SQL> select member from v$logfile;

将 RAC 实例添加到 tnsnames.ora 文件

对于 Oracle Real Application Clusters (Oracle RAC ),将 Oracle RAC 实例添加到 tnsnames.ora 并重新启动监听程序。扫描名称不能按混合配置中的任一方向解析,因此需要配置地址列表以提供高可用性。

  1. 编辑内部部署数据库主机上的 TNSNAMES.ORA 文件。
    Primary db_unique_name = (DESCRIPTION = 
    (SDU=65536) (RECV_BUF_SIZE=134217728) 
    (SEND_BUF_SIZE=134217728) 
    (ADDRESS_LIST = 
    		   (FAILOVER=on)
    (CONNECT_TIMEOUT=3)(RETRY_COUNT=3)
    (ADDRESS = (PROTOCOL = TCP)(HOST = PrimaryDB Node 1IP address)(PORT = {1521|port#})) 
    (ADDRESS = (PROTOCOL = TCP)(HOST = StandbyDB Node 2IP address)(PORT = {1521|port#})) 
    ) 
    (CONNECT_DATA = 
    (SERVER = DEDICATED) 
    (SERVICE_NAME = Primary_db_SID) 
    (UR=A) 
    )) 
    
    Standby db_unique_name = (DESCRIPTION = 
    (SDU=65536) (RECV_BUF_SIZE=134217728) 
    (SEND_BUF_SIZE=134217728) 
    (ADDRESS_LIST = 
    		   (FAILOVER=on)
    (CONNECT_TIMEOUT=3)(RETRY_COUNT=3)
    (ADDRESS = (PROTOCOL = TCP)(HOST = StandbyDB Node 1IP address)(PORT = {1521|port#})) 
    (ADDRESS = (PROTOCOL = TCP)(HOST = StandbyDB Node 2IP address)(PORT = {1521|port#})) 
    ) 
    (CONNECT_DATA = 
    (SERVER = DEDICATED) 
    (SERVICE_NAME = Primary_db_SID) 
    (UR=A) 
    )) 
  2. Oracle Cloud Infrastructure (OCI) 中编辑备用数据库主机上的 TNSNAMES.ORA 文件。
    Standby db_unique_name = (DESCRIPTION = 
    (SDU=65536) (RECV_BUF_SIZE=134217728) 
    (SEND_BUF_SIZE=134217728) 
    (ADDRESS_LIST = 
    		   (FAILOVER=on)
    (CONNECT_TIMEOUT=3)(RETRY_COUNT=3)
    (ADDRESS = (PROTOCOL = TCP)(HOST = StandbyDB Node 1IP address)(PORT = {1521|port#})) 
    (ADDRESS = (PROTOCOL = TCP)(HOST = StandbyDB Node 2IP address)(PORT = {1521|port#})) 
    ) 
    (CONNECT_DATA = 
    (SERVER = DEDICATED) 
    (SERVICE_NAME = Primary_db_SID) 
    (UR=A) 
    )) 
    
    <Primary db_unique_name> = (DESCRIPTION = 
    (SDU=65536) (RECV_BUF_SIZE=134217728) 
    (SEND_BUF_SIZE=134217728) 
    (ADDRESS_LIST = 
    		   (FAILOVER=on)
    (CONNECT_TIMEOUT=3)(RETRY_COUNT=3)
    (ADDRESS = (PROTOCOL = TCP)(HOST = PrimaryDB Node 1IP address)(PORT = {1521|port#})) 
    (ADDRESS = (PROTOCOL = TCP)(HOST = StandbyDB Node 2IP address)(PORT = {1521|port#})) 
    ) 
    (CONNECT_DATA = 
    (SERVER = DEDICATED) 
    (SERVICE_NAME = Primary_db_SID) 
    (UR=A) 
    ))