分清主备库
查看SID 
主库
[oracle@localhost ~]$ echo $ORACLE_SID
NANA
备库
[oracle@zw4 ~]$ echo $ORACLE_SID
STANDBY
二配置
 
一主库 NANA数据库配置
步骤1
确认主库处于归档模式,如果为非归档则必须改为归档模式
修改为归档模式
 
SQL> alter database archivelog;
 Oracle灾备系统-DATAGUARD_Oracle
查看是否为归档模式
SQL> archive log list;
步骤2
primary 数据库置为FORCE LOGGING 模式
SQL> alter database force logging;
SQL>  select t.FORCE_LOGGING from v$database t;
Oracle灾备系统-DATAGUARD_职场_02
步骤3:配置监听系统
SQL> 配置net service names(方式多样,不详述)并测试
两个数据库可以互相连通
开启监听
[oracle@localhost ~]$ lsnrctl start
配置监听
[oracle@localhost admin]$ netmgr
查看SID
[oracle@localhost admin]$ echo $ORACLE_SID
SQL> show parameter db_name;
 
在各数据库 配置互相的监听
Oracle灾备系统-DATAGUARD_灾备_03
步骤4
创建primary 数据库初始化参数文件便于参数修改
SQL> create pfile='/home/oracle/initdg.ora' from spfile;
修改主库参数文件
1.先确认主库名称
Oracle灾备系统-DATAGUARD_职场_04
DB_UNIQUE_NAME=NANA   --show parameter DB_UNIQUE_NAME确认主库名
2.网络连接的名字
Oracle灾备系统-DATAGUARD_Oracle_05
LOG_ARCHIVE_CONFIG=‘DG_CONFIG=(NANA ,STANDBY)--此处为主库网络连接串(tnsnames.ora)
3.查看归档文件路径
SQL> show parameter archive
Oracle灾备系统-DATAGUARD_灾备_06
LOG_ARCHIVE_DEST_1=LOCATION=/home/oracle/archivelog   --主库的归档日志路径
4查看监听的数据库
[oracle@localhost~]$more
 /oracle/app/oracle/product/10.2.0/db_1/network/admin/tnsnames.ora
5 配置备库参数
LOG_ARCHIVE_DEST_2=‘SERVICE=standby LGWR ASYNC --此处为备库网络连接串 VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=standby‘备库的DB_UNIQUE_NAME
 
6.查看LOG_ARCHIVE_DEST_STATE状态
SQL> show parameter archive;
Oracle灾备系统-DATAGUARD_灾备_07
8.查看remote
SQL> show parameter remote;
REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE
Oracle灾备系统-DATAGUARD_灾备_08
9
备库相当于服务端 主库相当于客户端 主库不断相备库请求连接
FAL_SERVER=standby 
FAL_CLIENT=primary
 
10查看主库和备库的数据文件路径和日志文件路径
SQL> select file_name from dba_data_files;
主库
Oracle灾备系统-DATAGUARD_休闲_09
备库
Oracle灾备系统-DATAGUARD_休闲_10
说明:‘/oracle/app/oradata/NANA’为主库数据\日志文件路径; ‘/oradata/STANDBY ‘为备库数据\日志文件路径
DB_FILE_NAME_CONVERT='/oracle/app/oradata/NANA ','/oradata/STANDBY‘
LOG_FILE_NAME_CONVERT='/oracle/app/oradata/NANA ','/oradata/STANDBY‘
 
11STANDBY_FILE_MANAGEMENT=AUTO
 
主库的初始化参数
*.audit_file_dest='/oracle/app/admin/NANA/adump'
*.background_dump_dest='/oracle/app/admin/NANA/bdump'
*.compatible='10.2.0.1.0'
*.control_files='/oracle/app/oradata/NANA/control01.ctl','/oracle/app/oradata/NANA/control02.ctl','/oracle/app/oradata/NANA/control03.ctl'
*.core_dump_dest='/oracle/app/admin/NANA/cdump'
*.db_block_size=8192
*.db_domain=''
*.db_file_multiblock_read_count=16
*.db_name='NANA'
 
