主備庫記憶體不一致的Data Guard環境搭建

Yukki發表於2019-11-11

前言

前幾天朋友問了我一個問題,一個單節點RAC ,要做一套 Data Guard ,但是備庫能給資料庫的記憶體只有主庫的一半左右,能不能成功。不知道,沒做過,試試唄。

一.概況

1. 涉及的技術點

1) RAC 作為 primary database nonRAC 作為 standby database

2) 使用RMAN 作為資料庫的備份方式

3) 使用Backup-based duplication方式 建立備庫

4) 主庫使用ASM 儲存方式,備庫使用 filesystem 作為儲存

5) 使用standby logfile ,開啟日誌實時更新

2. 主備庫資訊表概要


Primary(RAC)

Standby(fs)

HOSTNAME

yukki

fuzhou

ORACLE_SID

cs1

stbcs1

DB_NAME

cs

cs

DB_UNIQUE_NAME

cs

stby

SERVICE_NAMES

cs_pri

cs_stb

INSTANCE_NAME

cs1

stbcs1

INSTANCE_NUMBER

1

1

THREAD

1

1

TEMPFILE_LOCATION

+DATA/cs/tempfile

/u01/db/oradata

二. Primary 主庫配置

1. 檢視 Managed Standby 元件

SYS@ cs1>select * from v$option where lower(parameter)='managed standby';

 

PARAMETER                                                        VALUE

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

Managed Standby                                                  TRUE

# 請確保該值為 true

2.  檢查remote_login_passwordfile 的設定

SYS@ cs1>show parameter remote_login_passwordfile

 

NAME                                 TYPE        VALUE

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

remote_login_passwordfile            string      EXCLUSIVE

# 若該引數不為 exclusive ,則按照以下命令修改,並重啟使其生效

SYS@ cs1>alter system set remote_login=exclusive scope=spfile;

3.  檢查資料庫是否為歸檔模式

SYS@ cs1>archive log list

Database log mode              Archive Mode

Automatic archival             Enabled

Archive destination             +DATA

Oldest online log sequence     87

Next log sequence to archive   89

Current log sequence           89

# 若為非歸檔模式,則需要乾淨的關閉資料庫後,啟動到 mount 模式,修改為歸檔模式後再開庫

SYS@ cs1>shutdown immediate

SYS@ cs1>startup mount

SYS@ cs1>alter database archivelog;

SYS@ cs1>alter database open;

SYS@ cs1>select log_mode from v$database;

4.  檢查資料庫是否開啟force logging

SYS@ cs1>select name,force_logging from v$database;

 

NAME      FOR

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

CS        YES

# 若資料庫未開啟 force logging ,則

SYS@ cs1>alter database force logging;

SYS@ cs1>select name,force_logging from v$database;

SYS@ cs1>alter system archive log current;

5.  檢查主庫口令檔案的MD5

[oracle@ yukki  dbs]$ openssl md5 orapwcs1

MD5(orapwcs1)= 7836520c978614723e57330e12ccbe90

# 要確保主備庫口令檔案的 MD5 值相同,即使 sys 的金鑰相同也不行

6.  主庫引數修改

SYS@ cs1>alter system set db_unique_name=cs scope=spfile;

SYS@ cs1>alter system set log_archive_config='dg_config=(cs,stby)';

SYS@ cs1>alter system set log_archive_dest_1='location=+DATA valid_for=(all_logfiles,all_roles) db_unique_name=cs';

SYS@ cs1>alter system set log_archive_dest_2='service=dbstandby async valid_for= (online_logfiles,primary_roles ) db_unique_name=stby';

SYS@ cs1>alter system set log_archive_dest_state_1=enable;

SYS@ cs1>alter system set log_archive_dest_state_ 2 =enable;

SYS@ cs1>alter system set log_archive_max_processes=30;

SYS@ cs1>alter system set fal_server=dbstandby;

SYS@ cs1>alter system set standby_file_management= auto ;

SYS@ cs1>alter system set db_file_name_convert=' +DATA /cs/datafile, /u01/db/oradata' scope=spfile;

SYS@ cs1>alter system set log_file_name_convert=' +DATA /cs/onlinelog, /u01/db/oradata'scope=spfile;

SYS@ cs1>alter system set service_names=cs_pri;

三. Standby 備庫配置

1.  準備standby 的口令檔案

