Oracle笔记(3)Oracle11G + CentOS 6 DG 搭建及管理

一、准备工作

  1. 环境准备
  2. 主数据库Oracle Database安装
  3. 备服务器Oracle Database software 安装

二、正式配置ADG

三、ADG基本管理

--------------------------------------------------------------分割线-------------------------------------------------------------

一、准备工作

1.环境配置:

主机配置

 节点   操作系统 主机名称        IP地址
 主库 CentOS6.4_x86_64   dgp 192.168.10.61
 备库 CentOS6.4_x86_64   dgs 192.168.10.62

 

 

 

 

修改主机名:

[root@db /]# cat /etc/sysconfig/network
[root@db /]# sed -i s/HOSTNAME=.*/HOSTNAME=dgp/ /etc/sysconfig/network
[root@db /]# reboot

 

规划数据库配置

 节点   Oracle版本   SID    db_name
db_unique_name
 主库   11.2.0.4   orcl     orcl      orcl
 备库   11.2.0.4   orcl     orcl      orcl_std

 

 

 

以上完成之后,对两台主机进行以下操作:

1)禁用防火墙

[root@dgp ~]# chkconfig iptables off
[root@dgp ~]# service iptables stop

2)禁用selinux

[root@dgp ~]# sed -i s/SELINUX=.*/SELINUX=disabled/ /etc/sysconfig/selinux
[root@dgp ~]# setenforce 0

3)配置host文件

[root@dgp ~]# cat /etc/hosts
[root@dgp ~]# echo -e '192.168.10.61 dgp \n192.168.10.62 dgs' >>/etc/hosts

备注:echo -e 表示开启转义。

4)软件准备

数据库文件如下:

p13390677_112040_Linux-x86-64_1of7.zip

p13390677_112040_Linux-x86-64_2of7.zip

创建路径 /install路径,上传安装文件并解压。

[root@dgp /]# mkdir /install
[root@dgp /]# cd /install
[root@dgp /]# unzip p13390677_112040_Linux-x86-64_1of7.zip
[root@dgp /]# unzip p13390677_112040_Linux-x86-64_1of7.zip

 

2.主数据库Oracle Database安装

配置最小参数

修改参数文件/etc/sysctl.conf

 

[root@dgp /]# echo 'fs.suid_dumpable = 1
fs.aio-max-nr = 1048576
fs.file-max = 6815744
kernel.shmall = 2097152
kernel.shmmax = 536870912
kernel.shmmni = 4096
# semaphores: semmsl, semmns, semopm, semmni
kernel.sem = 250 32000 100 128
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default=262144
net.core.rmem_max=4194304
net.core.wmem_default=262144
net.core.wmem_max=1048586' >> /etc/sysctl.conf

 

 执行命令使配置生效

[root@dgp /]# /sbin/sysctl -p

修改limits.conf

添加以下内容到配置文件/etc/security/limits.conf:

[root@db /]# echo 'oracle              soft    nproc   16384
oracle              hard    nproc   16384
oracle              soft    nofile  4096
oracle              hard    nofile  65536
oracle              soft    stack   10240' >> /etc/security/limits.conf

安装必要的rpm包

[root@dgp /]# yum install -y binutils-2*x86_64* glibc-2*x86_64* nss-softokn-freebl-3*x86_64* glibc-2*i686* nss-softokn-freebl-3*i686* compat-libstdc++-33*x86_64*
yum install -y glibc-common-2*x86_64* glibc-devel-2*x86_64* glibc-devel-2*i686* glibc-headers-2*x86_64* elfutils-libelf-0*x86_64*
yum install -y elfutils-libelf-devel-0*x86_64* gcc-4*x86_64* gcc-c++-4*x86_64* ksh-*x86_64* libaio-0*x86_64*
yum install -y libaio-devel-0*x86_64* libaio-0*i686* libaio-devel-0*i686* libgcc-4*x86_64*  libgcc-4*i686* libstdc++-4*x86_64* libstdc++-4*i686* 
yum install -y libstdc++-devel-4*x86_64* make-3.81*x86_64* numactl-devel-2*x86_64* sysstat-9*x86_64* compat-libstdc++-33*i686* compat-libcap*

创建配置用户

[root@dgp /]# groupadd -g 501 oinstall
groupadd -g 502 dba
groupadd -g 503 oper
groupadd -g 504 asmadmin
groupadd -g 506 asmdba
groupadd -g 505 asmoper
useradd -u 502 -g oinstall -G dba,asmdba,oper oracle
[root@dgp install]# passwd oracle
Changing password for user oracle.
New password:
BAD PASSWORD: it is too simplistic/systematic
Retype new password:
passwd: all authentication tokens updated successfully.

