Oracle学习系列之一步一步搭建Dataguard


规划:

两台redhat5.4机器:

PRIMARY:

IP地址:10.0.18.251

数据库SID:zsddb

DB_UNIQUE_NAME:zsddb8

数据库软件安装路径: /data/ora11g/product/11.2.0/db_1

数据文件路径: /data/ora11g/oradata/zsddb/

本地归档路径: /data/ora11g/oradata/zsddb/archivelog

 

STANDBY库:

IP地址:10.0.18.250

数据库SID:zsddb

DB_UNIQUE_NAME:zsddb9

数据库软件安装路径: /data/ora11g/product/11.2.0/db_1

数据文件路径: /data/ora11g/oradata/zsddb/

本地归档路径: /data/ora11g/oradata/zsddb/archivelog


:Primary数据库配置和操作:

步骤:

1.       确认主库处于归档模式


  1. Archive log list;
  2. (如果不是归档模式,详见:Oracle学习系列之如何配置归档模式的数据库)

2.       Primary数据库置为Force Logging模式

  1. Alter database force logging

3.       配置Primary数据库的初始化参数

思路:想要修改spfile,首先获取pfile,然后用文本工具打开编辑,改成你所需要的初始化参数。然后再重新加载至数据库中

     *从当前的SPFILE中创建PFILE:

  1. create pfile ='/home/oracle/pfileZSDDB8.ora' from spfile;

  1. *.db_unique_name='zsddb8'
  2. *.fal_client='zsddb_1.8'
  3. *.fal_server='zsddb_1.9'
  4. *.log_archive_config='DG_CONFIG=(zsddb6,zsddb7,zsddb8,zsddb9)'
  5. *.log_archive_dest_1='location=/data/ora11g/oradata/zsddb/archivelog/ valid_for=(all_logfiles,all_roles) db_unique_name=zsddb8'
  6. *.log_archive_dest_2='service=zsddb_1.9 sync valid_for=(online_logfiles,primary_role) db_unique_name=zsddb9'
  7. *.log_archive_dest_3='service=zsddb_31.7 async valid_for=(online_logfiles,primary_role) db_unique_name=zsddb7'
  8. *.log_archive_dest_4='service=zsddb_31.6 async valid_for=(online_logfiles,primary_role) db_unique_name=zsddb6'
  9. *.log_archive_dest_state_2='defer'
  10. *.log_archive_dest_state_3='defer'
  11. *.log_archive_dest_state_4='defer'
  12. *.standby_file_management='auto'

  1. Shutdown immediate;
  2. create spfile from pfile='/home/oracle/pfileZSDDB8.ora' ;
  3. startup;

4.       配置主库的监听和网络服务名:

  1. cd $ORACLE_HOME/network/admin
  2. vi listener.ora

  3. SID_LIST_LISTENER =
  4. (SID_LIST =
  5. (SID_DESC =
  6. (GLOBAL_DBNAME = zsddb)
  7. (ORACLE_HOME = /data/ora11g/product/11.2.0/db_1)
  8. (SID_NAME = zsddb)
  9. )
  10. )

  11. LISTENER =
  12. (DESCRIPTION_LIST =
  13. (DESCRIPTION =
  14. (ADDRESS = (PROTOCOL = TCP)(HOST = zsddb8)(PORT = 1521))
  15. )
  16. )
  *使得监听器生效

  1. lsnrctl stop
  2. lsnrctl status
  3. lsnrctl start

  1. vi $ORACLE_HOME/network/admin/tnsname.ora

  2. zsddb_1.8 =
  3. (DESCRIPTION =
  4. (ADDRESS_LIST =
  5. (ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.18.251)(PORT = 1521))
  6. )
  7. (CONNECT_DATA =
  8. (SERVICE_NAME = zsddb)
  9. (SERVER = DEDICATED)
  10. )
  11. )

  12. zsddb_1.9 =
  13. (DESCRIPTION =
  14. (ADDRESS_LIST =
  15. (ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.18.250)(PORT = 1521))
  16. )
  17. (CONNECT_DATA =
  18. (SERVICE_NAME = zsddb)
  19. (SERVER = DEDICATED)
  20. )
  21. )

5.       创建密钥文件(由于我是dbca创建库的,所以就是有了)

具体位置: /data/ora11g/product/11.2.0/db_1/dbs/orapwzsddb

创建命令:

  1. orapwd file=/data/ora11g/product/11.2.0/db_1/dbs/orapwzsddb password=zsdzsd entries=30

6.       创建Standby Redologs

    配置细节:确保Standby Redologs的文件大小与Primary数据库的Online Redologs文件

大小一致。