# 拷貝主庫的口令檔案傳至備庫的 $ORACLE_HOME/dbs 目錄下,並重新命名為 orapwstbcs1

[oracle@ yukki  dbs]$ scp orapwcs1 oracle@ fuzhou :$ORACLE_HOME/dbs

[oracle@fuzhou dbs]$ mv orapwcs1 orapwstbcs1

# 檢查備庫口令檔案的 MD5 值,確保和主庫相同

[oracle@fuzhou dbs]$ openssl md5 orapwstbcs1

MD5(orapwstbcs1)= 7836520c978614723e57330e12ccbe90

2.  準備standby 的引數檔案

在主庫生成pfile ,並將其傳至備庫修改

SYS@ cs1>create pfile='/tmp/pfile2019110 1 ' from spfile;

[oracle@ yukki  tmp]$ scp pfile2019110 1   oracle@fuzhou:/tmp /initstbcs1.ora

[oracle@fuzhou dbs]$ vi initstbcs1.ora

stbcs1._...

...

*.audit_file_dest='/u01/db/admin/cs/adump'

*.audit_trail='db'

*.compatible='11.2.0.4.0'

*.control_files='/u01/db/oradata/control01.ctl','/u01/db/oradata/control02.ctl'#Restore Controlfile

*.db_block_size=8192

*.db_create_file_dest='/u01/db/oradata'

*.db_domain=''

*.db_file_name_convert='+DATA/cs/datafile','/u01/db/oradata'

*.db_name='cs'

*.db_recovery_file_dest='/u01/db/fast_recovery_area'

*.db_recovery_file_dest_size=4385144832

*.db_unique_name='STBY'

*.diagnostic_dest='/u01/db'

*.dispatchers='(PROTOCOL=TCP) (SERVICE=stbcsXDB)'

*.enable_goldengate_replication=TRUE

*.fal_server='DBPRIMARY'

*.log_archive_config='DG_CONFIG=(STBY,CS)'

*.log_archive_dest_1='location=/u01/db/arch valid_for=(all_logfiles,all_roles) db_unique_name=stby'

*.log_archive_dest_2='service=dbprimary async valid_for= (online_logfiles,primary_roles ) db_unique_name=cs'

*.log_archive_dest_state_1='ENABLE'

*.log_archive_dest_state_2='ENABLE'

*.log_archive_format='%t_%s_%r.dbf'

*.log_archive_max_processes=30

*.log_file_name_convert='+DATA/cs/onlinelog','/u01/db/oradata'

*.open_cursors=300

*.pga_aggregate_target=109715200

*.processes=150

*.remote_login_passwordfile='exclusive'

*.service_names='CS_STB'

*.sga_target=329145600

*.standby_file_management='AUTO'

*.undo_tablespace='UNDOTBS1'

# 此處需要注意的是由於實驗需求,備庫引數檔案裡的 sga_target pga_aggregate_target 需修改為主庫的一半

# 11g 中取消的引數:

*.standby_archive_dest

*.fal_client

3.  建立必要的目錄結構

[oracle@ fuzhou ~]$ mkdir -p /u01/db/admin/cs/adump

[oracle@ fuzhou ~]$ mkdir -p  /u01/db/oradata

[oracle@ fuzhou ~]$ mkdir -p  /u01/db/arch

[oracle@ fuzhou ~]$ mkdir -p  /u01/db/fast_recovery_area

4.  建立spfile ,並啟動 instance

[oracle@ fuzhou ~]$ export ORACLE_SID=stbcs1

[oracle@ fuzhou ~]$ sqlplus / as sysdba

SYS@ stbcs1>create spfile from pfile;

SYS@ stbcs1>startup nomount

SYS@ stbcs1>show parameter spfile

 

NAME      TYPE      VALUE

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

spfile         string   /u01/db/product/11204/dbhome_1/dbs/spfilestbcs1.ora

四. Backup-based duplication 複製 physical standby

1.  listener.ora 配置

# 由於 standby 端只有 oracle 軟體,例項無法啟動到 mount 狀態,此時 PMON 程式無法完成自動註冊,故採用靜態監聽。

主庫:

[grid@ yukki ~]$ cat /u01/11.2.0/grid/network/admin/listener.ora

LISTENER=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=IPC)(KEY=LISTENER))))                                                                    # line added by Agent

LISTENER_SCAN1=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=IPC)(KEY=LISTENER_SCAN1))))                                                       # line added by Agent

