oracle高階複製的詳細手冊(轉)

heying1229發表於2007-07-18

實現Oracle資料庫複製(一)
--------------------------------------------------------------------------------


??我們經常希望把各地的資料入庫後進行統一的應用。現在可以用複製技術來解決這個問題。但實現資料庫複製也是要有一些條件的。

??首先,資料庫要具備高階複製功能(用system身份登入資料庫,檢視v$option檢視,如果其中Advanced replication為TRUE,則支援高階複製功能;否則不支援)。

??如果具備高階複製功能,資料庫要進行一些引數初始化。

??db_domain = test.com.cn 指明資料庫的域名(預設的是WORLD),這裡可以用您公司的域名;global_names = true 它要求資料庫連結(database link)和被連線的資料庫名稱一致,現在全域性資料庫名:db_name+”.”+db_domain ;

??跟資料庫job執行有關的引數:
job_queue_processes = 1;
job_queue_interval = 60;
distributed_transactions = 10;
open_links = 4

??第一行定義SNP程式的啟動個數為n。系統預設值為0,正常定義範圍為0~36,根據任務的多少,可以配置不同的數值。第二行定義系統每隔N秒喚醒該程式一次。系統預設值為60秒,正常範圍為1~3600秒。事實上,該程式執行完當前任務後,就進入睡眠狀態,睡眠一段時間後,由系統的總控負責將其喚醒。如果修改了以上這幾個引數,需要重新啟動資料庫以使引數生效。

??做完了初步的準備,我們來實現資料庫同步複製。

??假設在Internet上有兩個資料庫:一個叫中國(China),一個叫日本(Japan)。
??具體配置如下:
??資料庫名:China、Japan
??資料庫域名 test.com.cn
??資料庫sid號 China、Japan
??Listener埠號 1521
??伺服器ip地址 10.1.0.100 10.1.0.200
??確認兩個資料庫之間可以互相訪問,在tnsnames.ora裡設定資料庫連線字串。
??中國這邊的資料庫連線字串是以下的格式:
Japan =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.1.200)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = Japan)
)
)
??執行$tnsping Japan,出現以下提示符:
??Attempting to contact (ADDRESS=(PROTOCOL=TCP)(HOST=10.1.1.200)(PORT=1521))
??OK(n毫秒)
??表明中國資料庫可以訪問日本資料庫。在日本那邊也同樣配置,確認$tnsping China 是通的。
??改資料庫全域性名稱,建公共的資料庫連結。
用system身份登入China資料庫
SQL>alter database rename global_name to China.test.com.cn;
用system身份登入Japan資料庫:
SQL>alter database rename global_name to Japan.test.com.cn;
用system身份登入China資料庫。
SQL>create public database link Japan.test.com.cn using 'Japan';
測試資料庫全域性名稱和公共的資料庫連結。
SQL>select * from ;
返回結果為Japan.test.com.cn就對了。
用system身份登入Japan資料庫:
SQL>create public database link China.test.com.cn using 'China';
測試資料庫全域性名稱和公共的資料庫連結。
SQL>select * from ;
返回結果為China.test.com.cn就對了。
建立管理資料庫複製的使用者repadmin,並賦權。
用system身份登入China資料庫:
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身份登入Japan資料庫,執行以上的命令,管理資料庫複製的使用者repadmin,並賦權。
在資料庫複製的使用者repadmin下建立私有的資料庫連結。
用repadmin身份登入China資料庫。
SQL>create database link Japan.test.com.cn connect to repadmin identified 試這個私有的資料庫連結:
SQL>select * from ;
返回結果為Japan.test.com.cn就對了。
用repadmin身份登入Japan資料庫。
SQL>create database link China.test.com.cn connect to repadmin identified by repadmin;
測試這個私有的資料庫連結:
SQL>select * from ;
返回結果為China.test.com.cn就對了。
建立或選擇實現資料庫複製的使用者和物件,給使用者賦權,資料庫物件必須有主關鍵字。
用internal身份登入China資料庫,建立scott使用者並賦權:
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身份登入China資料庫,建立表dept :
SQL>create table dept
(deptno number(2) primary key,
dname varchar2(14),
loc varchar2(13) );
如果資料庫物件沒有主關鍵字,可以執行以下SQL命令新增:
SQL>alter table dept add (constraint dept_deptno_pk primary key (deptno));
在China資料庫scott使用者下建立主關鍵字的序列號,範圍避免和Japan的衝突。
SQL> create sequence dept_no increment by 1 start with 1 maxvalue 44 cycle nocache;
在China資料庫scott使用者下插入初始化資料
SQL>insert into dept values (dept_no.nextval,'accounting','new york');
SQL>insert into dept values (dept_no.nextval,'research','dallas');
SQL>commit;
在Japan資料庫那邊同樣執行以上①,②,③。
在Japan資料庫scott使用者下建立主關鍵字的序列號,範圍避免和China的衝突。
SQL> create sequence dept_no increment by 1 start with 45 maxvalue 99 cycle nocache;
在Japan資料庫scott使用者下插入初始化資料。
SQL>insert into dept values (dept_no.nextval,'sales','chicago');
SQL>insert into dept values (dept_no.nextval,'operations','boston');
SQL>commit;

