oracle DataGuard 主从 踩过坑的

Posted 滴滴滴--你不是爱拖延,而是爱逃避

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oracle DataGuard 主从 踩过坑的相关的知识,希望对你有一定的参考价值。

一、主机描述

   dbprimary: 192.168.1.57 主机名称db1
   dbstandby: 192.168.1.58 主机名成db2
   SID: orcl

二、配置tns,配置好的文件内容:

dbprimary的listener.ora (/db/app/oracle/product/11.2.0/network/admi目录下):
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = orcl)
      (ORACLE_HOME = /db/app/oracle/product/11.2.0)
      (GLOBAL_DBNAME = orcl)
    )
  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = db1)(PORT = 1521))
    )
  )

dbstandby的listener.ora:
# Generated by Oracle configuration tools.

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = orcl)
      (ORACLE_HOME = /db/app/oracle/product/11.2.0)
      (GLOBAL_DBNAME = orcl)
    )
  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = db2)(PORT = 1521))
    )
  )

dbprimary和dbstandby上的tnsnames.ora是一样的
# Generated by Oracle configuration tools.

DBSTANDBY =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = db2)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = orcl)
    )
  )

DBPRIMARY =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = db1)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = orcl)
    )
  )

 然后在两台机器上分别检查配置是否正确
 oracle>lsnrctl stop
 oracle>lsnrctl start
 oracle>lsnrctl status
 oracle>tnsping dbprimary
 oracle>tnsping dbstandby
 oracle>sqlplus sys/[email protected] as sysdba
 oracle>sqlplus sys/[email protected] as sysdba

 

三、在dbprimary上操作
1. oracle> mkdir /db/app/oracle/oradata/archive
2. cd /db/app/oracle/product/11.2.0/dbs

oracle> cp orapworcl orapworcl.bak
oracle> orapwd file=orapw$ORACLE_SID password=oracle entries=30 force=y

产生密码文件oraworcl,一定要拷贝到从库上,就算从库执行同样的命令也不行
3. 备份并重新创建pfile文件

oracle>mv initorcl.ora initorcl.ora.bak

oracle>sqlplus sys/password as sysdba
sql>create pfile from spfile;

在/db/app/oracle/product/11.2.0/dbs下生成initorcl.ora 在initorcl.ora文件后面增加内容:
*.DB_UNIQUE_NAME=‘dbprimary‘
*.LOG_ARCHIVE_CONFIG=‘DG_CONFIG=(dbprimary, dbstandby)‘
*.LOG_ARCHIVE_DEST_1=‘LOCATION=/db/app/oracle/oradata/archive/ VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=dbprimary‘
*.LOG_ARCHIVE_DEST_2=‘SERVICE=dbstandby LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=dbstandby‘
*.LOG_ARCHIVE_DEST_STATE_1=‘ENABLE‘
*.LOG_ARCHIVE_DEST_STATE_2=‘ENABLE‘
*.LOG_ARCHIVE_FORMAT=‘%t_%s_%r.arc‘
*.LOG_ARCHIVE_MAX_PROCESSES=30
*.FAL_SERVER=‘dbstandby‘
*.FAL_CLIENT=‘dbprimary‘

*.DB_FILE_NAME_CONVERT=‘dbstandby‘,‘dbprimary‘
*.STANDBY_FILE_MANAGEMENT=‘AUTO‘

4. 备份并重新创建spfile文件

oracle>mv spfileorcl.ora spfileorcl.ora.bak

oracle>sqlplus sys/password as sysdba

sql>create spfile from pfile;
 sql>startup mount;
 sql>alter database archivelog;
 sql>alter database open;
 sql>archive log list; 查看是否处于log模式,如果不是执行如下命令
 sql>alter database force logging; 改变为logging模式
 sql>alter database create standby controlfile as ‘/db/app/oracle/oradata/standby01.ctl‘; 创建dbstandby上的控制文件
 sql>shutdown immediate;
 
