天天看點

Oracle to MySQL Goldengate實作增量遷移第一部分:安裝和基本配置.bash_profileGet the aliases and functionsUser specific environment and startup programs第二部分:oracle源端配置第三部分:mysql目标端的配置第四部分:總結第五部分:新加表的同步

兩台rhel 6.4虛拟機,分别異構oracle到mysql資料庫同步測試

ip:192.168.0.23 部署oracle 11.2.0.4,goldgate 12.1.2.1.0 for oracle

ip:192.168.0.25 部署mysql5.5.9,goldengate 12.1.2.1.0 for mysql

char|datetime|varchar|timestamp|int|binarytinyint|varbinarysmall int|textmedium int|tinytextbig int|mediumtextdecimal|longtextfloat|blobdouble|tinyblobdate|mediumblobtime|longblobyear|enum|bit(m)

•the xml, set, and geometry data types and similar are not supported.

•there is limited support for the interval data type.

mysql字元集:

database create database test charset utf8;

table create table test( id int, name char(100)) charset utf8;

column create table test ( id int, name1 char(100) charset gbk, name2 char(100) charset utf8));

insert, update, delete on target tables

create table

select any table or select on database.table

oracle goldengate supports innodb storage engine for a source mysql database,goldengate對mysql隻支援innodb引擎,是以,在建立mysql端的表的時候,要指定表為innodb引擎。create table mysql (name char(10)) engine=innodb;

當然5.9預設的存儲引擎就是innodb。

将ogg壓縮包(v47367-01.zip)解壓到 /u01 下

ogg 12c的安裝和以前的版本安裝不同,使用了oui的安裝方式,以前的版本直接解壓就可以了.是以最好要有x環境.如果沒有,也支援靜默安裝.

安裝步驟如下:

[oracle@oggtest disk1]$ ./runinstaller

顯示歡迎界面,點下一步,

Oracle to MySQL Goldengate實作增量遷移第一部分:安裝和基本配置.bash_profileGet the aliases and functionsUser specific environment and startup programs第二部分:oracle源端配置第三部分:mysql目标端的配置第四部分:總結第五部分:新加表的同步

gg 12c隻支援 oracle 11g和oracle 12c,選擇合适的oracle版本.

Oracle to MySQL Goldengate實作增量遷移第一部分:安裝和基本配置.bash_profileGet the aliases and functionsUser specific environment and startup programs第二部分:oracle源端配置第三部分:mysql目标端的配置第四部分:總結第五部分:新加表的同步

software location定義了ogg将要安裝的位置,database location定義了oracle資料庫軟體的安裝位置,并且定義manager的端口.

Oracle to MySQL Goldengate實作增量遷移第一部分:安裝和基本配置.bash_profileGet the aliases and functionsUser specific environment and startup programs第二部分:oracle源端配置第三部分:mysql目标端的配置第四部分:總結第五部分:新加表的同步

出現安裝資訊彙總,選擇"install"安裝.

把gg的安裝目錄分别到加ld_library_path和path變量中.整個安裝過程還是十分簡單,雖然也是oui的安裝方式,隻是在software和database location處有可能會産生誤解。

下面是oracle使用者的環境變量設定

[oracle@oggtest ~]$ cat .bash_profile

bash_profile:

<code></code>

<code>if [ -f ~/.bashrc ]; then</code>

<code>fi</code>

<code>path=$path:$home/bin</code>

<code>export path</code>

<code>oracle_base=/u01/app/oracle; export oracle_base oracle_home=$oracle_base/product/11.2.0/db_1; export oracle_home ogg_home=$oracle_base/product/11.2.0/ogg12c; export ogg_home oracle_sid=oggtt; export oracle_sid oracle_term=xterm; export oracle_term path=$oracle_home/bin:$ogg_home:$base_path:/usr/sbin:$path; export path</code>

<code>ld_library_path=$oracle_home/lib:$ogg_home:/lib:/usr/lib; export ld_library_path classpath=$oracle_home/jre:$oracle_home/jlib:$oracle_home/rdbms/jlib; export classpath</code>

create subdirectories:

<code>oracle goldengate command interpreter for oracle version 12.1.2.1.0 oggcore_12.1.2.1.0_platforms_140727.2135.1_fbo linux, x64, 64bit (optimized), oracle 11g on aug 7 2014 09:14:25 operating system character set identified as utf-8.</code>