實現Oracle資料庫複製(二)
--------------------------------------------------------------------------------

在Japan資料庫那邊同樣執行以上①,②,③。

??在Japan資料庫scott使用者下建立主關鍵字的序列號,範圍避免和China的衝突。

??SQL> create sequence dept_no increment by 1 start with 45 maxvalue 99 cycle nocache;

??在Japan資料庫scott使用者下插入初始化資料。

??SQL>insert into dept values (dept_no.nextval,'sales','chicago');
??SQL>insert into dept values (dept_no.nextval,'operations','boston');

??SQL>commit;

??建立要複製的組scott_mg,加入資料庫物件,產生物件的複製支援。

??用repadmin身份登入China資料庫,建立主複製組scott_mg:
??SQL> execute dbms_repcat.create_master_repgroup('scott_mg');

??在複製組scott_mg里加入資料庫物件:

??SQL>execute dbms_repcat.create_master_repobject(sname=>'scott',oname=>'dept', type=>'table',use_existing_object=>true,gname=>'scott_mg');

??引數說明:

??sname 實現資料庫複製的使用者名稱稱;
??oname 實現資料庫複製的資料庫物件名稱;
??type 實現資料庫複製的資料庫物件類別;
??use_existing_object true表示用主複製節點已經存在的資料庫物件;
??gname 主複製組名;

??對資料庫物件產生複製支援:
??SQL>execute dbms_repcat.generate_replication_support('scott','dept','table');

??確認複製的組和物件已經加入資料庫的資料字典:
??SQL>select gname, master, status from dba_repgroup;
??SQL>select * from dba_repobject;

??建立主複製節點:

??用repadmin身份登入China資料庫,建立主複製節點:
??SQL>execute dbms_repcat.add_master_database
??(gname=>'scott_mg',master=>'Japan.test.com.cn',use_existing_objects=>true, copy_rows=>false, propagation_mode => 'asynchronous');

??引數說明:

??gname 主複製組名;
??master 加入主複製節點的另一個資料庫;
??use_existing_object true表示用主複製節點已經存在的資料庫物件;
??copy_rows false表示第一次開始複製時不用和主複製節點保持一致;
??propagation_mode 非同步地執行;

??確認複製的任務佇列已經加入資料庫的資料字典:
??SQL>select * from user_jobs;
??使同步組的狀態由停頓(quiesced )改為正常(normal):

??用repaa資料庫,執行以下命令:
??SQL> execute dbms_repcat.resume_master_activity('scott_mg',false);

??確認同步組的狀態為正常(normal):
??SQL> select gname, master, status from dba_repgroup;

??如果這個①命令不能使同步組的狀態為正常(normal),可能有一些停頓的複製,執行以下命令再試試(建議在緊急的時候才用):
??SQL> execute dbms_repcat.resume_master_activity('scott_mg',true);

??建立複製資料庫的時間表,10分鐘複製一次。

??用repadmin身份登入China資料庫,執行以下命令:
SQL>begin
dbms_defer_sys.schedule_push (
destination => 'Japan.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身份登入Japan資料庫,執行以下命令:
SQL>begin
dbms_defer_sys.schedule_push (
destination => ' China.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使用者下找到push的job_number,然後執行:

??SQL>exec dbms_job.run(job_number);

??異常情況的處理:

??檢查複製工作正常否,可以在repadmin 使用者下查詢user_jobs
??SQL>select job,this_date,next_date,what, broken from user_jobs;

??正常的狀態有兩種:任務閒——this_date為空,next_date為當前時間後的一個時間值;任務忙——this_date不為空,next_date為當前時間後的一個時間值。異常狀態也有兩種:任務死鎖——next_date為當前時間前的一個時間值;任務死鎖——next_date為非常大的一個時間值,例如:4001-01-01。這可能因為網路中斷照成的死鎖。解除死鎖的辦法:

??$ps –ef|grep orale

??找到死鎖的重新整理快照的程式號ora_snp*,用kill –9 命令刪除此程式,然後進入repadmin 使用者SQL>運算子下,執行命令:

??SQL>exec dbms_job.run(job_number);
??說明:job_number 為用select job,this_date,next_date,what from user_jobs;命令查出的job編號。

??增加或減少複製組的複製物件:

??停止主資料庫節點的複製動作,使同步組的狀態由正常(normal)改為停頓(quiesced )。用repadmin身份登入China資料庫,執行以下命令:

??SQL>execute dbms_repcat.suspend_master_activity (gname => 'scott_mg');
??在複製組scott_mg里加入資料庫物件,保證資料庫物件必須有主關鍵字。

??SQL>execute dbms_repcat.create_master_repobject(sname=>'scott',oname=>'emp', ??type=>'table',use_existing_object=>true,gname=>'scott_mg');

??對加入的資料庫物件產生複製支援:
??SQL>execute dbms_repcat.generate_replication_support('scott','emp','table');

??在複製組scott_mg裡刪除資料庫物件:
??SQL>execute dbms_repcat.drop_master_repobject ('scott','dept','table');
??重新使同步組的狀態由停頓(quiesced )改為正常(normal)。

??SQL> execute dbms_repcat.resume_master_activity('scott_mg',false);

[@more@]

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

相關文章