修改"/etc/security/limits.d/90-nproc.conf" 文件中的内容

将 “* soft nproc 1024”这一行修改为“* - nproc 16384”

[root@dgp /]# cp /etc/security/limits.d/90-nproc.conf /etc/security/limits.d/90-nproc.conf_bak
[root@dgp /]# sed -i s/.*1024/*\ -\ nproc\ 16384/ /etc/security/limits.d/90-nproc.conf

创建数据库安装路径

[root@dgp /]# mkdir -p /u01/app/oracle/product/11.2.0/db_1
chown -R oracle:oinstall /u01
chmod -R 775 /u01

登录oracle用户,并在 ".bash_profile" 文件尾部添加内容。

[root@dgp /]# su - oracle
[oracle@dgp /]$ pwd
[oracle@dgp /]$
cat >> .bash_profile <<'EOF'

#Oracle Settings TMP
=/tmp; export TMP TMPDIR=$TMP; export TMPDIR ORACLE_HOSTNAME=dgp; export ORACLE_HOSTNAME ORACLE_UNQNAME=orcl; export ORACLE_UNQNAME ORACLE_BASE=/u01/app/oracle; export ORACLE_BASE ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1; export ORACLE_HOME ORACLE_SID=orcl; export ORACLE_SID PATH=/usr/sbin:$PATH; export PATH PATH=$ORACLE_HOME/bin:$PATH; export PATH LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib; export LD_LIBRARY_PATH CLASSPATH=$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib; export CLASSPATH
EOF

备注:配置备库时候:ORACLE_HOSTNAME=dgs  ORACLE_UNQNAME=orcl_std   ORACLE_SID=orcl

安装数据库

直接以Oracle用户登录,执行安装

[oracle@dgp /]$ cd /install/database、
[oracle@dgp /]$ ll
[oracle@dgp /]$ ./runInstaller

图形界面忽略,过程中在root用户下执行脚本。

[root@dgp ~]# /u01/app/oraInventory/orainstRoot.sh
[root@dgp ~]# /u01/app/oracle/product/11.2.0/db_1/root.sh

 备注:主库选择安装数据库软件和数据库实例,备库仅安装数据库软件。

 

设置Oracle Database自启动

修改/etc/oratab配置文件中每一个重起标志设置成Y:

[root@dgp /]# sed -i 's/\(orcl:\)\(.*:\)N/\1\2Y/' /etc/oratab

 创建自启动配置文件/etc/init.d/dbora:

[root@dgp /]# cat >> /etc/init.d/dbora <<EOF
#!/bin/sh
# chkconfig: 345 99 10
# description: Oracle auto start-stop script.
ORA_HOME=/u01/app/oracle/product/11.2.0/db_1
ORA_OWNER=oracle
if [ ! -f $ORA_HOME/bin/dbstart ]
then
echo "Oracle startup: cannot start"
exit
fi
case "$1" in
'start')
# Start the Oracle databases:
# The following command assumes that the oracle login
# will not prompt the user for any values
su - $ORA_OWNER -c "$ORA_HOME/bin/dbstart $ORA_HOME"
touch /var/lock/subsys/dbora
;;
'stop')
# Stop the Oracle databases:
# The following command assumes that the oracle login
# will not prompt the user for any values
su - $ORA_OWNER -c "$ORA_HOME/bin/dbshut $ORA_HOME"
rm -f /var/lock/subsys/dbora
;;
esac
EOF

赋予执行权限,添加自启动

[root@dgp ~]# chmod 750 /etc/init.d/dbora
chkconfig --add dbora
chkconfig dbora on

3.备服务器Oracle Database software 安装

备服务器安装software,过程参考主库数据库安装过程。

 

二、正式配置ADG

1.主库检查归档模式等

在主库操作:
#查看log_mode、force_logging、flashback_on
SQL> select
name,log_mode,force_logging,flashback_on from v$database;

#切换成归档模式
SQL> SHUTDOWN IMMEDIATE;
SQL> STARTUP MOUNT;
SQL> ALTER DATABASE ARCHIVELOG; #修改成归档模式

#开启FORCE LOGGING
SQL> ALTER DATABASE FORCE LOGGING #开启FORCE LOGGING
#修改归档日志路径(按需修改,这里仅仅列出供参考)
#[oracle@dgp /]$ mkdir /u01/app/oracle/archivelog
#SQL> archive log list
#SQL> alter system set log_archive_dest_1='location=/u01/app/oracle/archivelog';  