DB_UNIQUE_NAME=NANA     
LOG_ARCHIVE_CONFIG='DG_CONFIG=(nana,standby)'   
LOG_ARCHIVE_DEST_1='LOCATION=/home/oracle/archivelog
VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=NANA'
LOG_ARCHIVE_DEST_2='SERVICE=standby LGWR ASYNC
VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=standby'
LOG_ARCHIVE_DEST_STATE_1=ENABLE
LOG_ARCHIVE_DEST_STATE_2=ENABLE
REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE
FAL_SERVER=standby 
FAL_CLIENT=nana
DB_FILE_NAME_CONVERT=' /oracle/app/oradata/NANA','/oradata/STANDBY'
LOG_FILE_NAME_CONVERT='/oracle/app/oradata/NANA ','/oradata/STANDBY'
STANDBY_FILE_MANAGEMENT=AUTO
 
 
 
*.db_recovery_file_dest='/oracle/app/flash_recovery_area'
*.db_recovery_file_dest_size=2147483648
*.dispatchers='(PROTOCOL=TCP) (SERVICE=NANAXDB)'
*.job_queue_processes=10
*.open_cursors=300
*.pga_aggregate_target=16777216
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.sga_target=167772160
*.undo_management='AUTO'
*.undo_tablespace='UNDOTBS1'
*.user_dump_dest='/oracle/app/admin/NANA/udump'
 
备库standby 数据库配置
1.修改备库sys用户密码保持与库密码一致
SQL> alter user sys identified by sys;
2.从库改
db_name 与主库一致
3.配库归档文件路径与主库一致
LOG_ARCHIVE_DEST_1='LOCATION=/home/oracle/archivelog VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=standby'
 
 配库参数
 
*.audit_file_dest='/oracle/app/admin/STANDBY/adump'
*.background_dump_dest='/oracle/app/admin/STANDBY/bdump'
*.compatible='10.2.0.1.0'
*.control_files='/oradata/STANDBY/control01.ctl','/oradata/STANDBY/control02.ctl','/oradata/STANDBY/control03.ctl'
*.core_dump_dest='/oracle/app/admin/STANDBY/cdump'
*.db_block_size=8192
*.db_domain=''
*.db_file_multiblock_read_count=16
 
db_name='NANA'
DB_UNIQUE_NAME=standby
LOG_ARCHIVE_CONFIG='DG_CONFIG=(nana,standby)'
DB_FILE_NAME_CONVERT='/oracle/app/oradata/NANA','/oradata/STANDBY'
LOG_FILE_NAME_CONVERT='/oracle/app/oradata/NANA','/oradata/STANDBY'
STANDBY_FILE_MANAGEMENT=AUTO
LOG_ARCHIVE_DEST_1='LOCATION=/home/oracle/archivelog VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=standby'
LOG_ARCHIVE_DEST_2='SERVICE=nana LGWR ASYNC
VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=NANA'
LOG_ARCHIVE_DEST_STATE_1=ENABLE
LOG_ARCHIVE_DEST_STATE_2=ENABLE
REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE
FAL_SERVER=NANA      主库和备库相反
FAL_CLIENT=standby
STANDBY_FILE_MANAGEMENT=AUTO
 
*.db_recovery_file_dest='/oracle/app/flash_recovery_area'
*.db_recovery_file_dest_size=2147483648
*.dispatchers='(PROTOCOL=TCP) (SERVICE=STANDBYXDB)'
*.job_queue_processes=10
*.log_archive_dest_1='location=/home/oracle/data.bak'
*.open_cursors=300
*.pga_aggregate_target=89128960
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.sga_max_size=268435456
*.sga_target=268435456
*.undo_management='AUTO'
*.undo_tablespace='UNDOTBS1'
*.user_dump_dest='/oracle/app/admin/STANDBY/udump'
~
 