创建命令:


  1. SQL>alter database add standby logfile group4 ('/data/ora11g/oradata/dgfile/std01.log') size 200M;
  2. SQL>alter database add standby logfile group5 ('/data/ora11g/oradata/dgfile/std02.log') size200M;
  3. SQL>alter database add standby logfile group6 ('/data/ora11g/oradata/dgfile/std03.log') size200M;
 删除命令:

  1. SQL>alter database drop stand by logfile group4;

:STANDBY数据库配置和操作:

   Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE 思路:使用RMAN方式创建STANDBY,需要的准备工作:1,要指定实例2.创建standby的初始化文件。3.启动到nomount状态,4.创建密钥文件(可以直接copy过来).最后使用rman恢复Standby

1.      
创建standby实例

  1. set oracle_sid = zsddb

2.       再配置对应的监听和tnsname.ora文件

  1. vi $ORACLE_HOME/network/admin/listener.ora

  2. SID_LIST_LISTENER =
  3. (SID_LIST =
  4. (SID_DESC =
  5. (GLOBAL_DBNAME = zsddb)
  6. (ORACLE_HOME = /data/ora11g/product/11.2.0/db_1)
  7. (SID_NAME = zsddb)
  8. )
  9. )

  10. LISTENER =
  11. (DESCRIPTION_LIST =
  12. (DESCRIPTION =
  13. (ADDRESS = (PROTOCOL = TCP)(HOST = zsddb9)(PORT = 1521))
  14. )
  15. )

  16. 配置Net Server Name。
  17. vi $ORACLE_HOME/network/admin/tnsnames.ora

  18. zsddb_1.8 =
  19. (DESCRIPTION =
  20. (ADDRESS_LIST =
  21. (ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.18.251)(PORT = 1521))
  22. )
  23. (CONNECT_DATA =
  24. (SERVICE_NAME = zsddb)
  25. (SERVER = DEDICATED)
  26. )
  27. )

  28. zsddb_1.9 =
  29. (DESCRIPTION =
  30. (ADDRESS_LIST =
  31. (ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.18.250)(PORT = 1521))
  32. )
  33. (CONNECT_DATA =
  34. (SERVICE_NAME = zsddb)
  35. (SERVER = DEDICATED)
  36. )
  37. )
    *测试主库和备库直接监听器是否正常,可以用如下命令:

  1. tnsping zsddb_1.8;(tnsping zsddb_1.9)

3.       创建Standbypfile

Pfile参数文件具体内容

  1. zsddb.__db_cache_size=369098752
  2. zsddb.__java_pool_size=4194304
  3. zsddb.__large_pool_size=4194304
  4. zsddb.__oracle_base='/data/ora11g'#ORACLE_BASE set from environment
  5. zsddb.__pga_aggregate_target=180355072
  6. zsddb.__sga_target=545259520
  7. zsddb.__shared_io_pool_size=0
  8. zsddb.__shared_pool_size=159383552
  9. zsddb.__streams_pool_size=0
  10. *.audit_file_dest='/data/ora11g/admin/zsddb/adump'
  11. *.audit_trail='db'
  12. *.compatible='11.2.0.0.0'
  13. *.control_files='/data/ora11g/oradata/dgfile/control01.ctl','/data/ora11g/oradata/dgfile/control02.ctl'
  14. *.db_block_size=8192
  15. *.db_domain=''
  16. *.db_name='zsddb'
  17. *.db_unique_name='zsddb9'
  18. *.fal_client='zsddb_1.8'
  19. *.fal_server='zsddb_1.9'
  20. *.log_archive_config='DG_CONFIG=(zsddb6,zsddb7,zsddb8,zsddb9)'
  21. *.log_archive_dest_1='location=/data/ora11g/oradata/zsddb/archivelog/ valid_for=(all_logfiles,all_roles) db_unique_name=zsddb9'
  22. *.log_archive_dest_2='service=zsddb_1.8 sync valid_for=(online_logfiles,primary_role) db_unique_name=zsddb8'
  23. *.log_archive_dest_3='service=zsddb_31.7 async valid_for=(online_logfiles,primary_role) db_unique_name=zsddb7'
  24. *.log_archive_dest_4='service=zsddb_31.6 async valid_for=(online_logfiles,primary_role) db_unique_name=zsddb6'
  25. *.log_archive_dest_state_2='enable'
  26. *.standby_file_management='auto'
  27. *.diagnostic_dest='/data/ora11g'
  28. *.dispatchers='(PROTOCOL=TCP) (SERVICE=zsddbXDB)'
  29. *.log_archive_dest_1='LOCATION=/data/ora11g/oradata/zsddb/archivelog'
  30. *.log_archive_format='%t_%s_%r.dbf'
  31. *.open_cursors=300
  32. *.pga_aggregate_target=180355072
  33. *.processes=300
  34. *.remote_login_passwordfile='EXCLUSIVE'
  35. *.sessions=335
  36. *.sga_target=543162368
  37. *.undo_tablespace='UNDOTBS1'
    * 使得初始参数文件生效:

  1. create spfile from pfile='/home/oracle/pfile_userdb9.ora' ;
    * 之前如果没有相应的归档目录或者audit目录,都要重新的创建

  1. cd /data/ora11g/admin/zsddb/
  2. mkdir adump bdump cdump udump
  3. mkdir -p /data/ora11g/oradata/zsddb/archivelog/

