想飛就別怕摔

          大爺?shù)牟M罵人

          [轉(zhuǎn)]oracle數(shù)據(jù)庫(kù)同步技術(shù)

          oracle數(shù)據(jù)庫(kù)同步技術(shù)

          高級(jí)復(fù)制
          什么是復(fù)制?簡(jiǎn)單地說(shuō)復(fù)制就是在由兩個(gè)或者多個(gè)數(shù)據(jù)庫(kù)系統(tǒng)構(gòu)成的一個(gè)分布式數(shù)據(jù)庫(kù)環(huán)境中拷貝數(shù)據(jù)的過(guò)程。
                 高級(jí)復(fù)制,是在組成分布式數(shù)據(jù)庫(kù)系統(tǒng)的多個(gè)數(shù)據(jù)庫(kù)中復(fù)制和維護(hù)數(shù)據(jù)庫(kù)對(duì)象的過(guò)程。 Oracle 高級(jí)復(fù)制允許應(yīng)用程序更新數(shù)據(jù)庫(kù)的任何副本,并將這些更改自動(dòng)傳遞到其他數(shù)據(jù)庫(kù),同時(shí)確保全局事務(wù)處理的一致性和數(shù)據(jù)完整性。
                 同步復(fù)制,復(fù)制數(shù)據(jù)在任何時(shí)間在任何復(fù)制節(jié)點(diǎn)均保持一致。如果復(fù)制環(huán)境中的任何一個(gè)節(jié)點(diǎn)的復(fù)制數(shù)據(jù)發(fā)生了更新操作,這種變化會(huì)立刻反映到其他所有的復(fù)制節(jié)點(diǎn)。這種技術(shù)適用于那些對(duì)于實(shí)時(shí)性要求較高的商業(yè)應(yīng)用中。
                 異步復(fù)制,所有復(fù)制節(jié)點(diǎn)的數(shù)據(jù)在一定時(shí)間內(nèi)是不同步的。如果復(fù)制環(huán)境中的其中的一個(gè)節(jié)點(diǎn)的復(fù)制數(shù)據(jù)發(fā)生了更新操作,這種改變將在不同的事務(wù)中被傳播和應(yīng)用到其他所有復(fù)制節(jié)點(diǎn)。這些不同的事務(wù)間可以間隔幾秒,幾分種,幾小時(shí),也可以是幾天之后。復(fù)制節(jié)點(diǎn)之間的數(shù)據(jù)臨時(shí)是不同步的,但傳播最終將保證所有復(fù)制節(jié)點(diǎn)間的數(shù)據(jù)一致。
          一、實(shí)現(xiàn)數(shù)據(jù)庫(kù)復(fù)制的前提條件  
          1、數(shù)據(jù)庫(kù)支持高級(jí)復(fù)制功能  
          您可以用system身份登錄數(shù)據(jù)庫(kù),查看v$option視圖,如果其中Advanced replication為TRUE,則支持高級(jí)復(fù)制功能;否則不支持。  
          2、數(shù)據(jù)庫(kù)初始化參數(shù)要求  
          ①、db_domain = test.com.cn  
          指明數(shù)據(jù)庫(kù)的域名(默認(rèn)的是WORLD),這里可以用您公司的域名。  
          ②、global_names = true  
          它要求數(shù)據(jù)庫(kù)鏈接(database link)和被連接的數(shù)據(jù)庫(kù)名稱一致。  
          現(xiàn)在全局?jǐn)?shù)據(jù)庫(kù)名:db_name+”.”+db_domain  
          ③、有跟數(shù)據(jù)庫(kù)job執(zhí)行有關(guān)的參數(shù)  
          job_queue_processes = 1  
          job_queue_interval = 60  
          distributed_transactions = 10  
          open_links = 4  
          第一行定義SNP進(jìn)程的啟動(dòng)個(gè)數(shù)為n。系統(tǒng)缺省值為0,正常定義范圍為0~36,根據(jù)任務(wù)的多少,可以配置不同的數(shù)值。  
          第二行定義系統(tǒng)每隔N秒喚醒該進(jìn)程一次。系統(tǒng)缺省值為60秒,正常范圍為1~3600秒。事實(shí)上,該進(jìn)程執(zhí)行完當(dāng)前任務(wù)后,就進(jìn)入睡眠狀態(tài),睡眠一段時(shí)間后,由系統(tǒng)的總控負(fù)責(zé)將其喚醒。  
          如果修改了以上這幾個(gè)參數(shù),需要重新啟動(dòng)數(shù)據(jù)庫(kù)以使參數(shù)生效。  
          二、實(shí)現(xiàn)數(shù)據(jù)庫(kù)同步復(fù)制的步驟  
          假設(shè)在Internet上我們有兩個(gè)數(shù)據(jù)庫(kù):一個(gè)叫深圳(shenzhen),一個(gè)叫北京(beijing)。  
          具體配置見(jiàn)下表:  
          數(shù)據(jù)庫(kù)名 shenzhen beijing  
          數(shù)據(jù)庫(kù)域名 test.com.cn test.com.cn  
          數(shù)據(jù)庫(kù)sid號(hào) shenzhen beijing  
          Listener端口號(hào) 1521 1521  
          服務(wù)器ip地址 10.1.1.100 10.1.1.200  
            
          1、確認(rèn)兩臺(tái)數(shù)據(jù)庫(kù)之間可以互相訪問(wèn),在tnsnames.ora里設(shè)置數(shù)據(jù)庫(kù)連接字符串。  
          ①、例如:深圳這邊的數(shù)據(jù)庫(kù)連接字符串是以下的格式  
          beijing =  
          (DESCRIPTION =  
          (ADDRESS_LIST =  
          (ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.1.200)(PORT = 1521))  
          )  
          (CONNECT_DATA =  
          (SERVICE_NAME = beijing)  
          )  
          )  
          運(yùn)行$tnsping beijing  
          出現(xiàn)以下提示符:  
          Attempting to contact (ADDRESS=(PROTOCOL=TCP)(HOST=10.1.1.200)(PORT=1521))  
          OK(n毫秒)  
          表明深圳數(shù)據(jù)庫(kù)可以訪問(wèn)北京數(shù)據(jù)庫(kù)。  
          ②、在北京那邊也同樣配置,確認(rèn)$tnsping shenzhen 是通的。  
          2、改數(shù)據(jù)庫(kù)全局名稱,建公共的數(shù)據(jù)庫(kù)鏈接。  
          ①、用system身份登錄shenzhen數(shù)據(jù)庫(kù)  
          SQL>alter database rename global_name to shenzhen.test.com.cn;  
          用system身份登錄beijing數(shù)據(jù)庫(kù):  
          SQL>alter database rename global_name to beijing.test.com.cn;  
          ②、用system身份登錄shenzhen數(shù)據(jù)庫(kù)  
          SQL>create public database link beijing.test.com.cn using 'beijing';  
          測(cè)試數(shù)據(jù)庫(kù)全局名稱和公共的數(shù)據(jù)庫(kù)鏈接  
          SQL>select * from global_name@beijing.test.com.cn;  
          返回結(jié)果為beijing.test.com.cn就對(duì)了。  
          用system身份登錄beijing數(shù)據(jù)庫(kù):  
          SQL>create public database link shenzhen.test.com.cn using 'shenzhen';  
          測(cè)試數(shù)據(jù)庫(kù)全局名稱和公共的數(shù)據(jù)庫(kù)鏈接  
          SQL>select * from global_name@shenzhen.test.com.cn;  
          返回結(jié)果為shenzhen.test.com.cn就對(duì)了。  
          3、建立管理數(shù)據(jù)庫(kù)復(fù)制的用戶repadmin,并賦權(quán)。  
          ①、用system身份登錄shenzhen數(shù)據(jù)庫(kù)  
          SQL>create user repadmin identified by repadmin default tablespace users temporary tablespace temp;  
          SQL>execute dbms_defer_sys.register_propagator('repadmin');  
          SQL>grant execute any procedure to repadmin;  
          SQL>execute dbms_repcat_admin.grant_admin_any_repgroup('repadmin');  
          SQL>grant comment any table to repadmin;  
          SQL>grant lock any table to repadmin;  
          ②、同樣用system身份登錄beijing數(shù)據(jù)庫(kù),運(yùn)行以上的命令,管理數(shù)據(jù)庫(kù)復(fù)制的用戶repadmin,并賦權(quán)。  
          說(shuō)明:repadmin用戶名和密碼可以根據(jù)用戶的需求自由命名。  
          4、在數(shù)據(jù)庫(kù)復(fù)制的用戶repadmin下創(chuàng)建私有的數(shù)據(jù)庫(kù)鏈接。  
          ①、用repadmin身份登錄shenzhen數(shù)據(jù)庫(kù)  
          SQL>create database link beijing.test.com.cn connect to repadmin identified by repadmin;  
          測(cè)試這個(gè)私有的數(shù)據(jù)庫(kù)鏈接:  
          SQL>select * from global_name@beijing.test.com.cn;  
          返回結(jié)果為beijing.test.com.cn就對(duì)了。  
          ②、用repadmin身份登錄beijing數(shù)據(jù)庫(kù)  
          SQL>create database link shenzhen.test.com.cn connect to repadmin identified by repadmin;  
          測(cè)試這個(gè)私有的數(shù)據(jù)庫(kù)鏈接  
          SQL>select * from global_name@shenzhen.test.com.cn;  
          返回結(jié)果為shenzhen.test.com.cn就對(duì)了。  
          5、創(chuàng)建或選擇實(shí)現(xiàn)數(shù)據(jù)庫(kù)復(fù)制的用戶和對(duì)象,給用戶賦權(quán),數(shù)據(jù)庫(kù)對(duì)象必須有主關(guān)鍵字。  
          假設(shè)我們用ORACLE里舉例用的scott用戶,dept表。  
          ①、用internal身份登錄shenzhen數(shù)據(jù)庫(kù),創(chuàng)建scott用戶并賦權(quán)  
          SQL>create user scott identified by tiger default tablespace users temporary tablespace temp;  
          SQL>grant connect, resource to scott;  
          SQL>grant execute on sys.dbms_defer to scott;  
          ②、用scott身份登錄shenzhen數(shù)據(jù)庫(kù),創(chuàng)建表dept  
          SQL>create table dept  
          (deptno number(2) primary key,  
          dname varchar2(14),  
          loc varchar2(13) );  
          ③、如果數(shù)據(jù)庫(kù)對(duì)象沒(méi)有主關(guān)鍵字,可以運(yùn)行以下SQL命令添加:  
          SQL>alter table dept add (constraint dept_deptno_pk primary key (deptno));  
          ④、在shenzhen數(shù)據(jù)庫(kù)scott用戶下創(chuàng)建主關(guān)鍵字的序列號(hào),范圍避免和beijing的沖突。  
          SQL> create sequence dept_no increment by 1 start with 1 maxvalue 44 cycle nocache;  
          (說(shuō)明:maxvalue 44可以根據(jù)應(yīng)用程序及表結(jié)構(gòu)主關(guān)鍵字定義的位數(shù)需要而定)  
          ⑤、在shenzhen數(shù)據(jù)庫(kù)scott用戶下插入初始化數(shù)據(jù)  
          SQL>insert into dept values (dept_no.nextval,'accounting','new york');  
          SQL>insert into dept values (dept_no.nextval,'research','dallas');  
          SQL>commit;  
          ⑥、在beijing數(shù)據(jù)庫(kù)那邊同樣運(yùn)行以上①,②,③  
          ⑦、在beijing數(shù)據(jù)庫(kù)scott用戶下創(chuàng)建主關(guān)鍵字的序列號(hào),范圍避免和shenzhen的沖突。  
          SQL> create sequence dept_no increment by 1 start with 45 maxvalue 99 cycle nocache;  
          ⑧、在beijing數(shù)據(jù)庫(kù)scott用戶下插入初始化數(shù)據(jù)  
          SQL>insert into dept values (dept_no.nextval,'sales','chicago');  
          SQL>insert into dept values (dept_no.nextval,'operations','boston');  
          SQL>commit;  
          6、創(chuàng)建要復(fù)制的組scott_mg,加入數(shù)據(jù)庫(kù)對(duì)象,產(chǎn)生對(duì)象的復(fù)制支持  
          ①、用repadmin身份登錄shenzhen數(shù)據(jù)庫(kù),創(chuàng)建主復(fù)制組scott_mg  
          SQL> execute dbms_repcat.create_master_repgroup('scott_mg');  
          說(shuō)明:scott_mg組名可以根據(jù)用戶的需求自由命名。  
          ②、在復(fù)制組scott_mg里加入數(shù)據(jù)庫(kù)對(duì)象  
          SQL>execute dbms_repcat.create_master_repobject(sname=>'scott',oname=>'dept', type=>'table',use_existing_object=>true,gname=>'scott_mg');  
          參數(shù)說(shuō)明:  
          sname 實(shí)現(xiàn)數(shù)據(jù)庫(kù)復(fù)制的用戶名稱  
          oname 實(shí)現(xiàn)數(shù)據(jù)庫(kù)復(fù)制的數(shù)據(jù)庫(kù)對(duì)象名稱  
          (表名長(zhǎng)度在27個(gè)字節(jié)內(nèi),程序包名長(zhǎng)度在24個(gè)字節(jié)內(nèi))  
          type 實(shí)現(xiàn)數(shù)據(jù)庫(kù)復(fù)制的數(shù)據(jù)庫(kù)對(duì)象類別  
          (支持的類別:表,索引,同義詞,觸發(fā)器,視圖,過(guò)程,函數(shù),程序包,程序包體)  
          use_existing_object true表示用主復(fù)制節(jié)點(diǎn)已經(jīng)存在的數(shù)據(jù)庫(kù)對(duì)象  
          gname 主復(fù)制組名  
          ③、對(duì)數(shù)據(jù)庫(kù)對(duì)象產(chǎn)生復(fù)制支持  
          SQL>execute dbms_repcat.generate_replication_support('scott','dept','table');  
          (說(shuō)明:產(chǎn)生支持scott用戶下dept表復(fù)制的數(shù)據(jù)庫(kù)觸發(fā)器和程序包)  
          ④、確認(rèn)復(fù)制的組和對(duì)象已經(jīng)加入數(shù)據(jù)庫(kù)的數(shù)據(jù)字典  
          SQL>select gname, master, status from dba_repgroup;  
          SQL>select * from dba_repobject;  
          7、創(chuàng)建主復(fù)制節(jié)點(diǎn)  
          ①、用repadmin身份登錄shenzhen數(shù)據(jù)庫(kù),創(chuàng)建主復(fù)制節(jié)點(diǎn)  
          SQL>execute dbms_repcat.add_master_database  
          (gname=>'scott_mg',master=>'beijing.test.com.cn',use_existing_objects=>true, copy_rows=>false, propagation_mode => 'asynchronous');  
          參數(shù)說(shuō)明:  
          gname 主復(fù)制組名  
          master 加入主復(fù)制節(jié)點(diǎn)的另一個(gè)數(shù)據(jù)庫(kù)  
          use_existing_object true表示用主復(fù)制節(jié)點(diǎn)已經(jīng)存在的數(shù)據(jù)庫(kù)對(duì)象  
          copy_rows false表示第一次開(kāi)始復(fù)制時(shí)不用和主復(fù)制節(jié)點(diǎn)保持一致  
          propagation_mode 異步地執(zhí)行  
          ②、確認(rèn)復(fù)制的任務(wù)隊(duì)列已經(jīng)加入數(shù)據(jù)庫(kù)的數(shù)據(jù)字典  
          SQL>select * from user_jobs;  
          8、使同步組的狀態(tài)由停頓(quiesced )改為正常(normal)  
          ①、用repadmin身份登錄shenzhen數(shù)據(jù)庫(kù),運(yùn)行以下命令  
          SQL> execute dbms_repcat.resume_master_activity('scott_mg',false);  
          ②、確認(rèn)同步組的狀態(tài)為正常(normal)  
          SQL> select gname, master, status from dba_repgroup;  
          ③、如果這個(gè)①命令不能使同步組的狀態(tài)為正常(normal),可能有一些停頓的復(fù)制,運(yùn)行以下命令再試試(建議在緊急的時(shí)候才用):  
          SQL> execute dbms_repcat.resume_master_activity('scott_mg',true);  
          9、創(chuàng)建復(fù)制數(shù)據(jù)庫(kù)的時(shí)間表,我們假設(shè)用固定的時(shí)間表:10分鐘復(fù)制一次。  
          ①、用repadmin身份登錄shenzhen數(shù)據(jù)庫(kù),運(yùn)行以下命令  
          SQL>begin  
          dbms_defer_sys.schedule_push (  
          destination => 'beijing.test.com.cn',  
          interval => 'sysdate + 10/1440',  
          next_date => sysdate);  
          end;  
          /  
            
          SQL>begin  
          dbms_defer_sys.schedule_purge (  
          next_date => sysdate,  
          interval => 'sysdate + 10/1440',  
          delay_seconds => 0,  
          rollback_segment => '');  
          end;  
          /  
            
          ②、用repadmin身份登錄beijing數(shù)據(jù)庫(kù),運(yùn)行以下命令  
          SQL>begin  
          dbms_defer_sys.schedule_push (  
          destination => ' shenzhen.test.com.cn ',  
          interval => 'sysdate + 10 / 1440',  
          next_date => sysdate);  
          end;  
          /  
            
          SQL>begin  
          dbms_defer_sys.schedule_purge (  
          next_date => sysdate,  
          interval => 'sysdate + 10/1440',  
          delay_seconds => 0,  
          rollback_segment => '');  
          end;  
          /  
          10、添加或修改兩邊數(shù)據(jù)庫(kù)的記錄,跟蹤復(fù)制過(guò)程  
          如果你想立刻看到添加或修改后數(shù)據(jù)庫(kù)的記錄的變化,可以在兩邊repadmin用戶下找到push的job_number,然后運(yùn)行:  
          SQL>exec dbms_job.run(job_number);  
          三、異常情況的處理  
          1、檢查復(fù)制工作正常否,可以在repadmin 用戶下查詢user_jobs  
          SQL>select job,this_date,next_date,what, broken from user_jobs;  
          正常的狀態(tài)有兩種:  
          任務(wù)閑——this_date為空,next_date為當(dāng)前時(shí)間后的一個(gè)時(shí)間值  
          任務(wù)忙——this_date不為空,next_date為當(dāng)前時(shí)間后的一個(gè)時(shí)間值  
          異常狀態(tài)也有兩種:  
          任務(wù)死鎖——next_date為當(dāng)前時(shí)間前的一個(gè)時(shí)間值  
          任務(wù)死鎖——next_date為非常大的一個(gè)時(shí)間值,例如:4001-01-01  
          這可能因?yàn)榫W(wǎng)絡(luò)中斷照成的死鎖  
          解除死鎖的辦法:  
          $ps –ef|grep orale  
          找到死鎖的刷新快照的進(jìn)程號(hào)ora_snp*,用kill –9 命令刪除此進(jìn)程  
          然后進(jìn)入repadmin 用戶SQL>操作符下,運(yùn)行命令:  
          SQL>exec dbms_job.run(job_number);  
          說(shuō)明:job_number 為用select job,this_date,next_date,what from user_jobs;命令查出的job編號(hào)。  
          2、增加或減少?gòu)?fù)制組的復(fù)制對(duì)象  
          ①、停止主數(shù)據(jù)庫(kù)節(jié)點(diǎn)的復(fù)制動(dòng)作,使同步組的狀態(tài)由正常(normal)改為停頓(quiesced )  
          用repadmin身份登錄shenzhen數(shù)據(jù)庫(kù),運(yùn)行以下命令  
          SQL>execute dbms_repcat.suspend_master_activity (gname => 'scott_mg');  
          ②、在復(fù)制組scott_mg里加入數(shù)據(jù)庫(kù)對(duì)象,保證數(shù)據(jù)庫(kù)對(duì)象必須有主關(guān)鍵字。  
          SQL>execute dbms_repcat.create_master_repobject(sname=>'scott',oname=>'emp', type=>'table',use_existing_object=>true,gname=>'scott_mg');  
          對(duì)加入的數(shù)據(jù)庫(kù)對(duì)象產(chǎn)生復(fù)制支持  
          SQL>execute dbms_repcat.generate_replication_support('scott','emp','table');  
          ③、在復(fù)制組scott_mg里刪除數(shù)據(jù)庫(kù)對(duì)象。  
          SQL>execute dbms_repcat.drop_master_repobject ('scott','dept','table');  
          ④、重新使同步組的狀態(tài)由停頓(quiesced )改為正常(normal)。  
          SQL> execute dbms_repcat.resume_master_activity('scott_mg',false);

          posted on 2009-04-28 08:35 生命的綻放 閱讀(2613) 評(píng)論(0)  編輯  收藏 所屬分類: 數(shù)據(jù)庫(kù)

          <2009年4月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(5)

          隨筆分類(94)

          隨筆檔案(93)

          文章分類(5)

          文章檔案(5)

          相冊(cè)

          JAVA之橋

          SQL之音

          兄弟之窗

          常用工具下載

          積分與排名

          最新評(píng)論

          閱讀排行榜

          主站蜘蛛池模板: 安新县| 云龙县| 驻马店市| 石棉县| 甘洛县| 莱州市| 深州市| 周口市| 佛山市| 墨脱县| 房产| 周至县| 景谷| 仁怀市| 北流市| 三门峡市| 柳江县| 团风县| 卓资县| 星座| 巴塘县| 颍上县| 新宾| 南昌市| 图们市| 思南县| 同江市| 沐川县| 金湖县| 昌宁县| 喀什市| 凤庆县| 昌平区| 滨州市| 水城县| 江达县| 基隆市| 玉屏| 乌鲁木齐市| 达拉特旗| 武强县|