5. 把/db/app/oracle/oradata打包传到dbstandby服务器, 把 /db/app/oracle/product/11.2.0/dbs/oraworcl也传过去


四、在dbstandby服务器上操作
1. oracle>sqlplus sys/password as sysdba
   sql>shutdown immediate;
2. oracle>cd /db/app/oracle
   oracle>mv oradata  oradata.bak
   把刚才传过来的包解压
   oracle>cd oradata
   oracle>cp standby01.ctl standby02.ctl
   oracle>cp standby01.ctl standby03.ctl
3. 备份并重新创建pfile文件

oracle>mv initorcl.ora initorcl.ora.bak

sql>create pfile from spfile
 修改initorcl.ora中的*.control_files参数
*.control_files=‘/db/app/oracle/oradata/standby01.ctl‘,‘/db/app/oracle/oradata/standby02.ctl‘,‘/db/app/oracle/oradata/standby03.ctl‘
 增加
*.DB_UNIQUE_NAME=‘dbstandby‘
*.LOG_ARCHIVE_CONFIG=‘DG_CONFIG=(dbprimary, dbstandby)‘
*.LOG_ARCHIVE_DEST_1=‘LOCATION=/db/app/oracle/oradata/archive/ VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=dbstandby‘
*.LOG_ARCHIVE_DEST_2=‘SERVICE=dbprimary LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=dbprimary‘
*.LOG_ARCHIVE_DEST_STATE_1=‘ENABLE‘
*.LOG_ARCHIVE_DEST_STATE_2=‘ENABLE‘
*.LOG_ARCHIVE_FORMAT=‘%t_%s_%r.arc‘
*.LOG_ARCHIVE_MAX_PROCESSES=30
*.FAL_SERVER=‘dbprimary‘
*.FAL_CLIENT=‘dbstandby‘

*.DB_FILE_NAME_CONVERT=‘dbstandby‘,‘dbprimary‘
*.STANDBY_FILE_MANAGEMENT=‘AUTO‘
    
4. 备份并重新创建spfile文件

oracle>mv spfileorcl.ora spfileorcl.ora.bak

oracle>sqlplus sys/password as sysdba

sql>create spfile from pfile;
sql>shutdown immediate;

到此为止,已经完成了oracle DataGuard主备的所有配置

五、主备配置验证,及数据库重启步骤

(重点)在主库(db1)服务器上开启两个命令窗口,分别使用sqlplus登录主库和备库

登录主库

sqlplus sys/[email protected] as sysdba

登录备库

sqlplus sys/[email protected] as sysdba

1.关闭主库

SHUTDOWN IMMEDIATE;

2.关闭备库

SHUTDOWN IMMEDIATE;

3.启动备库到mount

startup mount;

4.启动主库

startup;

5.打开备库

alter database open;

6.开启备库上的同步服务

alter database recover managed standby database disconnect from session;

7.检查是否工作正常

(备)sql>select max(sequence#) from v$archived_log;
(主)sql>alter system switch logfile;
(主)sql>select max(sequence#) from v$archived_log; 查看当前sequence
(备)sql>select max(sequence#) from v$archived_log;
如果dbstandby的sequence跟着改变,说明已经好了

8. select dest_name,status,error from v$archive_dest;  察看相应的归档路径的状态是否valid,否则根据error信息进行处理

9. 可以查看alert日志文件
 /db/app/oracle/diag/rdbms/dbprimary/orcl/alert/log.xml
 /db/app/oracle/diag/rdbms/dbprimary/orcl/trace/alert_orcl.log

 

以上是关于oracle DataGuard 主从 踩过坑的的主要内容,如果未能解决你的问题,请参考以下文章

oracle10g查看主从延迟

实战dataguard主从切换

Oracle dataguard 备库归档日志不全.怎么恢复

mysql主从复制的介绍

[dataguard同步数据库]oracle11g dataguard 备库数据同步的检查方法

Oracle Dataguard(主库为 Oracle rac 集群)配置教程(03)—— 创建 dataguard 数据库之前的准备工作