在Oracle中實作資料庫的複制
在Internet上運作資料庫經常會有這樣的需求:把遍布全國各城市相似的資料庫應用統一起來,一個節點的資料改變不僅展現在本地,還反映到遠端。複制技術給使用者提供了一種快速通路共享資料的辦法。
一、實作資料庫複制的前提條件
1、資料庫支援進階複制功能
您可以用system身份登入資料庫,檢視v$option視圖,如果其中Advanced replication為TRUE,則支援進階複制功能;否則不支援。
2、資料庫初始化參數要求
①、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上我們有兩個資料庫:一個叫深圳(shenzhen),一個叫北京(beijing)。
具體配置見下表:
資料庫名 shenzhen beijing
資料庫域名 test.com.cn test.com.cn
資料庫sid号 shenzhen beijing
Listener端口号 1521 1521
伺服器ip位址 10.1.1.100 10.1.1.200
1、确認兩台資料庫之間可以互相通路,在tnsnames.ora裡設定資料庫連接配接字元串。
①、例如:深圳這邊的資料庫連接配接字元串是以下的格式
beijing =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.1.200)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = beijing)
運作$tnsping beijing
出現以下提示符:
Attempting to contact (ADDRESS=(PROTOCOL=TCP)(HOST=10.1.1.200)(PORT=1521))
OK(n毫秒)
表明深圳資料庫可以通路北京資料庫。
②、在北京那邊也同樣配置,确認$tnsping shenzhen 是通的。
2、改資料庫全局名稱,建公共的資料庫連結。
①、用system身份登入shenzhen資料庫
SQL>alter database rename global_name to shenzhen.test.com.cn;
用system身份登入beijing資料庫:
SQL>alter database rename global_name to beijing.test.com.cn;
②、用system身份登入shenzhen資料庫
SQL>create public database link beijing.test.com.cn using 'beijing';
測試資料庫全局名稱和公共的資料庫連結
傳回結果為beijing.test.com.cn就對了。
SQL>create public database link shenzhen.test.com.cn using 'shenzhen';
傳回結果為shenzhen.test.com.cn就對了。
3、建立管理資料庫複制的使用者repadmin,并賦權。
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資料庫,運作以上的指令,管理資料庫複制的使用者repadmin,并賦權。
說明:repadmin使用者名和密碼可以根據使用者的需求自由命名。
4、在資料庫複制的使用者repadmin下建立私有的資料庫連結。
①、用repadmin身份登入shenzhen資料庫
SQL>create database link beijing.test.com.cn connect to repadmin identified by repadmin;
測試這個私有的資料庫連結:
②、用repadmin身份登入beijing資料庫
SQL>create database link shenzhen.test.com.cn connect to repadmin identified by repadmin;
測試這個私有的資料庫連結
5、建立或選擇實作資料庫複制的使用者和對象,給使用者賦權,資料庫對象必須有主關鍵字。
假設我們用ORACLE裡舉例用的scott使用者,dept表。
①、用internal身份登入shenzhen資料庫,建立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身份登入shenzhen資料庫,建立表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));
④、在shenzhen資料庫scott使用者下建立主關鍵字的序列号,範圍避免和beijing的沖突。
SQL> create sequence dept_no increment by 1 start with 1 maxvalue 44 cycle nocache;
(說明:maxvalue 44可以根據應用程式及表結構主關鍵字定義的位數需要而定)
⑤、在shenzhen資料庫scott使用者下插入初始化資料
SQL>insert into dept values (dept_no.nextval,'accounting','new york');
SQL>insert into dept values (dept_no.nextval,'research','dallas');
SQL>commit;
⑥、在beijing資料庫那邊同樣運作以上①,②,③
⑦、在beijing資料庫scott使用者下建立主關鍵字的序列号,範圍避免和shenzhen的沖突。
SQL> create sequence dept_no increment by 1 start with 45 maxvalue 99 cycle nocache;
⑧、在beijing資料庫scott使用者下插入初始化資料
SQL>insert into dept values (dept_no.nextval,'sales','chicago');
SQL>insert into dept values (dept_no.nextval,'operations','boston');
6、建立要複制的組scott_mg,加入資料庫對象,産生對象的複制支援
①、用repadmin身份登入shenzhen資料庫,建立主複制組scott_mg
SQL> execute dbms_repcat.create_master_repgroup('scott_mg');
說明: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 實作資料庫複制的資料庫對象名稱
(表名長度在27個位元組内,程式包名長度在24個位元組内)
type 實作資料庫複制的資料庫對象類别
(支援的類别:表,索引,同義詞,觸發器,視圖,過程,函數,程式包,程式包體)
use_existing_object true表示用主複制節點已經存在的資料庫對象
gname 主複制組名
③、對資料庫對象産生複制支援
SQL>execute dbms_repcat.generate_replication_support('scott','dept','table');
(說明:産生支援scott使用者下dept表複制的資料庫觸發器和程式包)
④、确認複制的組和對象已經加入資料庫的資料字典
SQL>select gname, master, status from dba_repgroup;
SQL>select * from dba_repobject;
7、建立主複制節點
①、用repadmin身份登入shenzhen資料庫,建立主複制節點
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');
master 加入主複制節點的另一個資料庫
copy_rows false表示第一次開始複制時不用和主複制節點保持一緻
propagation_mode 異步地執行
②、确認複制的任務隊列已經加入資料庫的資料字典
SQL>select * from user_jobs;
8、使同步組的狀态由停頓(quiesced )改為正常(normal)
①、用repadmin身份登入shenzhen資料庫,運作以下指令
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);
9、建立複制資料庫的時間表,我們假設用固定的時間表:10分鐘複制一次。
SQL>begin
dbms_defer_sys.schedule_push (
destination => 'beijing.test.com.cn',
interval => 'sysdate + 10/1440',
next_date => sysdate);
end;
/
dbms_defer_sys.schedule_purge (
next_date => sysdate,
delay_seconds => 0,
rollback_segment => '');
②、用repadmin身份登入beijing資料庫,運作以下指令
destination => ' shenzhen.test.com.cn ',
interval => 'sysdate + 10 / 1440',
10、添加或修改兩邊資料庫的記錄,跟蹤複制過程
如果你想立刻看到添加或修改後資料庫的記錄的變化,可以在兩邊repadmin使用者下找到push的job_number,然後運作:
SQL>exec dbms_job.run(job_number);
三、異常情況的處理
1、檢查複制工作正常否,可以在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>操作符下,運作指令:
說明:job_number 為用select job,this_date,next_date,what from user_jobs;指令查出的job編号。
2、增加或減少複制組的複制對象
①、停止主資料庫節點的複制動作,使同步組的狀态由正常(normal)改為停頓(quiesced )
用repadmin身份登入shenzhen資料庫,運作以下指令
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)。