SQL> alter database flashback on;  #开启flashback on
SQL> ALTER DATABASE OPEN;


2.主库添加SRL:

注意:standby redo log比 redo log 多一个,并且大小保持一致。
SQL> select * from v$log;
SQL> select * from v$logfile;
SQL> alter database add standby logfile group 11 '/u01/app/oracle/oradata/orcl/redo_std01.log' size 50M;
SQL> alter database add standby logfile group 12 '/u01/app/oracle/oradata/orcl/redo_std02.log' size 50M;
SQL> alter database add standby logfile group 13 '/u01/app/oracle/oradata/orcl/redo_std03.log' size 50M;
SQL> alter database add standby logfile group 14 '/u01/app/oracle/oradata/orcl/redo_std04.log' size 50M;

3.主库修改参数:

#修改参数(可以用sql命令修改,也可以直接修改pfile文件)
[oracle@dgp /]$ sqlplus / as sysdba
SQL >
alter system set log_archive_config='DG_CONFIG=(orcl,orcl_std)' scope=both;
alter system set log_archive_dest_1='LOCATION=USE_DB_RECOVERY_FILE_DEST VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=orcl' scope=both;    #主库log路径 alter system set log_archive_dest_2
='SERVICE=orcl_std  LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcl_std' scope=both;  #备库log路径 alter system set log_archive_dest_state_1='enable' scope=both; alter system set log_archive_dest_state_2='enable' scope=both; # alter system set db_file_name_convert='/u01/oracle/datafile','/u01/oracle/datafile' scope=both; #主备路径一致,忽略 # alter system set log_file_name_convert='/u01/oracle/onlinelog','/u01/oracle/onlinelog' scope=both; #主备路径一致,忽略 alter system set fal_server='orcl_std' scope=both; alter system set fal_client='orcl' scope=both; alter system set standby_file_management='AUTO' scope=both;
alter system set db_unique_name='orcl' scope=both;

#检查主库pfile文件initorcl.ora
[oracle@dgp /]$ cat /u01/app/oracle/product/11.2.0/db_1/dbs/initorcl.ora
orcl.__db_cache_size=687865856
orcl.__java_pool_size=16777216
orcl.__large_pool_size=33554432
orcl.__oracle_base='/u01/app/oracle'#ORACLE_BASE set from environment
orcl.__pga_aggregate_target=671088640
orcl.__sga_target=989855744
orcl.__shared_io_pool_size=0
orcl.__shared_pool_size=234881024
orcl.__streams_pool_size=0
*.audit_file_dest='/u01/app/oracle/admin/orcl/adump'
*.audit_trail='db'
*.compatible='11.2.0.4.0'
*.control_files='/u01/app/oracle/oradata/orcl/control01.ctl','/u01/app/oracle/fast_recovery_area/orcl/control02.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='orcl'
*.db_recovery_file_dest='/u01/app/oracle/fast_recovery_area'
*.db_recovery_file_dest_size=4385144832
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=orclXDB)'
*.fal_client='orcl'
*.fal_server='orcl_std'
*.log_archive_dest_1='location=USE_DB_RECOVERY_FILE_DEST valid_for=(all_logfiles,all_roles) db_unique_name=orcl'
*.log_archive_dest_2='SERVICE=orcl_std LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcl_std'
*.log_archive_dest_state_1='enable'
*.log_archive_dest_state_2='enable'
*.memory_target=1660944384
*.open_cursors=300
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.standby_file_management='AUTO'
*.undo_tablespace='UNDOTBS1'
*.db_unique_name='orcl'
*.log_archive_config='DG_CONFIG=(orcl,orcl_std)'

检查完成确认无误后,重启数据库  
SQL> shutdown immediate
SQL> start nomount
SQL> create spfile from pfile='/u01/app/oracle/product/11.2.0/db_1/dbs/initorcl.ora'; #以pfile创建新的spfile
SQL> startup
SQL> show parameter spfile #确认以spfile启动

备注:备库的pfile、control file将安排和rman备份完成后创建,然后和其他文件一起copy至备库。

 

4.主库配listener.ora和tnsname.ora

(1)主库listener.ora配置:
[oracle@dgp admin]$ cat /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
# listener.ora Network Configuration File: /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
(SID_LIST =
  (SID_DESC =
  (GLOBAL_DBNAME = orcl)
  (SID_NAME = orcl)
  )
)

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = dgp)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

