Oracle 10G DATA GUARD主从复制记录

系统环境介绍

主机(primary)

  • IP:10.4.101.143
  • 软件: Oracle Release 10.2.0.1.0
  • 系统:CentOS release 6.10 (Final)
  • 主库数据库(SID):alphatst

从机(standby)

  • IP:10.4.101.170
  • 软件: Oracle Release 10.2.0.1.0
  • 系统:CentOS release 6.10 (Final)
  • 从库数据库(SID):alphatst(只创建默认数据库即可)

配置前环境准备

  1. 分别在主机和从机的/etc/hosts中添加记录(文件尾添加):
10.4.101.143 primary
10.4.101.170 standby
  1. 分别在主机和从机上创建相同的目录
-- 创建控制文件备份路径(主库从库都创建)
[root@localhost ~]# mkdir /home/oracle/interlib
-- 创建日志文件存放路径(主库从库都创建)
[root@localhost ~]# mkdir /home/oracle/interlib/logs

-- 数据文件存放地址(根据主库目录在从库创建)
[root@localhost ~]# mkdir -p /home/oracle/oradata/Oracle10g/

1 主库配置

1.1 开启归档模式

  1. 检查主库归档状态
[root@localhost ~]# sqlplus / as sysdba
SQL> shutdown immediate; 
-- 打开归档需要先关闭Oracle,然后将数据库启动至mount状态才能修改
SQL> startup mount; 
-- 修改为归档模式 
SQL> alter database archivelog;
SQL> alter database open; 
-- 设置强制归档模式 
SQL> alter database force logging;
-- 查看是否修改成功
SQL> Archive log list ;
-- 显示结果如下代表配置成功
	Database log mode	       Archive Mode
	Automatic archival	       Enabled
	Archive destination	       /home/oracle/interlib/logs/
	Oldest online log sequence     881
	Next log sequence to archive   883
	Current log sequence	       883

1.2. 在主库上建立为备用库日志文件,standby redo log是使用Real Time Apply的必要条件

--最少需要多一组,
SQL> select group#,member from v$logfile;
   GROUP#    MEMBER
-------------------------------------------------------------------
	 3       /usr/local/oracle/oradata/alphatst/redo03.log

	 2       /usr/local/oracle/oradata/alphatst/redo02.log

	 1       /usr/local/oracle/oradata/alphatst/redo01.log
SQL> select bytes/1024/1024 from v$log;
	BYTES/1024/1024
-----------------------
	     50
	     50
	     50
SQL> alter database add standby logfile ('/usr/local/oracle/oradata/alphatst/standby04.log') size 50m;
SQL> alter database add standby logfile ('/usr/local/oracle/oradata/alphatst/standby05.log') size 50m;
SQL> alter database add standby logfile ('/usr/local/oracle/oradata/alphatst/standby06.log') size 50m;
SQL> alter database add standby logfile ('/usr/local/oracle/oradata/alphatst/standby07.log') size 50m;

1.3. 导出主库参数文件并修改

1.3.1 导出参数文件

SQL> create pfile='/home/oracle/interlib/initora.ora' from spfile;

1.3.2 修改参数文件

[oracle@localhost interlib]$ vim initora.ora

