Oracle如何?兩個資料庫的同步

來源:互聯網
上載者:User

我們經常希望把各地的資料入庫後進行統一的應用。現在可以用複製技術來解決這個問題。但實現資料庫複寫也是要有一些條件的。

首先,資料庫要具備進階複製功能(用system身份登入資料庫,查看v$option視圖,如果其中Advanced replication為TRUE,則支援進階複製功能;否則不支援)。

如果具備進階複製功能,資料庫要進行一些參數初始化。

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上有兩個資料庫:一個叫中國(China),一個叫日本(Japan)。
具體配置如下:
資料庫名:China、Japan
資料庫網域名稱 test.com.cn
資料庫sid號 China、Japan
Listener連接埠號碼 1521
伺服器ip地址 10.1.0.100 10.1.0.200
確認兩個資料庫之間可以互相訪問,在tnsnames.ora裡設定資料庫連接字串。
中國這邊的資料庫連接字串是以下的格式:
Japan =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.1.200)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = Japan)
)
)
運行$tnsping Japan,出現以下提示符:
Attempting to contact (ADDRESS=(PROTOCOL=TCP)(HOST=10.1.1.200)(PORT=1521))
OK(n毫秒)
表明中國資料庫可以訪問日本資料庫。在日本那邊也同樣配置,確認$tnsping China 是通的。
改資料庫全域名稱,建公用的資料庫連結。
用system身份登入China資料庫
SQL>alter database rename global_name to China.test.com.cn;
用system身份登入Japan資料庫:
SQL>alter database rename global_name to Japan.test.com.cn;
用system身份登入China資料庫。
SQL>create public database link Japan.test.com.cn using 'Japan';
測試資料庫全域名稱和公用的資料庫連結。
SQL>select * from global_name@Japan.test.com.cn;
返回結果為Japan.test.com.cn就對了。
用system身份登入Japan資料庫:
SQL>create public database link China.test.com.cn using 'China';
測試資料庫全域名稱和公用的資料庫連結。
SQL>select * from global_name@China.test.com.cn;
返回結果為China.test.com.cn就對了。
建立管理資料庫複製的使用者repadmin,並賦權。
用system身份登入China資料庫:
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身份登入Japan資料庫,運行以上的命令,管理資料庫複製的使用者repadmin,並賦權。
在資料庫複寫的使用者repadmin下建立私人的資料庫連結。
用repadmin身份登入China資料庫。
SQL>create database link Japan.test.com.cn connect to repadmin identified 試這個私人的資料庫連結:
SQL>select * from global_name@Japan.test.com.cn;
返回結果為Japan.test.com.cn就對了。
用repadmin身份登入Japan資料庫。
SQL>create database link China.test.com.cn connect to repadmin identified by repadmin;
測試這個私人的資料庫連結:
SQL>select * from global_name@China.test.com.cn;
返回結果為China.test.com.cn就對了。
建立或選擇實現資料庫複寫的使用者和對象,給使用者賦權,資料庫物件必須有主關鍵字。
用internal身份登入China資料庫,建立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身份登入China資料庫,建立表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));
在China資料庫scott使用者下建立主關鍵字的序號,範圍避免和Japan的衝突。
SQL> create sequence dept_no increment by 1 start with 1 maxvalue 44 cycle nocache;
在China資料庫scott使用者下插入初始化資料
SQL>insert into dept values (dept_no.nextval,'accounting','new york');
SQL>insert into dept values (dept_no.nextval,'research','dallas');
SQL>commit;
在Japan資料庫那邊同樣運行以上①,②,③。
在Japan資料庫scott使用者下建立主關鍵字的序號,範圍避免和China的衝突。
SQL> create sequence dept_no increment by 1 start with 45 maxvalue 99 cycle nocache;
在Japan資料庫scott使用者下插入初始化資料。
SQL>insert into dept values (dept_no.nextval,'sales','chicago');
SQL>insert into dept values (dept_no.nextval,'operations','boston');
SQL>commit;

  • 1
  • 2
  • 3
  • 下一頁

相關文章

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.