在Oracle中實現(xiàn)數(shù)據(jù)庫的復制
2004-04-23 15:18 pm
作者:余楓
來自:Linux知識寶庫
聯(lián)系方式:無名
在Internet上運作數(shù)據(jù)庫經(jīng)常會有這樣的需求:把遍布全國各城市相似的數(shù)據(jù)庫應用統(tǒng)一起來,一個節(jié)點的數(shù)據(jù)改變不僅體現(xiàn)在本地,還反映到遠端。復制技術給用戶提供了一種快速訪問共享數(shù)據(jù)的辦法。
一、實現(xiàn)數(shù)據(jù)庫復制的前提條件
1、數(shù)據(jù)庫支持高級復制功能
您可以用system身份登錄數(shù)據(jù)庫,查看v$option視圖,如果其中Advanced replication為TRUE,則支持高級復制功能;否則不支持。
2、數(shù)據(jù)庫初始化參數(shù)要求
①、db_domain = test.com.cn
指明數(shù)據(jù)庫的域名(默認的是WORLD),這里可以用您公司的域名。
②、global_names = true
它要求數(shù)據(jù)庫鏈接(database link)和被連接的數(shù)據(jù)庫名稱一致。
現(xiàn)在全局數(shù)據(jù)庫名:db_name+”.”+db_domain
③、有跟數(shù)據(jù)庫job執(zhí)行有關的參數(shù)
job_queue_processes = 1
job_queue_interval = 60
distributed_transactions = 10
open_links = 4
第一行定義SNP進程的啟動個數(shù)為n。系統(tǒng)缺省值為0,正常定義范圍為0~36,根據(jù)任務的多少,可以配置不同的數(shù)值。
第二行定義系統(tǒng)每隔N秒喚醒該進程一次。系統(tǒng)缺省值為60秒,正常范圍為1~3600秒。事實上,該進程執(zhí)行完當前任務后,就進入睡眠狀態(tài),睡眠一段時間后,由系統(tǒng)的總控負責將其喚醒。
如果修改了以上這幾個參數(shù),需要重新啟動數(shù)據(jù)庫以使參數(shù)生效。
二、實現(xiàn)數(shù)據(jù)庫同步復制的步驟
假設在Internet上我們有兩個數(shù)據(jù)庫:一個叫深圳(shenzhen),一個叫北京(beijing)。
具體配置見下表:
數(shù)據(jù)庫名 shenzhen beijing
數(shù)據(jù)庫域名 test.com.cn test.com.cn
數(shù)據(jù)庫sid號 shenzhen beijing
Listener端口號 1521 1521
服務器ip地址 10.1.1.100 10.1.1.200
1、確認兩臺數(shù)據(jù)庫之間可以互相訪問,在tnsnames.ora里設置數(shù)據(jù)庫連接字符串。
①、例如:深圳這邊的數(shù)據(jù)庫連接字符串是以下的格式
beijing =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.1.200)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = beijing)
)
)
運行$tnsping beijing
出現(xiàn)以下提示符:
Attempting to contact (ADDRESS=(PROTOCOL=TCP)(HOST=10.1.1.200)(PORT=1521))
OK(n毫秒)
表明深圳數(shù)據(jù)庫可以訪問北京數(shù)據(jù)庫。
②、在北京那邊也同樣配置,確認$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;
返回結果為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;
返回結果為shenzhen.test.com.cn就對了。
3、建立管理數(shù)據(jù)庫復制的用戶repadmin,并賦權。
①、用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ù)庫,運行以上的命令,管理數(shù)據(jù)庫復制的用戶repadmin,并賦權。
說明:repadmin用戶名和密碼可以根據(jù)用戶的需求自由命名。
4、在數(shù)據(jù)庫復制的用戶repadmin下創(chuàng)建私有的數(shù)據(jù)庫鏈接。
①、用repadmin身份登錄shenzhen數(shù)據(jù)庫
SQL>create database link beijing.test.com.cn connect to repadmin identified by repadmin;
測試這個私有的數(shù)據(jù)庫鏈接:
SQL>select * from global_name@beijing.test.com.cn;
返回結果為beijing.test.com.cn就對了。
②、用repadmin身份登錄beijing數(shù)據(jù)庫
SQL>create database link shenzhen.test.com.cn connect to repadmin identified by repadmin;
測試這個私有的數(shù)據(jù)庫鏈接
SQL>select * from global_name@shenzhen.test.com.cn;
返回結果為shenzhen.test.com.cn就對了。
5、創(chuàng)建或選擇實現(xiàn)數(shù)據(jù)庫復制的用戶和對象,給用戶賦權,數(shù)據(jù)庫對象必須有主關鍵字。
假設我們用ORACLE里舉例用的scott用戶,dept表。
①、用internal身份登錄shenzhen數(shù)據(jù)庫,創(chuàng)建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數(shù)據(jù)庫,創(chuàng)建表dept
SQL>create table dept
(deptno number(2) primary key,
dname varchar2(14),
loc varchar2(13) );
③、如果數(shù)據(jù)庫對象沒有主關鍵字,可以運行以下SQL命令添加:
SQL>alter table dept add (constraint dept_deptno_pk primary key (deptno));
④、在shenzhen數(shù)據(jù)庫scott用戶下創(chuàng)建主關鍵字的序列號,范圍避免和beijing的沖突。
SQL> create sequence dept_no increment by 1 start with 1 maxvalue 44 cycle nocache;
(說明:maxvalue 44可以根據(jù)應用程序及表結構主關鍵字定義的位數(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ù)庫那邊同樣運行以上①,②,③
⑦、在beijing數(shù)據(jù)庫scott用戶下創(chuàng)建主關鍵字的序列號,范圍避免和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)建要復制的組scott_mg,加入數(shù)據(jù)庫對象,產(chǎn)生對象的復制支持
①、用repadmin身份登錄shenzhen數(shù)據(jù)庫,創(chuàng)建主復制組scott_mg
SQL> execute dbms_repcat.create_master_repgroup('scott_mg');
說明:scott_mg組名可以根據(jù)用戶的需求自由命名。
②、在復制組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 實現(xiàn)數(shù)據(jù)庫復制的用戶名稱
oname 實現(xiàn)數(shù)據(jù)庫復制的數(shù)據(jù)庫對象名稱
(表名長度在27個字節(jié)內(nèi),程序包名長度在24個字節(jié)內(nèi))
type 實現(xiàn)數(shù)據(jù)庫復制的數(shù)據(jù)庫對象類別
(支持的類別:表,索引,同義詞,觸發(fā)器,視圖,過程,函數(shù),程序包,程序包體)
use_existing_object true表示用主復制節(jié)點已經(jīng)存在的數(shù)據(jù)庫對象
gname 主復制組名
③、對數(shù)據(jù)庫對象產(chǎn)生復制支持
SQL>execute dbms_repcat.generate_replication_support('scott','dept','table');
(說明:產(chǎn)生支持scott用戶下dept表復制的數(shù)據(jù)庫觸發(fā)器和程序包)
④、確認復制的組和對象已經(jīng)加入數(shù)據(jù)庫的數(shù)據(jù)字典
SQL>select gname, master, status from dba_repgroup;
SQL>select * from dba_repobject;
7、創(chuàng)建主復制節(jié)點
①、用repadmin身份登錄shenzhen數(shù)據(jù)庫,創(chuàng)建主復制節(jié)點
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 主復制組名
master 加入主復制節(jié)點的另一個數(shù)據(jù)庫
use_existing_object true表示用主復制節(jié)點已經(jīng)存在的數(shù)據(jù)庫對象
copy_rows false表示第一次開始復制時不用和主復制節(jié)點保持一致
propagation_mode 異步地執(zhí)行
②、確認復制的任務隊列已經(jīng)加入數(shù)據(jù)庫的數(shù)據(jù)字典
SQL>select * from user_jobs;
8、使同步組的狀態(tài)由停頓(quiesced )改為正常(normal)
①、用repadmin身份登錄shenzhen數(shù)據(jù)庫,運行以下命令
SQL> execute dbms_repcat.resume_master_activity('scott_mg',false);
②、確認同步組的狀態(tài)為正常(normal)
SQL> select gname, master, status from dba_repgroup;
③、如果這個①命令不能使同步組的狀態(tài)為正常(normal),可能有一些停頓的復制,運行以下命令再試試(建議在緊急的時候才用):
SQL> execute dbms_repcat.resume_master_activity('scott_mg',true);
9、創(chuàng)建復制數(shù)據(jù)庫的時間表,我們假設用固定的時間表:10分鐘復制一次。
①、用repadmin身份登錄shenzhen數(shù)據(jù)庫,運行以下命令
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ù)庫,運行以下命令
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ù)庫的記錄,跟蹤復制過程
如果你想立刻看到添加或修改后數(shù)據(jù)庫的記錄的變化,可以在兩邊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;
正常的狀態(tài)有兩種:
任務閑——this_date為空,next_date為當前時間后的一個時間值
任務忙——this_date不為空,next_date為當前時間后的一個時間值
異常狀態(tài)也有兩種:
任務死鎖——next_date為當前時間前的一個時間值
任務死鎖——next_date為非常大的一個時間值,例如:4001-01-01
這可能因為網(wǎng)絡中斷照成的死鎖
解除死鎖的辦法:
$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編號。
2、增加或減少復制組的復制對象
①、停止主數(shù)據(jù)庫節(jié)點的復制動作,使同步組的狀態(tài)由正常(normal)改為停頓(quiesced )
用repadmin身份登錄shenzhen數(shù)據(jù)庫,運行以下命令
SQL>execute dbms_repcat.suspend_master_activity (gname => 'scott_mg');
②、在復制組scott_mg里加入數(shù)據(jù)庫對象,保證數(shù)據(jù)庫對象必須有主關鍵字。
SQL>execute dbms_repcat.create_master_repobject(sname=>'scott',oname=>'emp', type=>'table',use_existing_object=>true,gname=>'scott_mg');
對加入的數(shù)據(jù)庫對象產(chǎn)生復制支持
SQL>execute dbms_repcat.generate_replication_support('scott','emp','table');
③、在復制組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
小力力力 閱讀(693)
評論(0) 編輯 收藏 所屬分類:
ORACLE