在Oracle中實(shí)現(xiàn)數(shù)據(jù)庫的復(fù)制
2004-04-23 15:18 pm
作者:余楓
來自:Linux知識(shí)寶庫
聯(lián)系方式:無名
在Internet上運(yùn)作數(shù)據(jù)庫經(jīng)常會(huì)有這樣的需求:把遍布全國各城市相似的數(shù)據(jù)庫應(yīng)用統(tǒng)一起來,一個(gè)節(jié)點(diǎn)的數(shù)據(jù)改變不僅體現(xiàn)在本地,還反映到遠(yuǎn)端。復(fù)制技術(shù)給用戶提供了一種快速訪問共享數(shù)據(jù)的辦法。
一、實(shí)現(xiàn)數(shù)據(jù)庫復(fù)制的前提條件
1、數(shù)據(jù)庫支持高級復(fù)制功能
您可以用system身份登錄數(shù)據(jù)庫,查看v$option視圖,如果其中Advanced replication為TRUE,則支持高級復(fù)制功能;否則不支持。
2、數(shù)據(jù)庫初始化參數(shù)要求
①、db_domain = test.com.cn
指明數(shù)據(jù)庫的域名(默認(rèn)的是WORLD),這里可以用您公司的域名。
②、global_names = true
它要求數(shù)據(jù)庫鏈接(database link)和被連接的數(shù)據(jù)庫名稱一致。
現(xiàn)在全局?jǐn)?shù)據(jù)庫名:db_name+”.”+db_domain
③、有跟數(shù)據(jù)庫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ù)庫以使參數(shù)生效。
二、實(shí)現(xiàn)數(shù)據(jù)庫同步復(fù)制的步驟
假設(shè)在Internet上我們有兩個(gè)數(shù)據(jù)庫:一個(gè)叫深圳(shenzhen),一個(gè)叫北京(beijing)。
具體配置見下表:
數(shù)據(jù)庫名 shenzhen beijing
數(shù)據(jù)庫域名 test.com.cn test.com.cn
數(shù)據(jù)庫sid號 shenzhen beijing
Listener端口號 1521 1521
服務(wù)器ip地址 10.1.1.100 10.1.1.200
1、確認(rèn)兩臺(tái)數(shù)據(jù)庫之間可以互相訪問,在tnsnames.ora里設(shè)置數(shù)據(jù)庫連接字符串。
①、例如:深圳這邊的數(shù)據(jù)庫連接字符串是以下的格式
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ù)庫可以訪問北京數(shù)據(jù)庫。
②、在北京那邊也同樣配置,確認(rèn)$tnsping shenzhen 是通的。
2、改數(shù)據(jù)庫全局名稱,建公共的數(shù)據(jù)庫鏈接。
①、用system身份登錄shenzhen數(shù)據(jù)庫
SQL>alter database rename global_name to shenzhen.test.com.cn;
用system身份登錄beijing數(shù)據(jù)庫:
SQL>alter database rename global_name to beijing.test.com.cn;
②、用system身份登錄shenzhen數(shù)據(jù)庫
SQL>create public database link beijing.test.com.cn using 'beijing';
測試數(shù)據(jù)庫全局名稱和公共的數(shù)據(jù)庫鏈接
SQL>select * from global_name@beijing.test.com.cn;
返回結(jié)果為beijing.test.com.cn就對了。
用system身份登錄beijing數(shù)據(jù)庫:
SQL>create public database link shenzhen.test.com.cn using 'shenzhen';
測試數(shù)據(jù)庫全局名稱和公共的數(shù)據(jù)庫鏈接
SQL>select * from global_name@shenzhen.test.com.cn;
返回結(jié)果為shenzhen.test.com.cn就對了。
3、建立管理數(shù)據(jù)庫復(fù)制的用戶repadmin,并賦權(quán)。
①、用system身份登錄shenzhen數(shù)據(jù)庫
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ù)庫,運(yùn)行以上的命令,管理數(shù)據(jù)庫復(fù)制的用戶repadmin,并賦權(quán)。
說明:repadmin用戶名和密碼可以根據(jù)用戶的需求自由命名。
4、在數(shù)據(jù)庫復(fù)制的用戶repadmin下創(chuàng)建私有的數(shù)據(jù)庫鏈接。
①、用repadmin身份登錄shenzhen數(shù)據(jù)庫
SQL>create database link beijing.test.com.cn connect to repadmin identified by repadmin;
測試這個(gè)私有的數(shù)據(jù)庫鏈接:
SQL>select * from global_name@beijing.test.com.cn;
返回結(jié)果為beijing.test.com.cn就對了。
②、用repadmin身份登錄beijing數(shù)據(jù)庫
SQL>create database link shenzhen.test.com.cn connect to repadmin identified by repadmin;
測試這個(gè)私有的數(shù)據(jù)庫鏈接
SQL>select * from global_name@shenzhen.test.com.cn;
返回結(jié)果為shenzhen.test.com.cn就對了。
5、創(chuàng)建或選擇實(shí)現(xiàn)數(shù)據(jù)庫復(fù)制的用戶和對象,給用戶賦權(quán),數(shù)據(jù)庫對象必須有主關(guān)鍵字。
假設(shè)我們用ORACLE里舉例用的scott用戶,dept表。
①、用internal身份登錄shenzhen數(shù)據(jù)庫,創(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ù)庫,創(chuàng)建表dept
SQL>create table dept
(deptno number(2) primary key,
dname varchar2(14),
loc varchar2(13) );
③、如果數(shù)據(jù)庫對象沒有主關(guān)鍵字,可以運(yùn)行以下SQL命令添加:
SQL>alter table dept add (constraint dept_deptno_pk primary key (deptno));
④、在shenzhen數(shù)據(jù)庫scott用戶下創(chuàng)建主關(guān)鍵字的序列號,范圍避免和beijing的沖突。
SQL> create sequence dept_no increment by 1 start with 1 maxvalue 44 cycle nocache;
(說明:maxvalue 44可以根據(jù)應(yīng)用程序及表結(jié)構(gòu)主關(guān)鍵字定義的位數(shù)需要而定)
⑤、在shenzhen數(shù)據(jù)庫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ù)庫那邊同樣運(yùn)行以上①,②,③
⑦、在beijing數(shù)據(jù)庫scott用戶下創(chuàng)建主關(guān)鍵字的序列號,范圍避免和shenzhen的沖突。
SQL> create sequence dept_no increment by 1 start with 45 maxvalue 99 cycle nocache;
⑧、在beijing數(shù)據(jù)庫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ù)庫對象,產(chǎn)生對象的復(fù)制支持
①、用repadmin身份登錄shenzhen數(shù)據(jù)庫,創(chuàng)建主復(fù)制組scott_mg
SQL> execute dbms_repcat.create_master_repgroup('scott_mg');
說明:scott_mg組名可以根據(jù)用戶的需求自由命名。
②、在復(fù)制組scott_mg里加入數(shù)據(jù)庫對象
SQL>execute dbms_repcat.create_master_repobject(sname=>'scott',oname=>'dept', type=>'table',use_existing_object=>true,gname=>'scott_mg');
參數(shù)說明:
sname 實(shí)現(xiàn)數(shù)據(jù)庫復(fù)制的用戶名稱
oname 實(shí)現(xiàn)數(shù)據(jù)庫復(fù)制的數(shù)據(jù)庫對象名稱
(表名長度在27個(gè)字節(jié)內(nèi),程序包名長度在24個(gè)字節(jié)內(nèi))
type 實(shí)現(xiàn)數(shù)據(jù)庫復(fù)制的數(shù)據(jù)庫對象類別
(支持的類別:表,索引,同義詞,觸發(fā)器,視圖,過程,函數(shù),程序包,程序包體)
use_existing_object true表示用主復(fù)制節(jié)點(diǎn)已經(jīng)存在的數(shù)據(jù)庫對象
gname 主復(fù)制組名
③、對數(shù)據(jù)庫對象產(chǎn)生復(fù)制支持
SQL>execute dbms_repcat.generate_replication_support('scott','dept','table');
(說明:產(chǎn)生支持scott用戶下dept表復(fù)制的數(shù)據(jù)庫觸發(fā)器和程序包)
④、確認(rèn)復(fù)制的組和對象已經(jīng)加入數(shù)據(jù)庫的數(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ù)庫,創(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ù)說明:
gname 主復(fù)制組名
master 加入主復(fù)制節(jié)點(diǎn)的另一個(gè)數(shù)據(jù)庫
use_existing_object true表示用主復(fù)制節(jié)點(diǎn)已經(jīng)存在的數(shù)據(jù)庫對象
copy_rows false表示第一次開始復(fù)制時(shí)不用和主復(fù)制節(jié)點(diǎn)保持一致
propagation_mode 異步地執(zhí)行
②、確認(rèn)復(fù)制的任務(wù)隊(duì)列已經(jīng)加入數(shù)據(jù)庫的數(shù)據(jù)字典
SQL>select * from user_jobs;
8、使同步組的狀態(tài)由停頓(quiesced )改為正常(normal)
①、用repadmin身份登錄shenzhen數(shù)據(jù)庫,運(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ù)庫的時(shí)間表,我們假設(shè)用固定的時(shí)間表:10分鐘復(fù)制一次。
①、用repadmin身份登錄shenzhen數(shù)據(jù)庫,運(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ù)庫,運(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ù)庫的記錄,跟蹤復(fù)制過程
如果你想立刻看到添加或修改后數(shù)據(jù)庫的記錄的變化,可以在兩邊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)程號ora_snp*,用kill –9 命令刪除此進(jìn)程
然后進(jìn)入repadmin 用戶SQL>操作符下,運(yùn)行命令:
SQL>exec dbms_job.run(job_number);
說明:job_number 為用select job,this_date,next_date,what from user_jobs;命令查出的job編號。
2、增加或減少復(fù)制組的復(fù)制對象
①、停止主數(shù)據(jù)庫節(jié)點(diǎn)的復(fù)制動(dòng)作,使同步組的狀態(tài)由正常(normal)改為停頓(quiesced )
用repadmin身份登錄shenzhen數(shù)據(jù)庫,運(yùn)行以下命令
SQL>execute dbms_repcat.suspend_master_activity (gname => 'scott_mg');
②、在復(fù)制組scott_mg里加入數(shù)據(jù)庫對象,保證數(shù)據(jù)庫對象必須有主關(guān)鍵字。
SQL>execute dbms_repcat.create_master_repobject(sname=>'scott',oname=>'emp', type=>'table',use_existing_object=>true,gname=>'scott_mg');
對加入的數(shù)據(jù)庫對象產(chǎn)生復(fù)制支持
SQL>execute dbms_repcat.generate_replication_support('scott','emp','table');
③、在復(fù)制組scott_mg里刪除數(shù)據(jù)庫對象。
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 2005-03-10 15:35
小力力力 閱讀(694)
評論(0) 編輯 收藏 所屬分類:
ORACLE