alphatst.__db_cache_size=8573157376
alphatst.__java_pool_size=16777216
alphatst.__large_pool_size=16777216
alphatst.__shared_pool_size=1459617792
alphatst.__streams_pool_size=0
*.audit_file_dest='/usr/local/oracle/admin/alphatst/adump'
*.background_dump_dest='/usr/local/oracle/admin/alphatst/bdump'
*.compatible='10.2.0.1.0'
*.control_files='/usr/local/oracle/oradata/alphatst/control01.ctl','/usr/local/oracle/oradata/alphatst/control02.ctl','/usr/local/oracle/oradata/alphatst/control03.ctl'
*.core_dump_dest='/usr/local/oracle/admin/alphatst/cdump'
*.db_block_size=8192
*.db_domain=''
*.db_file_multiblock_read_count=16
*.db_name='alphatst'
*.db_recovery_file_dest='/usr/local/oracle/flash_recovery_area'
*.db_recovery_file_dest_size=2147483648
*.dispatchers='(PROTOCOL=TCP) (SERVICE=alphatstXDB)'
*.job_queue_processes=10
*.open_cursors=300
*.pga_aggregate_target=1647312896
*.processes=2200
*.remote_login_passwordfile='EXCLUSIVE'
*.sessions=540
*.sga_max_size=10083106816
*.sga_target=10083106816
*.shared_pool_size=0
*.undo_management='AUTO'
*.undo_tablespace='UNDOTBS1'
*.user_dump_dest='/usr/local/oracle/admin/alphatst/udump'
# 以下为需要添加的配置
## 主库的唯一名称
*.db_unique_name='primary'
*.archive_lag_target=1800
## FAL指获取归档日志(Fetch Archived Log),设置客户端和服务器
*.fal_client='primary'
*.fal_server='standby'
## 用于控制发送归档日志到远程位置、接收远程归档日志,并指定Data  Guard配置的惟一数据库名
*.log_archive_config='DG_CONFIG=(primary,standby)'
## 设置主库归档路径
*.log_archive_dest_1='LOCATION=/home/oracle/interlib/logs/ VALID_FOR=(all_logfiles,all_roles) db_unique_name=primary'
## 设置对从库的归档服务
*.log_archive_dest_2='service=standby LGWR SYNC AFFIRM valid_for=(online_logfiles,primary_role) db_unique_name=standby'
*.log_archive_dest_state_1='enable'
*.log_archive_dest_state_2='enable'
*.log_archive_format='%t_%s_%r.dbf'
## 主数据库和备用数据库的数据文件转换目录对映
## 格式: *.db_file_name_convert= 主数据库数据文件目录,备用数据库数据文件目录
*.DB_FILE_NAME_CONVERT='/usr/local/oracle/oradata/alphatst/','/usr/local/oracle/oradata/alphatst/'
## 指明主数据库和备用数据库的log文件转换目录对映
## 格式: *. log_file_name_convert=主数据库log目录,备用数据库log目录
*.LOG_FILE_NAME_CONVERT='/home/oracle/interlib/logs/','/home/oracle/interlib/logs/'
## If the STANDBY_FILE_MANAGEMENT parameter on the physical standby database is set to AUTO, 
## any new datafiles created on the primary database are automatically created on the physical standby database
*.standby_file_management='auto'

1.3.3 让主库应用新的配置

# 停止服务
SQL> shutdown immediate;
# 使用新参数文件启动数据库
SQL> startup pfile='/home/oracle/interlib/initora.ora' nomount;
# 创建新的spfile文件
SQL> create spfile from pfile='/home/oracle/interlib/initora.ora';
# 停止服务
SQL> shutdown immediate;
# 启动服务
SQL> startup;

1.4 查看并修改网络监听配置

1.4.1 修改监听配置

[oracle@localhost interlib]$ cd $ORACLE_HOME/network/admin
[oracle@localhost admin]$ vim listener.ora

# listener.ora Network Configuration File: /usr/local/oracle/product/10201/network/admin/listener.ora
# Generated by Oracle configuration tools.

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = PLSExtProc)
      (ORACLE_HOME = /usr/local/oracle/product/10201)
      (PROGRAM = extproc)
    )
    # 数据库服务listener
    (SID_DESC =
      (SID_NAME = alphatst)
      (ORACLE_HOME = /usr/local/oracle/product/10201)
      (GLOBAL_DBNAME = alphatst)
    )

  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
    # 这里的 HOST = primary 为hosts文件中配置的 10.4.101.143 primary
      (ADDRESS = (PROTOCOL = TCP)(HOST = primary)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
    )
  )

[oracle@localhost admin]$ vim tnsnames.ora

# tnsnames.ora Network Configuration File: /usr/local/oracle/product/10201/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.

EXTPROC_CONNECTION_DATA =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
    )
    (CONNECT_DATA =
      (SID = PLSExtProc)
      (PRESENTATION = RO)
    )
  )
# 主库的监听配置
PRIMARY =
    (DESCRIPTION =
    (ADDRESS_LIST =
        (ADDRESS = (PROTOCOL = TCP) (HOST = primary) (PORT = 1521))
    )
    (CONNECT_DATA =
        (SERVICE_NAME = alphatst)
    )
    )
# 从库的监听配置
STANDBY=
    (DESCRIPTION =
    (ADDRESS_LIST =
        (ADDRESS = (PROTOCOL = TCP) (HOST = standby) (PORT = 1521))
    )
    (CONNECT_DATA =
        (SERVICE_NAME = alphatst)
    )
    )

1.4.2 重启监听配置

[oracle@localhost admin]$ lsnrctl stop
[oracle@localhost admin]$ lsnrctl start

1.4.3 检查监听是否配置成功