<code>copyright (c) 1995, 2014, oracle and/or its affiliates. all rights reserved. ggsci (oggtest) 1&gt; create subdirs</code>

<code>creating subdirectories under current directory /u01/app/oracle/product/11.2.0/ogg12c</code>

<code>parameter files /u01/app/oracle/product/11.2.0/ogg12c/dirprm: already exists report files /u01/app/oracle/product/11.2.0/ogg12c/dirrpt: already exists checkpoint files /u01/app/oracle/product/11.2.0/ogg12c/dirchk: already exists process status files /u01/app/oracle/product/11.2.0/ogg12c/dirpcs: already exists sql script files /u01/app/oracle/product/11.2.0/ogg12c/dirsql: already exists database definitions files /u01/app/oracle/product/11.2.0/ogg12c/dirdef: already exists extract data files /u01/app/oracle/product/11.2.0/ogg12c/dirdat: already exists temporary files /u01/app/oracle/product/11.2.0/ogg12c/dirtmp: already exists credential store files /u01/app/oracle/product/11.2.0/ogg12c/dircrd: already exists masterkey wallet files /u01/app/oracle/product/11.2.0/ogg12c/dirwlt: already exists dump files /u01/app/oracle/product/11.2.0/ogg12c/dirdmp: already exists</code>

3.1 修改資料庫為歸檔模式,略過。

3.2 打開輔助日志

打開輔助日志:

3.3 關閉資源回收筒

關閉資源回收筒:

3.4 建立複制用的使用者,并授權

create user:

<code>user created.</code>

<code>sql&gt; grant dba to ggs;</code>

<code>grant succeeded. 測試系統,授予了dba權限,科用以下權限替代dba權限 grant create table to ggs; grant connect to ggs; grant alter any table to ggs; grant alter session to ggs; grant create session to ggs; grant flashback any table to ggs; grant select any dictionary to ggs; grant select any table to ggs; grant resource to ggs; grant delete any table to ggs; grant insert any table to ggs; grant update any table to ggs; grant restricted session to ggs; system altered.</code>

3.5 登陸到ogg,執行初始化

初始化:

1.安裝mysql5.5.9略過

2.給mysqlroot配置密碼:

mysql端安裝:

<code>mysql&gt; show grants for root@localhost;grants for root@localhostgrant all privileges on . to 'root'@'localhost' with grant optiongrant proxy on ''@'' to 'root'@'localhost' with grant option</code>

<code>2 rows in set (0.08 sec)</code>

<code>mysql&gt; flush privileges; query ok, 0 rows affected (0.01 sec) mysql&gt; exit</code>

3.因為複制需要二進制日志,是以啟動mysql的時候需要啟用二進制日志

檢視mysql是否開啟了二進制檔案

二進制檔案:

<code>mysql&gt; show variables like 'log_bin';variable_namevaluelog_binon</code>

<code>1 row in set (0.00 sec)</code>

<code></code><code>mysql&gt; show master status;filepositionbinlog_do_dbbinlog_ignore_dbmysql-bin.000003487</code>

<code>1 row in set (0.05 sec)</code>

<code>[root@oggtest ~]# mysqlbinlog mysql-bin.000003</code>

初始化備庫端:

<code>oracle goldengate command interpreter for mysql version 12.1.2.1.0 oggcore_12.1.2.1.0_platforms_140920.0203 linux, x64, 64bit (optimized), mysql enterprise on sep 20 2014 03:43:22 operating system character set identified as utf-8. copyright (c) 1995, 2014, oracle and/or its affiliates. all rights reserved. ggsci (ttmysql) 1&gt; create subdirs</code>

<code>creating subdirectories under current directory /ogg</code>

<code>parameter files /ogg/dirprm: already exists report files /ogg/dirrpt: created checkpoint files /ogg/dirchk: created process status files /ogg/dirpcs: created sql script files /ogg/dirsql: created database definitions files /ogg/dirdef: created extract data files /ogg/dirdat: created temporary files /ogg/dirtmp: created credential store files /ogg/dircrd: created masterkey wallet files /ogg/dirwlt: created dump files /ogg/dirdmp: created</code>

第一部分安裝和基本配置完成。

源端配置大緻分為如下三個步驟:配置mgr,配置抽取程序,配置投遞程序。

在源端先建立一張表,記得帶主鍵:

oracle建表:

<code>table created.</code>

oracle配置全局設定:

<code>copyright (c) 1995, 2014, oracle and/or its affiliates. all rights reserved.</code>

