在ORCAL中實現資料庫的複製

來源:互聯網
上載者:User
在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.200  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';
  測試資料庫全域名稱和公用的資料庫連結
  SQL>select * from global_name@beijing.test.com.cn;
  返回  結果為beijing.test.com.cn就對了。
  用system身份登入beijing資料庫:
  SQL>create public database link shenzhen.test.com.cn using 'shenzhen';
  測試資料庫全域名稱和公用的資料庫連結
  SQL>select * from global_name@shenzhen.test.com.cn;
  返回結果為shenzhen.test.com.cn就對了。
  3、建立管理資料庫複製的使用者repadmin,並賦權。
  ①、用system身份登入shenzhen資料庫
  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;
  測試這個私人的資料庫連結:
  SQL>select * from global_name@beijing.test.com.cn;
  返回結果為beijing.test.com.cn就對了。
  ②、用repadmin身份登入beijing資料庫
  SQL>create database link shenzhen.test.com.cn connect to repadmin identified by
   repadmin;
  測試這個私人的資料庫連結
  SQL>select * from global_name@shenzhen.test.com.cn;
  返回結果為shenzhen.test.com.cn就對了。
  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');
  SQL>commit;
  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');
    
  參數說明:
  gname 主複製組名
  master 加入主複製節點的另一個資料庫
  use_existing_object true表示用主複製節點已經存在的資料庫物件
  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分鐘複製一次。
  ①、用repadmin身份登入shenzhen資料庫,運行以下命令
  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資料庫,運行以下命令
  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、添加或修改兩邊資料庫的記錄,跟蹤複製過程
  如果你想立刻看到添加或修改後資料庫的記錄的變化,可以在兩邊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>操作符下,運行命令:
  SQL>exec dbms_job.run(job_number);
  說明: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)。
  SQL> execute dbms_repcat.resume_master_activity('scott_mg',false);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.