Oracle dataguard之主备库切换(switchover)

1.确保主库的所有日志文件都刷到备库

查询备库的v$archived_log视图。

SQL> select sequence#,applied from v$archived_log;

 SEQUENCE# APPLIED
---------- ---------
	48 YES
	49 YES
	50 YES
	51 YES
	52 YES
	53 YES
	55 YES
	54 YES
	56 YES
	57 YES
	58 YES

 SEQUENCE# APPLIED
---------- ---------
	59 YES
	60 YES
	61 YES
	62 YES
	63 YES
	64 YES
	65 YES
	66 YES

19 rows selected.

2.查看备库可以切换的状态

查询备库v$database视图。

SQL> select switchover_status from v$database;

SWITCHOVER_STATUS
--------------------
TO PRIMARY

3.查看主库可以切换的状态

查询主库v$databse视图。

SQL> select switchover_status from v$database;

SWITCHOVER_STATUS
--------------------
SESSIONS ACTIVE

4.将主库切换成备库

SQL> alter database commit to switchover to physical standby with session shutdown;

5.查看主库可以切换的状态

SQL> select switchover_status from v$database;

SWITCHOVER_STATUS
--------------------
TO PRIMARY

6.关闭主库

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.

7.将备库切换成主库

SQL> alter database commit to switchover to primary with session shutdown;

Database altered.

8.打开原备库(新主库)并查看数据库读写状态

SQL> alter database open;

Database altered.

SQL> select open_mode from v$database;

OPEN_MODE
--------------------
READ WRITE

9.打开原主库(新备库)并查看数据库读写状态

SQL> startup
ORACLE instance started.

Total System Global Area 1653518336 bytes
Fixed Size		    2253784 bytes
Variable Size		 1006636072 bytes
Database Buffers	  637534208 bytes
Redo Buffers		    7094272 bytes
Database mounted.
Database opened.
SQL> select open_mode from v$database;

OPEN_MODE
--------------------
READ ONLY

10.开启新备库(原主库)的MRP

SQL> alter database recover managed standby database disconnect from session;

Database altered.

11.验证新主库(原备库)

SQL> set lines 200  
SQL> col dest_name for a20  
SQL> col error for a20  
SQL> select dest_name,status,process,error,transmit_mode from v$archive_dest where target='STANDBY';  
  
DEST_NAME        STATUS    PROCESS    ERROR            TRANSMIT_MOD  
-------------------- --------- ---------- -------------------- ------------  
LOG_ARCHIVE_DEST_2   VALID     LGWR                ASYNCHRONOUS  

12.新主库(原备库)创建表

SQL> create table Shenyu(id number(10),name varchar2(20));

Table created.

SQL> insert into Shenyu values(1,'Csong');

1 row created.

SQL> insert into Shenyu values(2,'Liyuanyuan');

1 row created.

SQL> commit;

Commit complete.

SQL> alter system switch logfile;

System altered.

13.查看新备库(原主库)

SQL> desc Shenyu
 Name					   Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID						    NUMBER(10)
 NAME						    VARCHAR2(20)

SQL> select * from Shenyu;

	ID NAME
---------- --------------------
	 1 Csong
	 2 Liyuanyuan

这样主备库通过switch over切换已经全部完成。

你可能感兴趣的:(Oracle,DataGuard)