<code>ggsci (oggtest) 1&gt;dblogin userid ggs password ggs successfully logged into database.</code>

<code>ggsci (oggtest as ggs@oggtt) 2&gt; view params ./globals</code>

<code>ggschema ggs ggsci (oggtest as ggs@oggtt) 3&gt;</code>

配置mgr:

manager程序參數配置說明:

port:指定服務監聽端口;這裡以7809為例,預設端口為7809

dynamicportlist:動态端口:可以制定最大256個可用端口的動态清單,當指定的端口不可用時,管理程序将會從清單中選擇一個可用的端口,源端和目标段的collector、replicat、ggsci程序通信也會使用這些端口;

comment:注釋行,也可以用--來代替;

autostart:指定在管理程序啟動時自動啟動哪些程序;

autorestart:自動重新開機參數設定:本處設定表示每3分鐘嘗試重新啟動所有extract程序,共嘗試5次;

purgeoldextracts:定期清理trail檔案設定:本處設定表示對于超過3天的trail檔案進行删除。

lagreport、laginfo、lagcritical:

定義資料延遲的預警機制:本處設定表示mgr程序每隔1小時檢查extract的延遲情況,如果超過了30分鐘就把延遲作為資訊記錄到錯誤日志中,如果延遲超過了45分鐘,則把它作為警告寫到錯誤日志中。

啟動mgr:

<code>program status group lag at chkpt time since chkpt</code>

<code>manager running</code>

添加并檢視需要複制的表:

<code>ggsci (oggtest as ggs@oggtt) 8&gt; info trandata chen.* logging of supplemental redo log data is enabled for table chen.student. columns supplementally logged for table chen.student: id. ggsci (oggtest as ggs@oggtt) 9&gt;</code>

如果不執行add trandata,insert同步沒有問題(oracle資料庫),但是在同步update或delete操作時,就會因為丢失主鍵報同步錯誤。不開啟表級的最小附加日志,update的redo資訊不記錄沒有進行更新的字段資訊,如主鍵不更新的話主鍵不記錄在redo中,是以會導緻同步失敗。

配置抽取程序:

<code>ggsci (oggtest as ggs@oggtt) 10&gt; add extract ext3,tranlog,begin now extract added.</code>

<code>ggsci (oggtest as ggs@oggtt) 11&gt; add exttrail /u01/app/oracle/product/11.2.0/ogg12c/dirdat/xs,extract ext3 exttrail added.</code>

<code>ext的模闆可以是: extract extmb setenv (nls_lang = "american_america.utf8") setenv (oracle_home = "/u01/oracle/product/11.2.0/db_1") setenv (oracle_sid = "orcl") userid ggs, password ggs --gettruncates reportcount every 1 minutes, rate discardfile ./dirrpt/extmb.dsc,append,megabytes 1024 --threadoptions maxcommitpropagationdelay 60000 iolatens 60000 dboptions allowunusedcolumn warnlongtrans 2h,checkinterval 3m exttrail ./dirdat/mb --tranlogoptions excludeuser username fetchoptions nousesnapshot tranlogoptions convertucs2clobs table hr.emp;</code>

setenv:配置系統環境變量

userid/ password: 指定ogg連接配接資料庫的使用者名和密碼,這裡使用3.4部分中建立的資料庫使用者ogg;

table:定義需複制的表,後面需以;結尾

tableexclude:定義需要排除的表,如果在table參數中使用了通配符,可以使用該參數指定排除掉得表。

getupdateafters|ignoreupdateafters:

是否在隊列中寫入後影像,預設複制

getupdatebefores| ignoreupdatebefores:

是否在隊列中寫入前影像,預設不複制

getupdates|ignoreupdates:

是否複制update操作,預設複制

getdeletes|ignoredeletes:

是否複制delete操作,預設複制

getinserts|ignoreinserts:

是否複制insert操作,預設複制

gettruncates|ignoretrundates:

是否複制truncate操作,預設不複制;

配置投遞程序:

<code>ggsci (oggtest as ggs@oggtt) 13&gt; add extract push3,exttrailsource /u01/app/oracle/product/11.2.0/ogg12c/dirdat/xs extract added. ggsci (oggtest as ggs@oggtt) 14&gt; add rmttrail /ogg/dirdat/xs,extract push3 rmttrail added.</code>