ENABLE_GLOBAL_DYNAMIC_ENDPOINT_LISTENER_SCAN1=ON        # line added by Agent

ENABLE_GLOBAL_DYNAMIC_ENDPOINT_LISTENER=ON              # line added by Agent

 

SID_LIST_LISTENER =

    (SID_LIST =

    (SID_DESC =

        (GLOBAL_DBNAME= cs_pri)

        (ORACLE_HOME = /u01/db/product/11204/dbhome_1)

        (SID_NAME =cs1)

      )

)

備庫:

[oracle@ fuzhou ~]$ cat /u01/db/product/11204/dbhome_1/network/admin/listener.ora

# listener.ora Network Configuration File: /u01/db/product/11204/dbhome_1/network/admin/listener.ora

# Generated by Oracle configuration tools.

 

LISTENER =

  (DESCRIPTION_LIST =

    (DESCRIPTION =

      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.3.66)(PORT = 1521))

    )

  )

 

 

SID_LIST_LISTENER =

    (SID_LIST =

    (SID_DESC =

        (GLOBAL_DBNAME= cs_stb)

        (ORACLE_HOME = /u01/db/product/11204/dbhome_1)

        (SID_NAME =stbcs1)

      )

)

2.  tnsnames.ora 配置

往主備庫的$ORACLE_HOME/network/admin/tnsnames.ora 中新增:

dbprimary =

  (DESCRIPTION =

    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.3.88)(PORT = 1521))

    (CONNECT_DATA =

      (SERVER = DEDICATED)

      (SERVICE_NAME = cs_pri)

  )

 )

 

dbstandby =

  (DESCRIPTION =

    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.3.66)(PORT = 1521))

    (CONNECT_DATA =

      (SERVER = DEDICATED)

      (SERVICE_NAME = cs_stb)

  )

)

3.  備份primary 資料庫

1) 檢視資料庫物理結構

[oracle@ yukki ~]$ rman target /

 

Recovery Manager: Release 11.2.0.4.0 - Production on Mon Nov 4 17:40:28 2019

 

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

 

connected to target database: CS (DBID=1434125244)

 

RMAN> report schema;

 

using target database control file instead of recovery catalog

Report of database schema for database with db_unique_name CS

 

List of Permanent Datafiles

===========================

File Size(MB) Tablespace   RB segs Datafile Name

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

1    750      SYSTEM       ***     +DATADG/cs/datafile/system.256.1018198953

2    580      SYSAUX       ***     +DATADG/cs/datafile/sysaux.257.1018198953

3    75       UNDOTBS1     ***     +DATADG/cs/datafile/undotbs1.258.1018198953

4    5        USERS        ***     +DATADG/cs/datafile/users.259.1018198953

5    50       TEST         ***     +DATADG/cs/datafile/test.dbf

 

List of Temporary Files

=======================

File Size(MB) Tablespace   Maxsize(MB) Tempfile Name

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

1    29       TEMP         32767       +DATADG/cs/tempfile/temp.268.1018199043

2) 備份資料庫和控制檔案

run{

sql 'alter system archive log current';

allocate channel c1 device type disk;

allocate channel c2 device type disk;

allocate channel c3 device type disk;

backup database filesperset 1 format '/backup/whole_%d_%U_%t.bus';

backup current controlfile for standby format '/backup/ctl_%d_%U_%t.bus';

release channel c1;

release channel c2;

release channel c3;

}

3) 備份歸檔日誌

run{

sql 'alter system archive log current';

allocate channel c1 device type disk;

allocate channel c2 device type disk;

backup archivelog all format '/backup/ arch _%d_%U_%t.bus';

release channel c1;

release channel c2;

}

 

4) 將備份傳至備庫機