主陪库的参数复制到相应的数据库参数里
[oracle@localhost ~]$ vi /home/oracle/initdg.ora
 
先关掉数据库
SQL> shutdown abort
制定文件启动数据库
SQL> startup pfile=/home/oracle/initdg.ora
Oracle灾备系统-DATAGUARD_灾备_11
启动后产看归档日志文件的位置
SQL> show parameter archive
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
archive_lag_target                   integer     0
log_archive_config                   string      DG_CONFIG=(nana,standby)
log_archive_dest                     string
log_archive_dest_1                   string      LOCATION=/home/oracle/archivel
                                                 og
                                                 VALID_FOR=(ALL_LOGFILES,ALL_RO
                                                 LES) DB_UNIQUE_NAME=NANA
log_archive_dest_10                  string
log_archive_dest_2                   string      SERVICE=standby LGWR ASYNC
                                                 VALID_FOR=(ONLINE_LOGFILES,PRI
                                                 MARY_ROLE) DB_UNIQUE_NAME=STAN                                                 DBY
log_archive_dest_3                   string
 
步骤7:迁移主库stand数据库的数据文件、控制文件到standby
RMAN
利用新的参数文件启动NANAOPEN状态
-利用新的参数文件启动standby nomount 状态
前备库起到nomount
SQL> startup pfile=/home/oracle/initdg.ora nomount
 
 
 
-主库上利用RMAN制作nana数据库备份(所有RMAN操作都在主数据库上执行)
 主库做rman备份
进入RMAN
[oracle@localhost ~]$ rman
RMAN> connect target
查看备份数据
RMAN> list backup
删除备份文件
RMAN> delete backup;
备份数据
 
RMAN>backup full database format='/home/oracle/archivelog/%U' include current controlfile for standby;
 
-NANA主库的rman的备份集拷贝至standby库相同的路径,并在主库连接,不要断开上面的连接,如果断开,那么重新连接主库
 
 
将备份文件拷贝到备库的相同目录下
[oracle@localhost ~]$ scp *_1
oracle@10.1.1.32:/home/oracle/archivelog/
 Oracle灾备系统-DATAGUARD_灾备_12
RMAN > connect target /
RMAN > connect auxiliary sys/sys@standby
Oracle灾备系统-DATAGUARD_职场_13
 
复制standby数据库,接着上面的连接在主库上执行
RMAN > duplicate target database for standby nofilenamecheck;
Oracle灾备系统-DATAGUARD_休闲_14
 
正常运行无错误后直接执行下面的命令进行主备库的日志同步
说明:此命令结束后从库直接启动mount
执行完后会自动启动到mount
 
修改standby数据库自动恢复状态
alter database recover managed standby database disconnect from session;
 
 
确认DATAGUARD环境状态是否正常
主库操作:
select process from v$managed_standby;
PROCESS
------------------
查询主库
是否存在日志网络传输进程
ARCH
ARCH
ARCH
LNS --确认主库有此lns进程(日志网络服务进程-负责把主库归档日志传输到备库)
如果无此进程则执行下面命令
alter system set log_archive_dest_2='SERVICE=standby LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=standby';
alter system set log_archive_dest_state_2=ENABLE;
alter system switch logfile;
继续确认有无lns进程,如果还没有启动则查看相关错误信息
select error from v$archive_dest; 根据具体ORACLE错误码查找错误原因
Oracle灾备系统-DATAGUARD_职场_15
 
备库操作:
SQL> select process from v$managed_standby;
PROCESS
------------------
ARCH
ARCH
RFS --远程文件服务-接收由主库lns进程传送过来的日志
RFS
MRP0 --介质恢复进程-同步主库日志数据到从库
注意:如果没有RFS进程,则查看主库lns进程是否正常.如果没有MRP进程则执行
alter database recover managed standby database disconnect from session;
Oracle灾备系统-DATAGUARD_Oracle_16