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