4.       启动standbynomount状态

  1. SQL>startup nomount

5.       创建Standby的密码

PRIMARY库的密码copyStandby数据库中。

  1. scp /data/ora11g/product/11.2.0/db_1/dbs/orapwzsddb root@10.0.18.250:/data/ora11g/product/11.2.0/db_1/dbs
    这里可能有权限问题,使用chown修改权限

6.       开始使用rman创建standby

   *先对Primary数据库进行归档。

  1. alter system switch logfile
   *进入rman

  1. rman target /
   *备份主库:primary操作)

  1. run
  2. {
  3. allocate channel c1 device type disk format '/data/backup/rman/%U';
  4. backup database plus archivelog;
  5. }
  *生成备库控制文件(primary操作)

  1. SQL> alter database create standby controlfile as '/data/backup/rman/control01.ctl';
  *将主库的备份copy到备库中。

  1. scp /data/backup/rman/* root@10.0.18.250:/data/backup/rman/
  2. (细节,拷贝过来的备份是不可用的,由于权限的原因,所以要使用chown修改权限)
  3. chown oracle:oinstall /data/backup/rman/*
  4. 使用rman恢复备库的控制文件.
   使用rman恢复备库的控制文件.

  1. rman target /

  2. Rman> set DBID= 1538857847(DBID的获得方法,自己寻找,可以在主库使用rman target /即可看到)

  3. Rman> RESTORE CONTROLFILE FROM '/data/backup/rman/control01.ctl';
  7.将备库至于mount状态

  1. SQL> alter database mount standby database;
    *在这里可以查看一下standby online log 。
  

  1. select GROUP#,TYPE,MEMBER FROM V$LOGFILE;

  2. alter database drop standby logfile group 4;
  3. alter database drop standby logfile group 5;
  4. alter database drop standby logfile group 6;

  5. alter database add standby logfile group 4 ('/data/ora11g/oradata/dgfile/std01.log') size 256M;
  6. alter database add standby logfile group 5 ('/data/ora11g/oradata/dgfile/std02.log') size 256M;
  7. alter database add standby logfile group 6 ('/data/ora11g/oradata/dgfile/std03.log') size 256M;
8.恢复备库:

  1. rman target /
  2. restore database
9.修复成功后,开始开启archive_dest_state_2进程(primary操作)

  1. Alter system set log_archive_dest_state_2=enable;10.
  *复制密码文件

  1. scp -P 50718 /data/ora11g/product/11.2.0/db_1/dbs/orapwuserdb oracle@192.168.31.7:/data/ora11g/product/11.2.0/db_1/dbs/
10.将备库置于自动恢复状态
         
  1. SQL〉recover managed standby database disconnect from session;(这里就开始自动修复了)

  2. *恢复数据完毕后,你可以关闭Redo应用
  3. SQL>alter database recover managed standby database cancel;

  4. *然后以只读的方式打开数据库
  5. SQL>alter database open read only

  6. *最后一条命令很关键(这可是新功能哦,可以一边在打开的模式下,一边接受redo日志。但是你必须保证有standby redolog这个文件。要不然可是打不开的)
  7. alter database recover managed standby database using current logfile disconnect from session;

到这里大功告成。你可以选择喝杯咖啡,验证后续的正常情况。

  1. 查询当前库的角色和保护模式:
  2. SQL> select name,database_role,db_unique_name,open_mode,protection_mode,protection_level,switchover_status,supplemental_log_data_pk,supplemental_log_data_ui from v$database;
  3. 查看进程的活动状态:
  4. SQL> select process,client_process,sequence#,status from v$managed_standby;
  5. 检查REDO应用进度:
  6. select dest_name,archived_thread#,archived_seq#,applied_thread#,applied_seq#,db_unique_name from v$archive_dest_status where status='VALID';

  1. 查看同步情况

  2. select MAX(SEQUENCE#) FROM V$ARCHIVED_LOG;

  3. select sequence#,applied from v$archived_log;

  4. 查看online&standby日志

  5. select GROUP#,TYPE,MEMBER FROM V$LOGFILE;

上一篇:.NET应用架构设计—服务端开发多线程使用小结(多线程使用常识)


下一篇:Py之curses:curses库的简介、使用、安装方法详细攻略