ADR_BASE_LISTENER = /u01/app/oracle

(2)主库tnsnames.ora配置:
[oracle@dgp admin]$ cat /u01/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora
# tnsnames.ora Network Configuration File: /u01/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
ORCL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = dgp)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  )

ORCL_STD =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = dgs)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl_std)
    )
  )

(2)主库监听测试
# [oracle@dgp ~]$ lsnrctl reload
[oracle@dgp ~]$ lsnrctl stop
[oracle@dgp ~]$ lsnrctl start
[oracle@dgp ~]$ lsnrctl status

[oracle@dgp ~]$ sqlplus system/****@orcl #测试是否正常

 
至此主库的配置结束,下面将为备库准备相关文件。

5.RMAN备份主库

#在主库操作
[oracle@dgp ~]$ rman target /
RMAN> backup database plus archivelog

#备注:注意一下rman输出内容中的备份文件路径
#备用命令:
backup current controlfile for standby format '/XXX/dbbackup/CTL_%U'; backup database format '/XXX/dbbackup/DB_%d_%U' PLUS ARCHIVELOG;

 

6.创建备库pfile文件initorcl_std.ora

#在主库操作
[oracle@dgp ~]$ cd /u01/app/oracle/product/11.2.0/db_1/dbs
[oracle@dgp ~]$ cp initorcl.ora initorcl_std.ora
#修改initorcl_std.ora中内容,修改后的内容见下面新的initorcl_std.ora
#红色为DG相关参数,红色绿底为修改过的内容
[oracle@dgp ~]$ cat initorcl_std.ora
orcl.__db_cache_size=687865856
orcl.__java_pool_size=16777216
orcl.__large_pool_size=33554432
orcl.__oracle_base='/u01/app/oracle'#ORACLE_BASE set from environment
orcl.__pga_aggregate_target=671088640
orcl.__sga_target=989855744
orcl.__shared_io_pool_size=0
orcl.__shared_pool_size=234881024
orcl.__streams_pool_size=0
*.audit_file_dest='/u01/app/oracle/admin/orcl/adump'
*.audit_trail='db'
*.compatible='11.2.0.4.0'
*.control_files='/u01/app/oracle/oradata/orcl/control01.ctl','/u01/app/oracle/fast_recovery_area/orcl/control02.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='orcl'
*.db_recovery_file_dest='/u01/app/oracle/fast_recovery_area'
*.db_recovery_file_dest_size=4385144832
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=orclXDB)'
*.fal_client='orcl_std'
*.fal_server='orcl'
*.log_archive_dest_1='location=USE_DB_RECOVERY_FILE_DEST valid_for=(all_logfiles,all_roles) db_unique_name=orcl_std'
*.log_archive_dest_2='SERVICE=orcl LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcl'
*.log_archive_dest_state_1='enable'
*.log_archive_dest_state_2='enable'
*.memory_target=1660944384
*.open_cursors=300
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.standby_file_management='AUTO'
*.undo_tablespace='UNDOTBS1'
*.db_unique_name='orcl_std'
*.log_archive_config='DG_CONFIG=(orcl,orcl_std)'

 

7.创建备库控制文件

#在主库操作
[oracle@dgp ~]$ sqlplus / as sysdba
SQL> ALTER DATABASE CREATE STANDBY CONTROLFILE AS '/u01/app/oracle/fast_recovery_area/ORCL/backupset/orcl_stby.ctl'

 

8.传输文件至备库

需要copy至备库的文件有:rman备份、备库pfile、备库controlfile、listener.ora、tnsnames.ora、密码文件。

在备库上创建必要目录

在备库操作:
[root@dgs ~]# su - oracle
[oracle@dgs ~]$ mkdir -p /u01/app/oracle/oradata/orcl/
[oracle@dgs ~]$ mkdir -p /u01/app/oracle/fast_recovery_area/orcl/
[oracle@dgs ~]$ mkdir -p /u01/app/oracle/admin/orcl/adump
[oracle@dgs ~]$ mkdir -p /u01/app/oracle/fast_recovery_area/ORCL/
#[oracle@dgs ~]$ mkdir /u01/app/oracle/archivelog #忽略
配置主库oracle用户免密登录备库主机oracle用户

在主库操作: [oracle@dgp dbs]$
ssh-keygen #一直按回车 [oracle@dgp dbs]$ ssh-copy-id -i .ssh/id_rsa.pub oracle@dgs #输入备库oracle操作系统用户密码
开始copy文件至备库
# 1、controlfile 和 RMAN备份
[oracle@dgp ~]$ scp -r /u01/app/oracle/fast_recovery_area/ORCL/backupset oracle@dgs:/u01/app/oracle/fast_recovery_area/ORCL/
# 2、initorcl_std.ora密码文件orapworcl
[oracle@dgp ~]$ scp /u01/app/oracle/product/11.2.0/db_1/dbs/initorcl_std.ora oracle@dgs:/u01/app/oracle/product/11.2.0/db_1/dbs/
[oracle@dgp ~]$ scp /u01/app/oracle/product/11.2.0/db_1/dbs/orapworcl oracle@dgs:/u01/app/oracle/product/11.2.0/db_1/dbs/
#3、listener.ora 和 tnsnames.ora
[oracle@dgp ~]$ scp /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora oracle@dgs:/u01/app/oracle/product/11.2.0/db_1/network/admin/
[oracle@dgp ~]$ scp /u01/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora oracle@dgs:/u01/app/oracle/product/11.2.0/db_1/network/admin/

#复制备库控制文件至initorcl_std.ora指定位置(还是在主库操作,这里在主库ssh远程登录执行命令,避免切换窗口)
[oracle@dgp oracle]$ ssh oracle@dgs cp /u01/app/oracle/fast_recovery_area/ORCL/backupset/orcl_stby.ctl /u01/app/oracle/oradata/orcl/control01.ctl
[oracle@dgp oracle]$ ssh oracle@dgs cp /u01/app/oracle/fast_recovery_area/ORCL/backupset/orcl_stby.ctl /u01/app/oracle/fast_recovery_area/orcl/control02.ctl


至此,除listener.ora有待修改,备库的相关文件准备完成,下面将进入最后配置阶段。

9.备库配置监听

备库操作:

(1)修改监听件listener.ora
修改后查看,其他红色为修改的内容。
#注意:备库的SID_NAME为orcl,因此备库listener.ora静态监听SID_LIST_LISTENER中的配置
SID_NAME = orcl,不能设为orcl_std,orcl_std是db_unique_name
[oracle@dgs admin]$ cat /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
# listener.ora Network Configuration File: /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
(SID_LIST =
  (SID_DESC =
  (GLOBAL_DBNAME = orcl_std)
  (SID_NAME = orcl)
  )
)

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = dgs)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

ADR_BASE_LISTENER = /u01/app/oracle

(2)备库的tnsnames.ora和主库一样,无需修改
(3)重启监听并验证
   在备库:
    [oracle@dgs ~]$ lsnrctl stop
    [oracle@dgs ~]$ lsnrctl start
    [oracle@dgs ~]$ lsnrctl status
    [oracle@dgs ~]$ sqlplus sys/******@orcl as sysdba       #在备库上登录主库
    [oracle@dgs ~]$ sqlplus sys/******@orcl_std as sysdba    #在备库上登录备库
在主库:
    
[oracle@dgs ~]$ sqlplus sys/******@orcl as sysdba       #在主库上登录主库
    [oracle@dgs ~]$ sqlplus sys/******@orcl_std as sysdba    #在备库上登录备库

 

10.还原备库

#确认一下,至此备库相关文件均已修改完成。

备库RMAN恢复
[oracle@dgs oracle]$ export ORACLE_SID=orcl
[oracle@dgs oracle]$ sqlplus / as sysdba

SQL> create spfile from pfile='/u01/app/oracle/product/11.2.0/db_1/dbs/initorcl_std.ora'; #创建备库spfile
SQL> startup mount;
SQL> show paramter spfile; #确认以spfile启动
SQL> exit

[oracle@dgs oracle]$ rman target /
RMAN> alter database mount; #或者alter database mount standby database;
RMAN> restore database;
RMAN> recover database;

11.开启同步并验证

# 此时主库数据库处于open状态,备库处于mount状态
(1)开启同步:
在备库操作
SQL> alter database open read only;
SQL> alter database recover managed standby database using current logfile disconnect from session;

(2)验证

在主库操作
SQL> create user mydb identified by ******;
SQL> alter system switch logfile;
在备库操作
SQL> select username from dba_users where name='MYDB'

查看备库应用状态
SQL> select thread#,sequence#,standby_dest,archived,applied,status from v$archived_log;

 

三、基本管理

1.检查状态
查看数据库状态
select open_mode,protection_mode,database_role,switchover_status from v$database;
select max(sequence#) from v$log;--检查两边的序列好,是否一致
     
主库切换日志,
alert system switch logfile;

查看备库应用状态
select thread#,sequence#,standby_dest,archived,applied,status from v$archived_log;

查看有没有GAP
select * from v$archive_dest_status where dest_id=2;
select status,gap_status from v$archive_dest_status where dest_id=2;

备库查看RFS接收日志和MRP应用日志同步主库情况

select process, sequence#,GROUP#,thread#,status,delay_mins from v$managed_standby;  #备库查询其中,其中ARCH是归档进程,RFS是归档传输进程,MRP0是日志应用进程

备库查看日志应用进度及数据库模式

select dbid,name,open_mode,current_scn,protection_mode,database_role,force_logging,open_mode,switchover_status from v$database;

2.启动数据库--先启备库
sql>startup nomount
sql>alter database mount standby database;
sql>alter database open;                 #或者alter database open read only;
sql>alter database recover managed standby database using current logfile disconnect from session;
        
--再启主库
sql>startup

3.关闭数据库
--先关主库: sql>shutdown immediate
--
再关备用库: sql>alter database recover managed standby database cancel; --停止同步 sql>shutdown immediate;

4.GAP处理

就是有从库有日志尚未应用,存在着RESOLVABLE GAP ,解决方法如下:
SQL> select status,gap_status from v$archive_dest_status where dest_id=2;
STATUS      GAP_STATUS
--------- ------------------------
ERROR      RESOLVABLE GAP
#主库,将所有未传送的redo传送给从库,target_db_name使用DB_UNIQUE_NAME。 ALTER SYSTEM FLUSH REDO TO orcl_std; #验证备库 SELECT UNIQUE THREAD# AS THREAD, MAX(SEQUENCE#) OVER (PARTITION BY thread#) AS LAST from V$ARCHIVED_LOG;
#如果必要,拷贝归档日志到从库,并进行注册 ALTER DATABASE REGISTER PHYSICAL LOGFILE 'filespec1'; #再次检查 SELECT THREAD#, LOW_SEQUENCE#, HIGH_SEQUENCE# FROM V$ARCHIVE_GAP;

--在目标从库上,停止日志应用 ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL; --在目标从库上 ALTER DATABASE RECOVER MANAGED STANDBY DATABASE FINISH; --如果日志确定丢失,可以采用激活方式,单这样会有数据丢失。 --ALTER DATABASE ACTIVATE PHYSICAL STANDBY DATABASE; --验证目标从库 SELECT SWITCHOVER_STATUS FROM V$DATABASE; --开始切换,如果状态为"TO PRIMARY."则WITH SESSION SHUTDOWN从句可以去掉。 ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY WITH SESSION SHUTDOWN;
--打开新主库
ALTER DATABASE OPEN;

5.主备切换
https://www.cnblogs.com/abclife/p/5416671.html

1)原主库检查切换状态
SQL> select switchover_status from v$database;

SWITCHOVER_STATUS
--------------------
TO STANDBY
说明:状态为"TO STANDBY"说明原主库已准备好切换为备库。而此时原备库状态为:NOT ALLOWED

2)原主库切换角色
SQL> alter database commit to switchover to physical standby;
SQL> select name,open_mode,database_role,protection_mode,SWITCHOVER_STATUS  From v$database; #检查新备库状态和角色
说明:原主库切换为standby,原备库还未切换,并且两个数据库角色均为STANDBY。并且原备库切换状态为由"NOT ALLOWED"转变为"TO PRIMARY"。

3)原备库检查切换状态
SQL> select switchover_status from v$database;

SWITCHOVER_STATUS
--------------------
TO PRIMARY
4)原备库切换角色
SQL> alter database commit to switchover to primary;
SQL> alter database open;            #正常打开,read write。
SQL> select name,open_mode,database_role,protection_mode,SWITCHOVER_STATUS  From v$database;      #检查新主库状态和角色

#至此角色切换完成。

5)新备库开启同步
SQL> alter database open;           #standby 数据库默认以read only 打开。
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;  #开启新备库同步 
SQL> select name,open_mode,database_role,protection_mode,SWITCHOVER_STATUS  From v$database;        #检查新主库状态和角色

6)同步检查
略...
备注:角色切换之前,可以发现原备库目录/u01/app/oracle/oradata/orcl下没有 redo log 和 standby redo log文件,切换之后,系统已经创建了这些日志文件。










 




 

posted @ 2020-02-21 23:38  浮生若夢sky  阅读(901)  评论(0编辑  收藏  举报