[oracle@ yukki  ~]$ scp /backup/* oracle@fuzhou:/backup

4.  使用duplicate 進行資料庫恢復

1) 建立指令碼

[oracle@ yukki  ~]$ vi  duplicate.sh

 

connect target sys/oracle@dbprimary

connect auxiliary sys/oracle@dbstandby

run{

allocate channel c1 device type disk;

allocate channel c2 device type disk;

allocate channel c3 device type disk;

allocate auxiliary channel aux1 device type disk;

allocate auxiliary channel aux2 device type disk;

allocate auxiliary channel aux3 device type disk;

set until sequence=87 thread=1;

set newname for tempfile 1 to '/u01/db/oradata/temp01.dbf';

duplicate target database for standby  nofilenamecheck dorecover;

release channel aux1;

release channel aux2;

release channel aux3;

release channel c1;

release channel c2;

release channel c3;

}

# 由於沒有 temp_file_name_convert 這個引數,故在 duplicate 前需要給 tempfile set newname 操作

# 手動分配複製通道時,必須要加上 allocate auxiliary channel ,否則會提示 :

RMAN-05503: at least one auxiliary channel must be allocated to execute this command

#如果duplicate的時候使用關鍵詞from active database(通過網路直傳不落地的active database duplication方式,不需要主庫的備份,節省了磁碟空間和傳輸備份的時間,但在複製的過程中對主庫有一定壓力,需要一定的網路頻寬),則必須為主庫分配通道,否則會提示:

RMAN-06034: at least 1 channel must be allocated to execute this command

2) 使用nohup 呼叫指令碼,使其在後臺執行

[oracle@ yukki ~]$ nohup rman cmdfile=duplicate.sh >duplicate.log  &

5.  啟動physical standby

SYS@ stbcs1>shutdown immediate;

SYS@ stbcs1>startup;

SYS@ stbcs1>recover managed standby database disconnect from session;

SYS@ stbcs1>select name,open_mode,database_role,protection_mode,switchover_status,controlfile_type from v$database;

 

NAME    OPEN_MODE                DATABASE_ROLE        PROTECTION_MODE            SWITCHOVER_STATUS        CONTROL

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

CS       READ ONLY WITH APPLY       PHYSICAL STANDBY     MAXIMUM PERFORMANCE  NOT ALLOWED          STANDBY

五. DATAGUARD 使用 standby logfile

1.  standby logfile 建立要求

# 確保主備庫的日誌檔案大小相同,建議備庫的 standby logfile 要比主庫的 redo logfile 多一組,目的是確保備庫隨時都有一組空閒日誌可使用。

# 當使用 rman 生成 controlfile for standby 的備份時, alert 日誌中會有相關的提示資訊,如下:

Clearing standby activation ID 1434109882 (0x557ac7ba)

The primary database controlfile was created using the

'MAXLOGFILES 192' clause.

There is space for up to 189 standby redo logfiles

Use the following SQL commands on the standby database to create

standby redo logfiles that match the primary database:

ALTER DATABASE ADD STANDBY LOGFILE 'srl1.f' SIZE 52428800;

ALTER DATABASE ADD STANDBY LOGFILE 'srl2.f' SIZE 52428800;

ALTER DATABASE ADD STANDBY LOGFILE 'srl3.f' SIZE 52428800;

ALTER DATABASE ADD STANDBY LOGFILE 'srl4.f' SIZE 52428800;

WARNING: OMF is enabled on this database. Creating a physical

standby controlfile, when OMF is enabled on the primary

database, requires manual RMAN intervention to resolve OMF

datafile pathnames.

NOTE: Please refer to the RMAN documentation for procedures

describing how to manually resolve OMF datafile pathnames.

2.  備庫新增standby logfile

# 首先檢視主庫 online redo logfiles 的資訊

SYS@ cs1>select group#,thread#,bytes from v$log;

 

    GROUP#    THREAD#      BYTES

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

         1          1   52428800

         2          1   52428800

         3          1   52428800

# 確保主庫 ORLs 日誌組大小相同,再配置 SRLs ,且在備庫新增 standby logfile 時,要先停掉 MRP 程式:

SYS@ stbcs1> recover managed standby database cancel;

SYS@ stbcs1>alter database add standby logfile thread 1 group 11  '/u01/db/oradata/stb_redo01.log' size 52428800;

SYS@ stbcs1>alter database add standby logfile thread 1 group 12  '/u01/db/oradata/stb_redo0 2 .log' size 52428800;

SYS@ stbcs1>alter database add standby logfile thread 1 group 13  '/u01/db/oradata/stb_redo0 3 .log' size 52428800;

SYS@ stbcs1>alter database add standby logfile thread 1 group 14  '/u01/db/oradata/stb_redo0 4 .log' size 52428800;

# 由於主庫有三組 ORLs ,在建立 SRLs 的時候若不指定組數,預設會是 4-7 ,那麼後續在主庫新增日誌組的話就會產生混亂,故從第 11 組開始配置 standby redo logfiles

# 還有就是當主庫多例項的時候,備庫也要配置上多個 thread ,目的是為了能開啟 real time apply ,但是如果備庫只建立了 thread 1 ,並不會影響 archive log 的傳輸和應用,但是備庫並不會採用 real time apply ,主庫 online redo 無法做到實時傳輸應用,只在歸檔切換後備庫才會應用。

3.  主庫新增standby logfile

SYS@ cs1>alter database add standby logfile thread 1  group 1 1  '+DATADG/cs/onlinelog/stb y_ redo01.log' size 52428800;

SYS@ cs1>alter database add standby logfile thread 1  group 1 2  '+DATADG/cs/onlinelog/stb y_ redo0 2 .log' size 52428800;

SYS@ cs1>alter database add standby logfile thread 1  group 1 3  '+DATADG/cs/onlinelog/stb y_ redo0 3 .log' size 52428800;

SYS@ cs1>alter database add standby logfile thread 1  group 1 4  '+DATADG/cs/onlinelog/stb y_ redo0 4 .log' size 52428800;

# 在配置備庫的 standby logfile 的時候,也需要在主庫上預配置,目的是用於未來切換使用。

六. 部分引數說明

1.  db_name

資料庫名稱,一套Data Guard 環境中,需要保持主備庫的 db_name 相同。

2.  db_unique_name

DG 環境中用於區分主備庫的唯一名字,即使主備庫角色互換, db_unique_name 也不會更改。

3.  log_archive_config

該引數通過dg_configs 設定同一個 Data Guard 環境中的所有 db_unique_name ,以逗號分隔,定義該引數能確保主備資料庫能夠傳送或接收日誌。

4.  log_archive_dest_1

通過location 設定日誌歸檔的本地路徑,主備庫需要定義各自的 Online Redo Log 的歸檔地址。本例 log_archive_dest_1='location=+DATA valid_for=(all_logfiles,all_roles) db_unique_name=cs' ,可以理解為對主庫( cs )而言,不管她是主庫還是備庫( all_roles ),她都會自己完成歸檔動作,並將日誌歸檔於本地路徑 +DATA 下。

5.  log_archive_dest_2

該引數僅當資料庫角色為primary 時生效,指定 primary 傳輸 redo log 到該引數定義的 standby database 上,其中 service 的設定為 tnsnames.ora 中定義的 Oracle Net 名稱。 log_archive_dest_2 可以說是 dataguard 上最重要的引數之一,它定義了 redo log 的傳輸方式 (sync or async) 以及傳輸目標 ( standby apply node) ,直接決定了 dataguard 的資料保護級別。

6.  fal_server

fal fatch archive log ,其值為 tnsnames.ora 中遠端資料庫服務的 Oracle Net 名稱, fal_server 為備庫中設定的引數,一旦備庫產生 gap ,會通過 fal_server 引數向主庫請求傳輸缺失的日誌,當然為了 switchover ,主庫上也要預配置該引數。

7.  db_file_name_convert

定義主備庫的資料檔案路徑轉換,遠端在前,本地端在後。若有多個,逐一指明對映關係。

8.  log_file_name_convert

定義主備庫線上日誌檔案路徑轉換,遠端在前,本地端在後。若有多個,逐一指明對映關係。

9.  standby_file_management

備庫引數,用來控制是否主動將主庫增加表空間或資料檔案的改動,傳播到物理備庫。

auto :主庫執行的表空間建立操作會被傳播到物理備庫上執行。

manual default ,需要手工複製新建立的資料檔案到物理備庫伺服器。

10.  service_name tnsnames.ora 中的引數)

service_name 是在多例項出現後,為了方便應用連線資料庫提出的引數,該引數直接對應資料庫而不是某個例項,故該引數與 sid 沒有直接關係,不必與 sid 一樣。當伺服器端 listener.ora 中配置了靜態監聽後,客戶端 tnsnames.ora service_name 與伺服器端靜態監聽中的 GLOBAL_DBNAME 相對應,且可不必與伺服器端資料庫中 service_names 引數對應。但若沒有配置靜態監聽,客戶端 tnsnames.ora 裡的 service_name 需要從伺服器端資料庫中的 service_names 中取值。

以上,主備庫記憶體不一致,可以搭建Data Guard 環境。

 


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

相關文章