oracle 11g datagurd主從切換

賀子_DBA時代發表於2018-07-21

前言:

環境介紹:

主庫:10.9.21.57

備庫:10.9.21.59

要想完成主從切換,需要保證如下幾點:

1.需要保證主備庫都得設定了下面的引數

*.log_archive_config='DG_CONFIG=(stdb59,testdb57)'

*.log_archive_dest_2='SERVICE=testdb57

LGWR  SYNC VALID_FOR=(ONLINE_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=testdb57'

*.log_archive_dest_state_1='ENABLE'

*.log_archive_dest_state_2='ENABLE'

*.LOG_FILE_NAME_CONVERT='/data/u01/app/oracle/oradata/testdb57','/data/u01/app/oracle/oradata/stdb59'

*.DB_FILE_NAME_CONVERT='/data/u01/app/oracle/oradata/testdb57','/data/u01/app/oracle/oradata/stdb59'

最好都設定:

FAL_SERVER

2.如果你想實時應用的話,主備庫都得新增了standby redo log;

概述:本文首先介紹正常的switchover,這樣方式用於主從切換演練,資料庫遷移等等,接著會介紹另一種切換方式是failover,也就是當主庫crash了,需要把從庫提升為新的主庫,failover可能會丟失部分資料!

首先介紹switchover:

一:在主庫上執行(10.9.21.57)

1.檢視主庫的狀態:

SQL>  select name,open_mode,protection_mode from v$database;

NAME  OPEN_MODE       PROTECTION_MODE

--------- -------------------- --------------------

TESTDB57  READ WRITE       MAXIMUM PERFORMANCE

SQL>  select switchover_status ,database_role from v$database;

SWITCHOVER_STATUS    DATABASE_ROLE

-------------------- ----------------

TO STANDBY     PRIMARY

SWITCHOVER_STATUS–>TO STANDBY,表示可以正常切換.

SWITCHOVER_STATUS–>SESSIONS ACTIVE,表示當前有會話處於ACTIVE狀態,此時切換的話,需要加引數with session shutdown wait

2.在主庫執行切換:

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

Database altered.

3.主庫執行切換之後,發現主庫例項已經down了!

SQL>  select switchover_status ,database_role from v$database;

select switchover_status ,database_role from v$database

*

ERROR at line 1:

ORA-01034: ORACLE not available

Process ID: 18269

Session ID: 196 Serial number: 9

4.啟動主庫到mount狀態

SQL> startup mount

ORACLE instance started.

Total System Global Area 1603411968 bytes

Fixed Size    2253664 bytes

Variable Size  973081760 bytes

Database Buffers  620756992 bytes

Redo Buffers    7319552 bytes

Database mounted.

5檢視主庫的狀態,發現資料庫已經變成了PHYSICAL STANDBY的角色

SQL>  select switchover_status ,database_role from v$database;

SWITCHOVER_STATUS    DATABASE_ROLE

-------------------- ----------------

RECOVERY NEEDED     PHYSICAL STANDBY

6.以redo  only方式啟動新的standby 庫

SQL> alter database open read only;

Database altered.

二:備庫上操作:(10.9.21.59)

1.主庫的切換操作會傳輸到備庫,備庫的狀態就會自動轉換為to primary狀態;接下來檢視備庫的狀態:

SQL> select name,open_mode,protection_mode from v$database;

NAME          OPEN_MODE                        PROTECTION_MODE

--------- -------------------- -----------------------------------------------------------------

TESTDB57     READ ONLY WITH APPLY           MAXIMUM PERFORMANCE

SQL>  select switchover_status ,database_role from v$database;

SWITCHOVER_STATUS    DATABASE_ROLE

-------------------- ----------------

TO PRIMARY     PHYSICAL STANDBY

2.確認沒有問題後,可以進行切換轉換standby 到primary 角色,這個過程會把資料庫從open read only的狀態變成為mounted的狀態,他必須這樣,因為之前是read only的,現在他要變成主了,需要以read write方式從新open;

SQL> alter database commit to switchover to primary;

Database altered.

3.再次檢視備庫的狀態,發現資料庫角色變成了primary,

SQL>  select switchover_status ,database_role from v$database;

SWITCHOVER_STATUS    DATABASE_ROLE

-------------------- ----------------

NOT ALLOWED     PRIMARY

4.此時檢視新的主庫的狀態,發現確實已經由之前的open read only的狀態變成為mounted的狀態,

SQL> select status from v$instance;

STATUS

------------

MOUNTED

5.以read write方式從新open

SQL> alter database open;

Database altered.

三:回到新的從庫( 10.9.21.57)

1.開啟mrp程式應用日誌,重演變化!

SQL> alter database recover managed standby database using current logfile disconnect from session;

Database altered.

2.檢視從庫的各個程式的狀態,

SQL> select process,status from v$managed_standby;

PROCESS   STATUS

--------- ------------

ARCH  CLOSING

ARCH  CONNECTED

ARCH  CONNECTED

ARCH  CLOSING

MRP0  APPLYING_LOG

RFS  IDLE

RFS  IDLE

RFS  IDLE

驗證:

在主庫建立一個表

1.10.9.21.59 上操作:

SQL> create table liuwenhe.liuwenhe(id int ,name varchar(100));

Table created.

SQL> insert into liuwenhe.liuwenhe values ( 1,'liuwenhe');

1 row created.

SQL> commit;

Commit complete.

2.在10.9.21.57 上檢視是夠已經同步過來,如下已經同步過來了!

SQL> select * from liuwenhe.liuwenhe;

ID       NAME

---------- ------------------------------------------

1        liuwenhe

至此主從switchover切換完畢了

接著介紹failover的操作過程:

一:在主庫上操作(10.9.21.57)

1.模擬主庫crash的場景:

[root@testdb57 ~]# reboot

二:在從庫執行:( 10.9.21.59)

1.檢視有沒有gap

SQL> select * from v$archive_gap;

no rows selected

2.檢視此時的狀態:

SQL> select name,open_mode,switchover_status, DATABASE_ROLE,PROtection_mode from v$database;

NAME  OPEN_MODE       SWITCHOVER_STATUS    DATABASE_ROLE    PROTECTION_MODE

--------- -------------------- -------------------- ---------------- --------------------

TESTDB57  READ ONLY WITH APPLY NOT ALLOWED    PHYSICAL STANDBY MAXIMUM PERFORMANCE

3.初始化failover,相當於switchover的時候在主庫執行了 alter database commit to switchover to physical standby with session shutdown wait;

SQL> alter database  recover managed standby database finish force;

Database altered.

4.再次檢視備庫的狀態,發現SWITCHOVER_STATUS 變成了TO PRIMARY;

SQL> select name,open_mode,switchover_status, DATABASE_ROLE,protection_mode from v$database;

NAME  OPEN_MODE       SWITCHOVER_STATUS    DATABASE_ROLE    PROTECTION_MODE

--------- -------------------- -------------------- ---------------- --------------------

TESTDB57  MOUNTED       TO PRIMARY    PHYSICAL STANDBY MAXIMUM PERFORMANCE

5.將備課轉換成主庫!

SQL> alter database commit to switchover to primary;

Database altered.

6.再次檢視備庫的狀態,發現database_role變成了 PRIMARY了!並且open_mode變成了mounted的狀態了!

SQL> select name,open_mode,switchover_status, DATABASE_ROLE,PROtection_mode from v$database;

NAME  OPEN_MODE       SWITCHOVER_STATUS    DATABASE_ROLE    PROTECTION_MODE

--------- -------------------- -------------------- ---------------- --------------------

TESTDB57  MOUNTED       NOT ALLOWED     PRIMARY     MAXIMUM PERFORMANCE

7.開啟新的主

SQL> alter database open ;

Database altered.

至此oracle11g的主從failover切換完畢!


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29654823/viewspace-2158290/,如需轉載,請註明出處,否則將追究法律責任。

相關文章