[oracle@localhost admin]$ tnsping primary

TNS Ping Utility for Linux: Version 10.2.0.1.0 - Production on 15-DEC-2019 17:00:35

Copyright (c) 1997, 2005, Oracle.  All rights reserved.

Used parameter files:


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (HOST = primary) (PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = alphatst)))
OK (0 msec)

[oracle@localhost admin]$ tnsping standby

TNS Ping Utility for Linux: Version 10.2.0.1.0 - Production on 15-DEC-2019 17:03:34

Copyright (c) 1997, 2005, Oracle.  All rights reserved.

Used parameter files:


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (HOST = standby) (PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = alphatst)))
OK (0 msec)

1.5 使用RMAN备份主库

#创建备份存放目录
[oracle@localhost admin]$ mkdir -p /home/oracle/rman_backup
#执行备份
rman>run{
allocate channel c1 type disk;
backup format '/home/oracle/rman_backup/orcl_%T_%s_%p' database;
sql 'alter system archive log current';
backup format '/home/oracle/rman_backup/archive_log_%T_%s_%p' archivelog all;
backup spfile format '/home/oracle/rman_backup/spfile_%u_%T.bak';
release channel c1;
}
# 创建用于构建从库的控制文件
rman>copy current controlfile for standby to '/home/oracle/rman_backup/standby.ctl';

1.6 复制相关文件至从库

#数据文件
[oracle@localhost ~]$ scp -r rman_backup standby:/home/oracle
#参数文件
[oracle@localhost ~]$ cd interlib/
[oracle@localhost interlib]$ scp initora.ora standby:$ORACLE_HOME/dbs
#监听文件
[oracle@localhost interlib]$ cd $ORACLE_HOME/network/admin
[oracle@localhost admin]$ scp -r listener.ora tnsnames.ora standby:$ORACLE_HOME/network/admin
#密码文件(如果有并且记得密码就不用重新生成,只复制)
[oracle@localhost dbs]$ orapwd file=orapwalphatst password=oracle force=y
[oracle@localhost dbs]$ scp -r orapwalphatst standby:$ORACLE_HOME/dbs

1.7 关闭主数据库

SQL> shutdown immediate;

2 从库配置

2.1 修改从库监听

[oracle@localhost ~]$ cd $ORACLE_HOME/network/admin
[oracle@localhost admin]$ vim listener.ora

# listener.ora Network Configuration File: /usr/local/oracle/product/10201/network/admin/listener.ora
# Generated by Oracle configuration tools.

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = PLSExtProc)
      (ORACLE_HOME = /usr/local/oracle/product/10201)
      (PROGRAM = extproc)
    )
    (SID_DESC =
      (SID_NAME = alphatst)
      (ORACLE_HOME = /usr/local/oracle/product/10201)
      (GLOBAL_DBNAME = alphatst)
    )

  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
    # 只用修改此处standby
      (ADDRESS = (PROTOCOL = TCP)(HOST = standby)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
    )
  )
# 重启监听
[oracle@localhost admin]$ lsnrctl stop
[oracle@localhost admin]$ lsnrctl start
# tnsping 测试连接
[oracle@localhost admin]$ tnsping primary
[oracle@localhost admin]$ tnsping standby

2.2 修改参数文件并应用

[oracle@localhost ~]$ cd /home/oracle/interlib/
[oracle@localhost interlib]$ vim initora.ora 

#...
#系统默认的部分省略
#···
# 将上面从主库拷贝过来的参数文件中所有primary替换成standby,standby替换成primary
*.db_unique_name='standby'
*.archive_lag_target=1800
*.fal_client='standby'
*.fal_server='primary'
*.log_archive_config='DG_CONFIG=(primary,standby)'
*.log_archive_dest_1='LOCATION=/home/oracle/interlib/logs/ VALID_FOR=(all_logfiles,all_roles) db_unique_name=standby'
*.log_archive_dest_2='service=primary LGWR SYNC AFFIRM valid_for=(online_logfiles,primary_role) db_unique_name=primary'
*.log_archive_dest_state_1='enable'
*.log_archive_dest_state_2='enable'
*.log_archive_format='%t_%s_%r.dbf'
*.DB_FILE_NAME_CONVERT='/usr/local/oracle/oradata/alphatst/','/usr/local/oracle/oradata/alphatst/'
*.LOG_FILE_NAME_CONVERT='/home/oracle/interlib/logs/','/home/oracle/interlib/logs/'
*.standby_file_management='auto'

