文章詳情頁
Oracle 數(shù)據(jù)庫集中復(fù)制方法逐步精細
瀏覽:119日期:2023-11-24 19:38:00
日益增長的分布式應(yīng)用需求要求實現(xiàn)更好分布式的軟件環(huán)境,不斷推動著分布式技術(shù)的進步。Oracle數(shù)據(jù)復(fù)制是實現(xiàn)分布式數(shù)據(jù)環(huán)境的一種技術(shù),通過在不同的物理站點拷貝數(shù)據(jù)來建立分布式數(shù)據(jù)環(huán)境。它與分布式數(shù)據(jù)庫不同,在分布式數(shù)據(jù)庫中,雖然每個數(shù)據(jù)對象也對所有的站點可用,但是特定的數(shù)據(jù)對象只存在于一個特定的站點中。而數(shù)據(jù)復(fù)制實現(xiàn)所有的站點都有相同數(shù)據(jù)對象的可用拷貝。 在一個典型的分布式商業(yè)應(yīng)用中經(jīng)常需要把個地區(qū)的數(shù)據(jù)備份到總部的數(shù)據(jù)庫中,一方面可以作為一種備份方式,另一方面也方便總部應(yīng)用中的綜合統(tǒng)計。這是Oracle數(shù)據(jù)復(fù)制中的簡單應(yīng)用,本文將以這樣一個例子,講述如何實現(xiàn)Oracle數(shù)據(jù)復(fù)制。 實際情況是,A公司總部在北京,有三個營業(yè)部分別位于上海(Orace.ShangeHai.com)、杭州(Oracle.HangZhou.com)和武漢(Oracle.Wuhan.com)。三個營業(yè)部的軟件系統(tǒng)相同,數(shù)據(jù)庫結(jié)構(gòu)也相同?,F(xiàn)在需要把三個營業(yè)部的數(shù)據(jù)全部備份到總部的數(shù)據(jù)庫中。 預(yù)備工作 在進行復(fù)制之前需要預(yù)備的東西很多,當(dāng)然最基礎(chǔ)就是網(wǎng)絡(luò)必須暢通,之后需要收集一些復(fù)制環(huán)境的基本信息: 1. 需要復(fù)制的數(shù)據(jù)庫站點的數(shù)量。 2. 每個站點的Oracle版本號。 3. 每個需要復(fù)制的數(shù)據(jù)庫的大小。 4. 每個數(shù)據(jù)庫所使用的字符集。 5. 每個需要復(fù)制的數(shù)據(jù)所用的方案名。 收集完環(huán)境信息,可以開始建立總部的集中數(shù)據(jù)庫,集中數(shù)據(jù)庫要求版本高于所有主戰(zhàn)點的版本,最好所有的數(shù)據(jù)庫都是用相同的字符集。建好庫后為每個主站點的備份數(shù)據(jù)分別建一個表空間,表空間大于需要復(fù)制的數(shù)據(jù)量,至于預(yù)留以后的發(fā)展空間視實際情況而定。 為每個主站點的對應(yīng)復(fù)制數(shù)據(jù)建立方案,假如各個主站點所使用的方案名不同,在集中數(shù)據(jù)庫站點分別建立名稱相同的對應(yīng)方案。否則為各主站點的復(fù)制數(shù)據(jù)分別建立相應(yīng)的方案名。實際情況是后者,各營業(yè)部的數(shù)據(jù)庫都是用Oracle的方案名,這里我們建立三個對應(yīng)方案:SHORACL、HZORACL 和WHORACL。所有數(shù)據(jù)庫的版本都是9i。 基本概念 復(fù)制之前先解釋一下復(fù)制中的幾個概念: 1.主站點(Mater Site):在復(fù)制過程中提供數(shù)據(jù)源的站點。如上圖中的上海數(shù)據(jù)庫站點。 2.實體化視圖站點(Materialized View Site):實體化視圖復(fù)制中的目標(biāo)站點。 3.多主體站點復(fù)制(Multimaster Replication):復(fù)制環(huán)境中的站點都是主站點,對復(fù)制的數(shù)據(jù)庫對象有相同的治理權(quán)限。 4.實體化視圖復(fù)制(Materialized View Replication):一個主體站點提供源復(fù)制對象,一個實體化視圖站點拷貝主站點數(shù)據(jù)。 5.實體化視圖(Materialized View):在實體化視圖站點為每個復(fù)制表或者視圖建立一個對應(yīng)的表保存相應(yīng)的數(shù)據(jù),該表只能通過Oracle的復(fù)制機制進行增刪改數(shù)據(jù)的操作。 6. 快速刷新、完全刷新和強制刷新:復(fù)制過程中的三種刷新方式??焖偎⑿轮粡?fù)制源數(shù)據(jù)對象的改變部分;完全刷新每次都拷貝一遍源數(shù)據(jù)對象;強制刷新是數(shù)據(jù)庫的一個折衷方案,假如快速刷新失敗則使用完全刷新。 7. 主體組(Master Group):主體站點中被復(fù)制的源數(shù)據(jù)對象的集合。 8. 實體化視圖組(Materialized View Site):實體化視圖站點中復(fù)制對象的集合。 9. 實體化視圖日志(Materialized View Log):實體化視圖復(fù)制中使用快速刷新時記錄主體源數(shù)據(jù)對象操作日志的表。 進行復(fù)制 配置好本地服務(wù)名分別為:上海站點:SH,杭州站點:HZ,武漢站點:WH,北京站點:BJ,進入沒有登錄的sqlplus,讓我們開始復(fù)制。 一.設(shè)置主站點: 這里以上海主站點設(shè)置為例。 1.連接主站點,創(chuàng)建復(fù)制治理員并授予相應(yīng)的權(quán)限,復(fù)制治理員是治理整個復(fù)制環(huán)境并創(chuàng)建復(fù)制對象的用戶。只有數(shù)據(jù)治理員可以建立主體組和實體化視圖組。 connect system/passwd@SH create user repadmin identified by repadmin; begin dbms_repcat_admin.grant_admin_any_schema( username=>’repadmin’); end; /grant comment any table to REPADMIN; grant lock any table to REPADMIN;后面的兩個grant語句使復(fù)制治理員可以為任何表建立實體化視圖日志。假如想改用戶可以使用視圖治理器,還需要下面的命令: grant select any dictionary to REPADMIN; 2.注冊傳播方,傳播方會將主體站點的延遲事務(wù)隊列推入其他主體站點或者實體化視圖站點。 begin dbms_defer_sys.register_purpagator(username=>’repadmin’); end;3.調(diào)度清除作業(yè),該作業(yè)會定時清除延遲事務(wù)隊列并用傳播方將延遲事務(wù)推入其他主體站點或者實體化視圖站點。先更換用戶: disconnect; connect repadmin/repadmin@SH; begin dbms_defer_sys.schedule_purge( next_date=>sysdate,interval=>’sysdate + 1’,delay_seconds=>0); end;next_date:下一次執(zhí)行日期,sysdate表示立即。 interval:間隔時段,sysdate + 1表示間隔一天,sysdate+ 1/24表示間隔一小時 delay_seconds:當(dāng)延遲隊列沒有延遲事件時停止被次清除操作的延遲時間。 4.為實體化視圖站點建立復(fù)制代理。創(chuàng)建復(fù)制代理用戶并授予視圖接受方權(quán)限。復(fù)制代理是復(fù)制接收方連接主體站點的用戶 disconnect; connect system/passwd@SH; create user proxy_bjoracle identified by proxy_bjoracle; begin dbms_repcat_admin.register_user_repgroup( user_name=>’proxy_bjoracle, privilege_type => ’proxy_snapadmin’,list_of_gnames => NULL); end; /grant select_catalog_role to proxy_bjoracle;5. 創(chuàng)建主體組: disconnect; connect repadmin/repadmin@SH; begin dbms_repcat.create_master_repgroup(gname=>’sh_rep’); end; /6. 向主體組中添加復(fù)制對象: a) 添加表: begin dbms_repcat.create_master_repobject( gname=>’sh_rep’, type=>’TABLE’, oname=>’ CREDIT_CARD’ sname=>’SHORACL’ use_existing_object=>TRUE, copy_rows=>TRUE); end;b) 添加索引: begin dbms_repcat.create_master_repobject( gname=>’sh_rep’, type=>’INDEX’, oname=>’ INDEX_CREDIT_CARD’ sname=>’SHORACL’ use_existing_object=>TRUE, copy_rows=>FALSE); end; /7. 假如添加的表沒有主鍵需要設(shè)置可以代替主鍵的列或者列的集合: begin dbms_repcat.set_columns( sname => ’SHORACL’, oname => ’ CREDIT_CARD ’, column_list => ’ CREDIT_CARD_ID’); end; /8. 在主體組中的數(shù)據(jù)對象可以被復(fù)制之前,必須為他們生成復(fù)制支持。該方法為復(fù)制創(chuàng)建必要的觸發(fā)器、包或者存儲過程: begin dbms_repcat.generate_replication_support( sname=>’SHORACL’, oname=>’ CREDIT_CARD’, type=>’TABLE’, min_communication=>TRUE); end; /9. 為快速刷新創(chuàng)建實體化視圖日志: create materialized view log on SHORACL. CREDIT_CARD; 假如是沒有主鍵的表示用一下語句: create materialized view log on SHORACL. CREDIT_CARD with rowid excluding new values;10.啟動復(fù)制: begin dbms_repcat.resume_master_activity( name=>’sh_rep’); end; /二.設(shè)置實體化視圖站點: 1.創(chuàng)建復(fù)制治理員并授予相應(yīng)的權(quán)限: disconnect; connect system/passwd@BJ; create user mvadmin identified by mvadmin; begin dbms_repcat_admin. grant_admin_any_schema(username=> ’mvadmin’); end; /grant comment any table to mvadmin; grant lock any table to mvadmin; grant select any dictionary to mvadmin;2.注冊傳播方: begin dbms_defer_sys.register_propagator( username => ’mvadmin’); end; /3.公共數(shù)據(jù)庫連接。需要每個復(fù)制需要創(chuàng)建三個數(shù)據(jù)庫連接。公共數(shù)據(jù)庫連接指定數(shù)據(jù)庫的全局名稱: create public database link ORACLSH using ’oracle.shanghai. com’;Using子句后跟的是全局?jǐn)?shù)據(jù)庫名或者是連接字符串。 create public database link ORACLSH using ’(description= (address=(protocol=tcp)(host=127.0.0.1)(port=1521)) (connect_data=(service_name=oracl)))’4.建立清除延遲事務(wù)隊列調(diào)度作業(yè): disconnect; connect mvadmin/mvadmin@BJ; begin dbms_defer_sys.schedule_purge( next_date => sysdate, interval => ’/*1:hr*/ sysdate + 1’, delay_seconds => 0, rollback_segment => ’’); end;5.建立復(fù)制治理員mvadmin的數(shù)據(jù)庫連接: create database link ORACLSH connect to proxy_bjoracle identified by proxy_bjoralce Connect to ... Identified by ...子句指明用什么用戶連接遠程數(shù)據(jù)庫6.建立復(fù)制調(diào)度數(shù)據(jù)庫連接作業(yè): begin dbms_defer_sys.schedule_push( destination => ’ora92zjk’,interval => ’/*1:hr*/ sysdate + 1’, next_date => sysdate,stop_on_error => false, delay_seconds => 0,parallelism => 0); end; /7.授予SHORACL用戶(對應(yīng)SHORACL方案)相應(yīng)的權(quán)限建立實體化視圖: disconnect; connect system/passwd@BJ; grant alter session to crm; grant create cluster to crm; grant create database link to crm; grant create sequence to crm; grant create session to crm; grant create synonym to crm; grant create table to crm; grant create view to crm; grant create procedure to crm; grant create trigger to crm; grant unlimited tablespace to crm; grant create type to crm; grant create any snapshot to crm; grant alter any snapshot to crm;8.建立復(fù)制方案的數(shù)據(jù)庫連接: disconnect; connect SHORACL/SHORACL@BJ; create database link ORACLSH connect to ORACL identified by ORACL;復(fù)制方案的數(shù)據(jù)庫連接和復(fù)制治理員的數(shù)據(jù)庫連接要和system用戶間里的對應(yīng)公共數(shù)據(jù)庫連接使用相同的名字,在調(diào)度連接時將使用公共數(shù)據(jù)庫連接中指定的數(shù)據(jù)庫全局名或者連接字符串。 9.建立實體化視圖: disconnect; connect mvadmin/mvadmin@BJ; create materialized view SHORACL.CREDIT_CARD refresh fast wit h pr imar y key as sele ct * from ORA CL. CREDIT_CARD@ORACLSH;@后面是數(shù)據(jù)庫連接名。假如該表沒有主鍵則使用rowid來刷新。 create materialized view SHORACL. CREDIT_CARD refresh fast with rowid as select * from ORACL. CREDIT_CARD@ORACLSH;10.為多個視圖建立刷新組: begin dbms_refresh.make ( name => ’mvadmin.sh_refresh’,list => ’’, next_date => sysdate,interval => ’sysdate + 1’, implicit_destroy => false,rollback_seg => ’’, push_deferred_rpc => true,refresh_after_errors => false); end;11.向刷新組中添加復(fù)制對象: begin dbms_refresh.add (name => ’mvadmin.sh_refresh’,list => ’SHORACL.CREDIT_CARD ’,lax => true); end; /三.檢查復(fù)制進程: 1.查看sys.dba_jobs視圖是否生成了足夠的作業(yè)。 經(jīng)過以上的步驟應(yīng)該有三個作業(yè)分別是清除作業(yè)、調(diào)度作業(yè)和刷新作業(yè),查看視圖的what字段是否有下面的內(nèi)容: a) declare rc binary_integer; begin rc := sys.dbms_defer_sys. purge( delay_seconds=>0); end; b) declare rc binary_integer; begin rc := sys.dbms_defer_sys. push(destination=>’ORACLSH’, stop_on_error=>FALSE, delay_seconds=>0, parallelism=>0); end; c) dbms_refresh.refresh(’'MVADMIN'.'SH_REFRESH'’);假如排除其它系統(tǒng)作業(yè)本例中杭州和武漢的數(shù)據(jù)庫復(fù)制建立之后將會有7個作業(yè)(假如為每個復(fù)制分別建立刷新組的話),清除作業(yè)始終只有一個。每個復(fù)制對應(yīng)一個調(diào)度作業(yè),每個刷新組對應(yīng)一個刷新作業(yè)。 2.查看job_queue_processes參數(shù),確保該參數(shù)不為零(數(shù)據(jù)庫的默認值是零),假如該參數(shù)為零,除非每次手工執(zhí)行刷新,否則系統(tǒng)不會自動刷新復(fù)制數(shù)據(jù)。 3.確保復(fù)制執(zhí)行之后,觀察sys.dba_jobs視圖的failures字段。假如復(fù)制在刷新過程中除錯,Oracle會自動在1分鐘之后再次嘗試刷新,失敗之后再在2分鐘、4分鐘、8分鐘..之后嘗試刷新,直到失敗次數(shù)達到16次或者間隔時間超過作業(yè)設(shè)置的間隔時間,該作業(yè)將被標(biāo)記為中斷,Oracle不再執(zhí)行該作業(yè)。要重新執(zhí)行改作業(yè)使用dbms_job包的run過程: begin dbms_job.run(job_no); end; /job_no 是sys.dba_jobs 的Job字段的值,作業(yè)號。在重新執(zhí)行因出錯而中斷的作業(yè)前,需要手工找到出錯點,并更正。 總結(jié) 本文只是使用了Oracle高級復(fù)制中最簡單的功能,Oracle的高級復(fù)制還提供可更新視圖和復(fù)雜的只讀實體化視圖復(fù)制,當(dāng)然并不是我們都要去用高級復(fù)雜強大的功能,在具體應(yīng)用的時候,還要根據(jù)系統(tǒng)功能和性能需求,選擇適當(dāng)?shù)膹?fù)制技術(shù)。
標(biāo)簽:
Oracle
數(shù)據(jù)庫
排行榜