<code>push的模闆: extract pushmb setenv (nls_lang = "american_america.utf8") userid ggs, password ggs passthru rmthost 192.168.0.165, mgrport 7809, compress rmttrail /u01/ogg/11.2/dirdat/xs table hr.ah4;</code>

rmthost:指定目标系統及其goldengate manager程序的端口号,還用于定義是否使用壓縮進行傳輸,本例中的compress為壓縮傳輸;

rmttrail:指定寫入到目标斷的哪個隊列;

exttrail:指定寫入到本地的哪個隊列;

sqlexec:在extract程序運作時首先運作一個sql語句;

passthru:禁止extract程序與資料庫互動,适用于data pump傳輸程序;

report:定義自動定時報告;

statoptions:定義每次使用stat時統計數字是否需要重置;

reportcount:報告已經處理的記錄條數統計數字;

tltrace:打開對于資料庫日志的跟蹤日志;

discardfile:定義discardfile檔案位置,如果進行中油記錄出錯會寫入到此檔案中;

dboptions:指定對于某種特定資料庫所需要的特殊參數;

tranlogoptions:指定在解析資料庫日志時所需要的特殊參數,例如:對于裸裝置,可能需要加入以下參數 rawdeviceoggset 0

warnlongtrans:指定對于超過一定時間的長交易可以在gsserr.log裡面寫入警告資訊,本處配置為每隔3分鐘檢查一次場交易,對于超過2小時的進行警告;

因為是異構關系,是以define兩個資料庫之間表的關系映射,是必不可少的。

配置define檔案:

并将生成的/u01/app/oracle/product/11.2.0/ogg12c/dirdef/chen.student傳到目的端的相應目錄中去

scp /u01/app/oracle/product/11.2.0/ogg12c/dirdef/chen.student 192.168.0.25:/ogg/dirdef/

注意:mysql端的ogg我是裝在oracle使用者下的。

目的端配置大緻分為如下三個步驟:配置mgr,配置checkpoint table,配置應用程序

在目的端先建立一張表,記得帶主鍵:

mysql建立表:

<code>mysql&gt; show tables;tables_in_chenstudent</code>

<code>ggsci (oggtest) 2&gt; info error: invalid command.</code>

<code>ggsci (oggtest) 3&gt; info all</code>

配置checkpoint table:

配置應用程序:

<code>replicat rep3 sourcedefs /ogg/dirdef/chen.student sourcedb chen,userid root,password 123456 reperror default,discard discardfile /ogg/dirrpt/f,append,megabytes 50 map chen.student,target chen.student;</code>

<code>ggsci (ttmysql dblogin as root) 11&gt; add replicat rep3,exttrail /ogg/dirdat/xs,checkpointtable chen.checkpointtab replicat added.</code>

replicat程序參數配置說明:

assumetargetdefs:假定兩端資料結構一緻使用此參數;

sourcedefs:假定兩端資料結構不一緻,使用此參數指定源端的資料結構定義檔案,該檔案需要由glodengate工具産生。

map:用于指定源端與目标端表的映射關系;

mapexclude:用于使用在map中使用*比對時排除掉指定的表;

reperror:定義出錯以後程序的響應,一般可以定義為兩種:

abend,即一旦出現錯誤即停止複制,此為預設配置;

discard,出現錯誤後繼續複制,隻是把錯誤的資料放到discard檔案中。

sqlexec:在程序運作時首先運作一個sql語句;

grouptransops:将小交易合并成一個大的交易進行送出,減少送出次數,降低系統io消耗。

maxtransops:将大交易拆分,每xx條記錄送出一次。

在目的端啟動rep3程序,在源端啟動ext3和push3程序。

在源端的student表中插入一條資料,看是否在目的端的student表中能看到。

源端程序

源端程序:

<code>manager running extract running ext3 00:00:00 00:00:09 extract running push3 00:00:00 00:00:09 源端插入資料 sql&gt; insert into student values(3,'xinxin');</code>

<code>1 row created.</code>

目标端程序

目标端程序:

<code>manager running replicat running rep3 00:00:00 00:00:03 查詢目标端資料庫</code>

<code></code><code>mysql&gt; select * from student;idname2jia3xinxin</code>

<code>2 rows in set (0.00 sec)</code>

到此完成ogg異構oracle到mysql資料庫準實時資料同步。

本測試主要描述了針對一個測試表異構從oracle到mysql實作準實時同步資料。為了實作資料同步,需要配好ogg的幾個程序。

