淺析Oracle數(shù)據(jù)庫(kù)集中方法(1)

字號(hào):


    Oracle數(shù)據(jù)復(fù)制是實(shí)現(xiàn)分布式數(shù)據(jù)環(huán)境的一種技術(shù),通過(guò)在不同的物理站點(diǎn)拷貝數(shù)據(jù)來(lái)建立分布式數(shù)據(jù)環(huán)境。它與分布式數(shù)據(jù)庫(kù)不同,在分布式數(shù)據(jù)庫(kù)中,雖然每個(gè)數(shù)據(jù)對(duì)象也對(duì)所有的站點(diǎn)可用,但是特定的數(shù)據(jù)對(duì)象只存在于一個(gè)特定的站點(diǎn)中。而數(shù)據(jù)復(fù)制實(shí)現(xiàn)所有的站點(diǎn)都有相同數(shù)據(jù)對(duì)象的可用拷貝。
    在一個(gè)典型的分布式商業(yè)應(yīng)用中經(jīng)常需要把個(gè)地區(qū)的數(shù)據(jù)備份到總部的數(shù)據(jù)庫(kù)中,一方面可以作為一種備份方式,另一方面也方便總部應(yīng)用中的綜合統(tǒng)計(jì)。這是Oracle數(shù)據(jù)復(fù)制中的簡(jiǎn)單應(yīng)用,本文將以這樣一個(gè)例子,講述如何實(shí)現(xiàn)Oracle數(shù)據(jù)復(fù)制。
    實(shí)際情況是,A公司總部在北京,有三個(gè)營(yíng)業(yè)部分別位于上海(ORACLE.SHANGHAI.COM)、杭州(ORACLE.HANGZHOU.COM)和武漢(ORACLE.
    WUHAN.COM)。三個(gè)營(yíng)業(yè)部的軟件系統(tǒng)相同,數(shù)據(jù)庫(kù)結(jié)構(gòu)也相同。現(xiàn)在需要把三個(gè)營(yíng)業(yè)部的數(shù)據(jù)全部備份到總部的數(shù)據(jù)庫(kù)中。
    準(zhǔn)備工作
    在進(jìn)行復(fù)制之前需要準(zhǔn)備的東西很多,當(dāng)然最基礎(chǔ)就是網(wǎng)絡(luò)必須暢通,之后需要收集一些復(fù)制環(huán)境的基本信息:
    1.需要復(fù)制的數(shù)據(jù)庫(kù)站點(diǎn)的數(shù)量
    2.每個(gè)站點(diǎn)的Oracle版本號(hào)
    3.每個(gè)需要復(fù)制的數(shù)據(jù)庫(kù)的大小
    4.每個(gè)數(shù)據(jù)庫(kù)所使用的字符集
    5.每個(gè)需要復(fù)制的數(shù)據(jù)所用的方案名
    收集完環(huán)境信息,可以開始建立總部的集中數(shù)據(jù)庫(kù),集中數(shù)據(jù)庫(kù)要求版本高于所有主戰(zhàn)點(diǎn)的版本,最好所有的數(shù)據(jù)庫(kù)都是用相同的字符集。建好庫(kù)后為每個(gè)主站點(diǎn)的備份數(shù)據(jù)分別建一個(gè)表空間,表空間大于需要復(fù)制的數(shù)據(jù)量,至于預(yù)留以后的發(fā)展空間視實(shí)際情況而定。
    為每個(gè)主站點(diǎn)的對(duì)應(yīng)復(fù)制數(shù)據(jù)建立方案,如果各個(gè)主站點(diǎn)所使用的方案名不同,在集中數(shù)據(jù)庫(kù)站點(diǎn)分別建立名稱相同的對(duì)應(yīng)方案。否則為各主站點(diǎn)的復(fù)制數(shù)據(jù)分別建立相應(yīng)的方案名。實(shí)際情況是后者,各營(yíng)業(yè)部的數(shù)據(jù)庫(kù)都是用Oracle的方案名,這里我們建立三個(gè)對(duì)應(yīng)方
    案:SHORACL、HZORACL和WHORACL。所有數(shù)據(jù)庫(kù)的版本都是9i。
    基本概念
    復(fù)制之前先解釋一下復(fù)制中的幾個(gè)概念:
    1.主站點(diǎn)(MaterSite):在復(fù)制過(guò)程中提供數(shù)據(jù)源的站點(diǎn)。如上圖中的上海數(shù)據(jù)庫(kù)站點(diǎn)。
    2.實(shí)體化視圖站點(diǎn)(MaterializedViewSite):實(shí)體化視圖復(fù)制中的目標(biāo)站點(diǎn)。如上圖中的北京數(shù)據(jù)庫(kù)站點(diǎn)。
    3.多主體站點(diǎn)復(fù)制(MultimasterReplication):復(fù)制環(huán)境中的站點(diǎn)都是主站點(diǎn),對(duì)復(fù)制的數(shù)據(jù)庫(kù)對(duì)象有相同的管理權(quán)限。
    4.實(shí)體化視圖復(fù)制(MaterializedViewReplication):一個(gè)主體站點(diǎn)提供源復(fù)制對(duì)象,一個(gè)實(shí)體化視圖站點(diǎn)拷貝主站點(diǎn)數(shù)據(jù)。
    5.實(shí)體化視圖(MaterializedView):在實(shí)體化視圖站點(diǎn)為每個(gè)復(fù)制表或者視圖建立一個(gè)對(duì)應(yīng)的表保存相應(yīng)的數(shù)據(jù),該表只能通過(guò)Oracle的復(fù)制機(jī)制進(jìn)行增刪改數(shù)據(jù)的操作。
    6.快速刷新、完全刷新和強(qiáng)制刷新:復(fù)制過(guò)程中的三種刷新方式??焖偎⑿轮粡?fù)制源數(shù)據(jù)對(duì)象的改變部分;完全刷新每次都拷貝一遍源數(shù)據(jù)對(duì)象;強(qiáng)制刷新是數(shù)據(jù)庫(kù)的一個(gè)折衷方案,如果快速刷新失敗則使用完全刷新。
    7.主體組(MasterGroup):主體站點(diǎn)中被復(fù)制的源數(shù)據(jù)對(duì)象的集合。
    8.實(shí)體化視圖組(MaterializedViewSite):實(shí)體化視圖站點(diǎn)中復(fù)制對(duì)象的集合。
    9.實(shí)體化視圖日志(MaterializedViewLog):實(shí)體化視圖復(fù)制中使用快速刷新時(shí)記錄主體源數(shù)據(jù)對(duì)象操作日志的表。
    同步復(fù)制和異步復(fù)制就不解釋了,本例采用每天一次的異步復(fù)制。
    進(jìn)行復(fù)制
    配置好本地服務(wù)名分別為:上海站點(diǎn):SH,杭州站點(diǎn):HZ,武漢站點(diǎn):WH,北京站點(diǎn):BJ,進(jìn)入沒(méi)有登錄的sqlplus,讓我們開始復(fù)制!
    一.設(shè)置主站點(diǎn)。
    這里以上海主站點(diǎn)設(shè)置為例。
    1.連接主站點(diǎn),創(chuàng)建復(fù)制管理員并授予相應(yīng)的權(quán)限,復(fù)制管理員是管理整個(gè)復(fù)制環(huán)境并創(chuàng)建復(fù)制對(duì)象的用戶。只有數(shù)據(jù)管理員可以建立主體組和實(shí)體化視圖組。
    connectsystem/passwd@SH
    createuserrepadminidentifiedbyrepadmin;
    begin
    dbms_repcat_admin.grant_admin_any_schema(
    username=>’repadmin’);
    end;
    /
    grantcommentanytabletoREPADMIN;
    grantlockanytabletoREPADMIN;
    后面的兩個(gè)grant語(yǔ)句使復(fù)制管理員可以為任何表建立實(shí)體化視圖日志。如果想改用戶可以使用視圖管理器,還需要下面的命令:
    grantselectanydictionarytoREPADMIN;
    2.注冊(cè)傳播方,傳播方會(huì)將主體站點(diǎn)的延遲事務(wù)隊(duì)列推入其他主體站點(diǎn)或者實(shí)體化視圖站點(diǎn)。
    begin
    dbms_defer_sys.register_purpagator(username=>’repadmin’);
    end;
    3.調(diào)度清除作業(yè),該作業(yè)會(huì)定時(shí)清除延遲事務(wù)隊(duì)列并用傳播方將延遲事務(wù)推入其他主體站點(diǎn)或者實(shí)體化視圖站點(diǎn)。先更換用戶:
    disconnect;
    connectrepadmin/repadmin@SH;
    begin
    dbms_defer_sys.schedule_purge(
    next_date=>sysdate,interval=>’sysdate+1’,delay_seconds=>0);
    end;
    next_date:下一次執(zhí)行日期,sysdate表示立即。
    interval:間隔時(shí)段,sysdate+1表示間隔一天,sysdate+1/24表示間隔一小時(shí)
    delay_seconds:當(dāng)延遲隊(duì)列沒(méi)有延遲事件時(shí)停止被次清除操作的延遲時(shí)間。
    4.為實(shí)體化視圖站點(diǎn)建立復(fù)制代理。創(chuàng)建復(fù)制代理用戶并授予視圖接受方權(quán)限。復(fù)制代理是復(fù)制接收方連接主體站點(diǎn)的用戶
    disconnect;
    connectsystem/passwd@SH;
    createuserproxy_bjoracleidentifiedbyproxy_bjoracle;
    begin
    dbms_repcat_admin.register_user_repgroup(
    user_name=>’proxy_bjoracle,
    privilege_type=>’proxy_snapadmin’,list_of_gnames=>NULL);
    end;
    /
    grantselect_catalog_roletoproxy_bjoracle;
    5.創(chuàng)建主體組。
    disconnect;
    connectrepadmin/repadmin@SH;
    begin
    dbms_repcat.create_master_repgroup(gname=>’sh_rep’);
    end;
    /
    6.向主體組中添加復(fù)制對(duì)象
    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.如果添加的表沒(méi)有主鍵需要設(shè)置可以代替主鍵的列或者列的集合
    begin
    dbms_repcat.set_columns(
    sname=>’SHORACL’,
    oname=>’CREDIT_CARD’,
    column_list=>’CREDIT_CARD_ID’);
    end;
    /
    8.在主體組中的數(shù)據(jù)對(duì)象可以被復(fù)制之前,必須為他們生成復(fù)制支持。該方法為復(fù)制創(chuàng)建必要的觸發(fā)器、包或者存儲(chǔ)過(guò)程:
    begin
    dbms_repcat.generate_replication_support(
    sname=>’SHORACL’,
    oname=>’CREDIT_CARD’,
    type=>’TABLE’,
    min_communication=>TRUE);
    end;
    /
    9.為快速刷新創(chuàng)建實(shí)體化視圖日志:
    creatematerializedviewlogonSHORACL.CREDIT_CARD;
    如果是沒(méi)有主鍵的表示用一下語(yǔ)句:
    creatematerializedviewlogonSHORACL.CREDIT_CARDwith
    rowidexcludingnewvalues;
    10.啟動(dòng)復(fù)制:
    begin
    dbms_repcat.resume_master_activity(
    name=>’sh_rep’);
    end;
    /
    二.設(shè)置實(shí)體化視圖站點(diǎn)。
    1.創(chuàng)建復(fù)制管理員并授予相應(yīng)的權(quán)限:
    disconnect;
    connectsystem/passwd@BJ;
    createusermvadminidentifiedby
    mvadmin;
    begin
    dbms_repcat_admin.
    grant_admin_any_schema(username=>’mvadmin’);
    end;
    /
    grantcommentanytabletomvadmin;
    grantlockanytabletomvadmin;
    grantselectanydictionarytomvadmin;
    2.注冊(cè)傳播方:
    begin
    dbms_defer_sys.register_propagator(
    username=>’mvadmin’);
    end;
    /
    3.公共數(shù)據(jù)庫(kù)連接。需要每個(gè)復(fù)制需要?jiǎng)?chuàng)建三個(gè)數(shù)據(jù)庫(kù)連接。公共數(shù)據(jù)庫(kù)連接指定數(shù)據(jù)庫(kù)的全局名稱:
    createpublicdatabaselinkORACLSHusing’oracle.shanghai.
    com’;
    Using子句后跟的是全局?jǐn)?shù)據(jù)庫(kù)名或者是連接字符串。
    createpublicdatabaselinkORACLSHusing’(description=
    (address=(protocol=tcp)(host=127.0.0.1)(port=1521))
    (connect_data=(service_name=oracl)))’
    4.建立清除延遲事務(wù)隊(duì)列調(diào)度作業(yè):
    disconnect;
    connectmvadmin/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ù)庫(kù)連接:
    createdatabaselinkORACLSHconnecttoproxy_bjoracle
    identifiedbyproxy_bjoralce
    Connectto...Identifiedby...子句指明用什么用戶連接遠(yuǎn)程數(shù)據(jù)庫(kù)
    6.建立復(fù)制調(diào)度數(shù)據(jù)庫(kù)連接作業(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用戶(對(duì)應(yīng)SHORACL方案)
    相應(yīng)的權(quán)限建立實(shí)體化視圖:
    disconnect;
    connectsystem/passwd@BJ;
    grantaltersessiontocrm;
    grantcreateclustertocrm;
    grantcreatedatabaselinktocrm;
    grantcreatesequencetocrm;
    grantcreatesessiontocrm;
    grantcreatesynonymtocrm;
    grantcreatetabletocrm;
    grantcreateviewtocrm;
    grantcreateproceduretocrm;
    grantcreatetriggertocrm;
    grantunlimitedtablespacetocrm;
    grantcreatetypetocrm;
    grantcreateanysnapshottocrm;
    grantalteranysnapshottocrm;
    8.建立復(fù)制方案的數(shù)據(jù)庫(kù)連接:
    disconnect;
    connectSHORACL/SHORACL@BJ;
    createdatabaselinkORACLSHconnecttoORACLidentified
    byORACL;
    復(fù)制方案的數(shù)據(jù)庫(kù)連接和復(fù)制管理員的數(shù)據(jù)庫(kù)連接要和system用戶間里的對(duì)應(yīng)公共數(shù)據(jù)庫(kù)連接使用相同的名字,在調(diào)度連接時(shí)將使用公共數(shù)據(jù)庫(kù)連接中指定的數(shù)據(jù)庫(kù)全局名或者連接字符串。
    9.建立實(shí)體化視圖:
    disconnect;
    connectmvadmin/mvadmin@BJ;
    creatematerializedviewSHORACL.CREDIT_CARDrefreshfast
    withprimarykeyasselect*fromORACL.
    CREDIT_CARD@ORACLSH;
    @后面是數(shù)據(jù)庫(kù)連接名。如果該表沒(méi)有主鍵則使用rowid來(lái)刷新
    creatematerializedviewSHORACL.CREDIT_CARDrefresh
    fastwithrowidasselect*fromORACL.CREDIT_CARD@ORACLSH;
    10.為多個(gè)視圖建立刷新組:
    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;