#应用参数文件
[oracle@localhost interlib]$ sqlplus / as sysdba
# 停止服务
SQL> shutdown immediate;
# 使用新参数文件启动数据库
SQL> startup pfile='/home/oracle/interlib/initora.ora' nomount;
# 创建新的spfile文件
SQL> create spfile from pfile='/home/oracle/interlib/initora.ora';
# 停止服务
SQL> shutdown immediate;
# 启动服务
SQL> startup;

2.3 使用RMAN恢复备份至从库

# 首先关闭数据库
SQL> shutdown immediate;
SQL> quit
# 使用rman工具恢复备份
[oracle@localhost interlib]$ rman taget /
# 设置DBID(需要保证主库和从库的DBID一致)
RMAN> set DBID=2940770335
executing command: SET DBID
# rman自带的nomount小环境
RMAN> startup nomount;   
RMAN> restore controlfile from '/home/oracle/rman_backup/standby.ctl';
# 加载恢复的控制文件
RMAN>alter database mount; 
# 恢复数据库文件
RMAN> restore database;

2.4配置从库为备用库模式

#启动备用数据库为挂载备库模式
SQL>startup nomount;
SQL>alter database mount standby database;
#在备用服务器上启动日志传送服务
SQL>alter database recover managed standby database disconnect from session;
SQL>shutdown immediate;
SQL>startup;

如果报错:ora-16004,ora-01152,ora-01110

解决办法:

SQL> alter database recover managed standby database using current logfile disconnect from session; (需要在备库建立standby logfile,否则会出现ORA-38500错误)

# 在主库
sql> alter system switch logfile;

# 在备库
SQL> alter database recover managed standby database cancel;

SQL> alter database open;

如果报错:ORA-38500: USING CURRENT LOGFILE option not available without stand

SQL> shutdown immeidate;

SQL> startup mount;

SQL> select member from v$logfile;
MEMBER
------------------------------------------------------------------
/usr/local/oracle/oradata/alphatst/redo03.log
/usr/local/oracle/oradata/alphatst/redo02.log
/usr/local/oracle/oradata/alphatst/redo01.log

SQL> alter database add standby logfile group 4 '/usr/local/oracle/oradata/alphatst/stdREDO01.LOG' size 50m;
SQL> alter database add standby logfile group 5 '/usr/local/oracle/oradata/alphatst/stdREDO02.LOG' size 50m;
SQL> alter database add standby logfile group 6 '/usr/local/oracle/oradata/alphatst/stdREDO03.LOG' size 50m;
SQL> alter database add standby logfile group 7 '/usr/local/oracle/oradata/alphatst/stdREDO04.LOG' size 50m;

3 验证主从是否配置成功

    具体操作步骤:
    1. 查看oradata/archive文件是否传过去了
    2. 在dbprimary上插入一些数据
         #主库操作,查看当前sequence
         (dbprimary)sql>select max(sequence#) from v$archived_log; 
         #从库操作,查看当前sequence
         (dbstandby)sql>select max(sequence#) from v$archived_log;
         #主库操作,查看当前sequence
         (dbprimary)sql>alter system switch logfile;
         #主库操作,查看当前sequence
         (dbprimary)sql>select max(sequence#) from v$archived_log;  
         #从库操作,查看当前sequence
         (dbstandby)sql>select max(sequence#) from v$archived_log;
如果dbstandby的sequence跟着改变,说明已经好了

4 其他说明

在Oracle 11g以前版本中的的Data Guard物理备用数据库,可以以只读的方式打开数据库,但此时Media Recovery利用日志进行数据同步的过程就停止了,如果物理备用数据库处于恢复的过程中数据库就不能打开查询,也就是说日志应用和只读打开两个状态是互斥的,而Oracle 11g Active Data Guard功能解决了这个矛盾,在利用日志恢复数据的同时可以用只读的方式打开数据库,用户可以在备用数据库上进行查询、报表等操作,这类似逻辑Data Guard备用数据库的功能(查询功能方面),但是,数据同步的效率更高、对硬件的资源要求更低。这样可以更大程度地发挥物理备用数据库的硬件资源的效能,此短话参考【DataGuard】Oracle 11g物理Active Data Guard实时查询(Real-time query)特性。

参考资料:

  1. Oracle 11G 数据库 DATA GUARD主从同步配置详解
  2. 阿里oracle主从复制

你可能感兴趣的:(数据库,oracle,oracle,数据库)