1、manager管理程序在兩端開啟,監控和重新開機其他程序;配置設定資料存儲和報告錯誤及事件(程序不能run,需要仔細檢視ggserr.log,此日志在ogg安裝目錄下)。

2、extract程序從日志中抓取并傳輸到target端事務資料;extract日志抓取根據參數配置可分為本地和異地。本文檔在源端配置了兩個ext程序,ext3(本地),push3(異地)。

3、server collector程序在target(接受)端接受資料并寫入trail檔案;

4、replicat程序讀取trail檔案,并應用到traget資料庫;

5、trail檔案時gg自己抓捕資訊的檔案,是一個os檔案,存放在./dirdat/下,以xs00000命名,n順序1,2,3…此檔案用完可配置參數自動删除。

sqlpus下執行

create table chen.t1 as select * from chen.student;

alter table chen.t1 add constraint pk_id primary key (id);

配置源端抽取參數:

<code>extract ext3 dynamicresolution userid ggs,password ggs exttrail /u01/app/oracle/product/11.2.0/ogg12c/dirdat/xs table chen.student; table chen.t1;</code>

<code>ggsci (oggtest) 14&gt; view params push3</code>

<code>extract push3 passthru dynamicresolution userid ggs,password ggs rmthost 192.168.0.25,mgrport 7809 rmttrail /ogg/dirdat/xs table chen.student; table chen.t1;</code>

添加需要同步的表:

重新生成異構表的關系映射:

<code>defsfile /u01/app/oracle/product/11.2.0/ogg12c/dirdef/chen.student userid ggs,password ggs table chen.student; table chen.t1;</code>

用指令生成映射(注意因為與原映射檔案名相同,原映射檔案需要改名備份)

[oracle@oggtest ogg12c]$ ./defgen paramfile dirprm/chen.prm

show create table chen.studentg,檢視,改表名就行。

目标端修改複制程序參數:

<code>replicat rep3 sourcedefs /ogg/dirdef/chen.student sourcedb chen,userid root,password 123456 reperror default,discard discardfile /ogg/dirrpt/rep4.dsc,append,megabytes 50 map chen.student,target chen.student; map chen.t1,target chen.t1;</code>

先info all檢視程序情況

目标端:

start mgr

start rep3

源端:

start ext3

start push3

分别檢視ggserr.log,沒有報錯,程序正常

檢視目标端複制情況,因為源端是用create table as select建表,帶有資料,但發現原資料沒有同步到目标端,随後在源端insert的資料能同步到目标端。

第六部分:表資料在源端的導出和目标端的導入

前提:需要在源端配置相關mgr,ext等程序工作正常,涉及到的異構表的轉換檔案準備好,目标端rep程序配置好,但不啟用。(以上設定參考前幾部分的配置)

建測試表和測試sql:

<code>sql&gt; begin</code>

<code>insert into tt01 values(seqt1.nextt01al,200000*dbms_random.value,'nanfang is updating'); commit; insert into tt01 values(seqt1.nextt01al,300000*dbms_random.value,'nanfang is updating'); commit; update tt01 set t2=t2+10 where rownum=1; commit; dbms_lock.sleep(1); end loop; end; /</code>

從源端導出表資料:

<code>add extract einit,sourceistable</code>

<code>/ 以sourceistable選項建立extract /</code>

<code>view params einit extract einit sourceistable userid ggs, password ggs rmthost 59.202.48.46, mgrport 7809 rmtfile /u01/app/gg12c/dirdat/chentt1 table chen.tt1;</code>

<code>/ 之後我們需要在指令行界面下調用該extract / extract paramfile dirprm/einit.prm 3.在目标端導入資料 首先需要在mysql下建表 use chen create table tt01 (t1 int primary key,t2 int,t3 varchar(30)); / 接下來我們要在target上配置initial load使用的replicat,以導入之前的extfile / ggsci下操作 add replicat rinit,specialrun</code>

<code>異構導入的參數配置 edit params rinit replicat rinit specialrun sourcedefs /u01/app/gg12c/dirdef/chen.tt1 sourcedb chen,userid root,password 123456 extfile /u01/app/gg12c/dirdat/chentt1 assumetargetdefs map chen.tt1, target chen.tt1;</code>

<code>/ 之後我們在指令行上調用該replicat / os $gghome下操作 replicat paramfile dirprm/rinit.prm</code>

利用ogg将原表資料的導出,再異構導入,後啟動複制程序,追同步日志,這一原理能實作線上oracle到mysql的資料遷移。