< p >Oracle 11g的流復(fù)制(stream replication)是一種數(shù)據(jù)復(fù)制和同步的技術(shù),它可以將數(shù)據(jù)庫(kù)的變化流式地傳遞到其他數(shù)據(jù)庫(kù)中。流復(fù)制可以用于各種應(yīng)用場(chǎng)景,例如: 數(shù)據(jù)庫(kù)備份、數(shù)據(jù)中心負(fù)載均衡、實(shí)時(shí)數(shù)據(jù)處理、業(yè)務(wù)分離等。下面我們將詳細(xì)介紹Oracle 11g的流復(fù)制。< p >在Oracle 11g中,流復(fù)制的主要構(gòu)成組件是源端數(shù)據(jù)庫(kù)和目標(biāo)端數(shù)據(jù)庫(kù)。源端數(shù)據(jù)庫(kù)是需要復(fù)制數(shù)據(jù)的數(shù)據(jù)庫(kù),它包含了需要同步到目標(biāo)端數(shù)據(jù)庫(kù)的數(shù)據(jù)。目標(biāo)端數(shù)據(jù)庫(kù)是接收源端數(shù)據(jù)庫(kù)變化的數(shù)據(jù)庫(kù),它可以是單個(gè)數(shù)據(jù)庫(kù)也可以是多個(gè)數(shù)據(jù)庫(kù)。這些數(shù)據(jù)庫(kù)之間會(huì)通過(guò)網(wǎng)絡(luò)通信來(lái)實(shí)現(xiàn)數(shù)據(jù)的同步。< p >要使用流復(fù)制,我們需要在源端和目標(biāo)端數(shù)據(jù)庫(kù)上配置相應(yīng)的組件。首先,我們需要在源端數(shù)據(jù)庫(kù)上啟用日志歸檔(archive log),這樣可以確保數(shù)據(jù)庫(kù)中所有的事務(wù)操作都被記錄在歸檔日志中。然后,我們需要在源端和目標(biāo)端數(shù)據(jù)庫(kù)上創(chuàng)建復(fù)制組件(replication objects),例如:發(fā)布者(publisher)、訂閱者(subscriber)、分發(fā)器(distributor)等。< pre >--在源端數(shù)據(jù)庫(kù)上啟用歸檔日志
ALTER DATABASE ARCHIVELOG;
--創(chuàng)建發(fā)布者
EXECUTE DBMS_STREAMS_ADM.ADD_TABLE_RULES(
table_name =>'hr.employees',
streams_type =>'apply',
streams_name =>'apply_rules_1',
queue_name =>'streams_queue_1',
include_dml =>true,
include_ddl =>false,
source_database =>'source_db'
);
--創(chuàng)建訂閱者
EXECUTE DBMS_STREAMS_ADM.ADD_SUBSCRIBER(
subscriber_name =>'destination_db',
subscriber_address =>'destination_db.example.com',
queue_name =>'streams_queue_1',
include_dml =>true,
include_ddl =>false,
source_database =>'source_db'
); pre >< p >在創(chuàng)建發(fā)布者和訂閱者之后,我們需要在源端和目標(biāo)端數(shù)據(jù)庫(kù)上啟動(dòng)流復(fù)制。啟動(dòng)流復(fù)制之后,源端數(shù)據(jù)庫(kù)中的所有變化都會(huì)自動(dòng)傳輸?shù)侥繕?biāo)端數(shù)據(jù)庫(kù)中進(jìn)行同步。< pre >--在源端數(shù)據(jù)庫(kù)上啟動(dòng)流復(fù)制
EXECUTE DBMS_STREAMS_ADM.START_REPLICATION(
source_object_name_list =>'hr.employees',
destination_db_unique_name =>'destination_db',
source_db_unique_name =>'source_db',
parallelism_mode =>'DEQUEUE_PARALLEL',
propagation_mode =>'ASYNC',
capture_name =>'capture_1',
min_messages =>1,
max_messages =>100
);
--在目標(biāo)端數(shù)據(jù)庫(kù)上啟動(dòng)流復(fù)制
EXECUTE DBMS_STREAMS_ADM.START_REPLICATION(
source_object_name_list =>'hr.employees',
destination_db_unique_name =>'destination_db',
source_db_unique_name =>'source_db',
parallelism_mode =>'DEQUEUE_PARALLEL',
propagation_mode =>'ASYNC',
apply_name =>'apply_1',
min_messages =>1,
max_messages =>100
); pre >< p >除了基本的流復(fù)制配置外,Oracle 11g還提供了高級(jí)的配置選項(xiàng),例如:過(guò)濾器(filter)、轉(zhuǎn)換器(transformer)等。過(guò)濾器可以用于選擇一部分需要同步的數(shù)據(jù),而轉(zhuǎn)換器可以用于修改同步的數(shù)據(jù)格式。< pre >--創(chuàng)建過(guò)濾器
CREATE OR REPLACE FUNCTION filter_function(
dml_event IN SYS.DBMS_STREAMS_EVTS.STREAMS_EVENT_TYPE,
schema_name IN VARCHAR2,
object_name IN VARCHAR2)
RETURN BOOLEAN AS
BEGIN
IF dml_event IN ('insert','update') THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
END;
/
EXECUTE DBMS_STREAMS_ADM.ADD_TABLE_RULES(
table_name =>'hr.employees',
streams_type =>'apply',
streams_name =>'apply_rules_2',
queue_name =>'streams_queue_2',
include_dml =>true,
include_ddl =>false,
source_database =>'source_db',
include_tagged_lcr =>true,
dml_condition =>'"SALARY" >1000',
rule_set_name =>'my_ruleset_1',
rule_type =>DBMS_STREAMS_ADM.RULE_TYPE_SUBSET,
rule_condition =>'DEPARTMENT_ID IN (10,20,30)',
include_extra_txw =>true,
filter_function =>'filter_function'
);
--創(chuàng)建轉(zhuǎn)換器
CREATE OR REPLACE FUNCTION transformer_function(
dml_event IN SYS.DBMS_STREAMS_EVTS.STREAMS_EVENT_TYPE,
ddl_event IN SYS.DBMS_STREAMS_EVTS.STREAMS_EVENT_TYPE,
old_tag IN VARCHAR2,
new_tag IN VARCHAR2,
source_schema IN VARCHAR2,
source_object IN VARCHAR2,
source_type IN VARCHAR2,
source_database IN VARCHAR2,
object_id IN NUMBER,
object_number IN NUMBER,
flags IN NUMBER,
column_count IN NUMBER,
modified_columns IN SYS.DBMS_STREAMS_INT.LCR_COLUMN_LIST,
old_values IN SYS.DBMS_STREAMS_INT.LCR_COLUMN_LIST,
new_values IN SYS.DBMS_STREAMS_INT.LCR_COLUMN_LIST)
RETURN SYS.DBMS_STREAMS_INT.LCR_ROW_RECORD AS
BEGIN
--修改列SALARY的值
new_values(2).VARCHAR2_TYPE := '2000';
RETURN SYS.DBMS_STREAMS_INT.LCR_ROW_RECORD(
OLD_ROW =>old_values,
NEW_ROW =>new_values
);
END;
/
EXECUTE DBMS_STREAMS_ADM.ADD_TABLE_RULES(
table_name =>'hr.employees',
streams_type =>'apply',
streams_name =>'apply_rules_3',
queue_name =>'streams_queue_3',
include_dml =>true,
include_ddl =>false,
source_database =>'source_db',
include_tagged_lcr =>true,
rule_set_name =>'my_ruleset_2',
rule_type =>DBMS_STREAMS_ADM.RULE_TYPE_SUBSET,
rule_condition =>'DEPARTMENT_ID = 10',
include_extra_txw =>true,
transformer_function =>'transformer_function'
); pre >< p >總之,Oracle 11g的流復(fù)制是一種高效、可靠、靈活的數(shù)據(jù)復(fù)制和同步技術(shù),它提供了豐富的配置選項(xiàng),可以滿(mǎn)足各種應(yīng)用場(chǎng)景的需求。如果您想從源端數(shù)據(jù)庫(kù)同步數(shù)據(jù)到目標(biāo)端數(shù)據(jù)庫(kù)中,建議嘗試使用Oracle 11g的流復(fù)制技術(shù)。
網(wǎng)站導(dǎo)航
- zblogPHP模板zbpkf
- zblog免費(fèi)模板zblogfree
- zblog模板學(xué)習(xí)zblogxuexi
- zblogPHP仿站zbpfang