Oracle GoldenGate 详解
转自:https://www.cnblogs.com/rangle/p/8987063.html一、Oracle GoldenGate介绍GoldenGate软件是一种基于日志的结构化数据复制软件。GoldenGate 能够实现大量交易数据的实时捕捉、变换和投递,实现源数据库与目标数据库的数据同步,保持亚秒级的数据延迟。1、应用场景1)高可用容灾2)数据库迁移、升级(支持跨版本、异构数据库、零宕机时间、亚秒级恢复)3)实时数据集成(支持异构数据库、多源数据库)2、常用拓扑(下图来自网络)
3、支持的平台和数据库我这边几年前做过oracle到mysql的同步,也作过mysql到mysql的同步(支持不是很好,而且有DDL和DML的限制、字段类型限制等)(下图来自网络)
4、OGG同步原理源端通过抽取进程提取redo log或archive log日志内容,通过pump进程(TCP/IP协议)发送到目标端,最后目标端的rep进程接收日志、解析并应用到目标端,进而完成数据同步。
5、OGG相关进程GoldenGate主要包含Manager进程、Extract进程、Pump进程、Replicat进程1)Manager进程不管是源端还是目标端必须并且只能有一个Manager进程,可以启动、关闭、监控其他进程的健康状态,报告错误事件、分配数据存储空间,发布阀值报告等。2)Extract 进程运行在数据库源端,负责从源端数据表或日志中捕获数据,然后捕获到的将写到本地trail文件。 想要复制的对象数据发生改变时,Extract进程 就会进行捕捉,当事务提交时,所有和该事务相关的日志记录被以事务为单元顺序的记录到trail文件中。Extract进程利用其内在的checkpoint机制,周期性的记录其读写的位置,实现断点同步。Extract其实有两种捕获模式A.传统模式抽取方式:基于在线日志或归档日志进行抽取
B.Integrated模式( Oracle Goldengate 11g版本)抽取方式: Logmining server负责以LCR格式从数据库日志中捕获数据变化,extract进程再将这些数据存入 trail文件。
传统模式是大家非常熟悉的模式啦,平时搭建的都是传统模式。但是Integrated模式是Oracle Goldengate 11g版本才出现的,很多人会比较陌生,有兴趣的朋友也可以去了解一番。3)Pump进程(可选)运行在数据库源端, 其作用是将源端产生的本地trail文件,把trail以数据块的形式通过TCP/IP 协议发送到目标端。但是如果如果不使用trail文件,那么extract进程在抽取完数据以后,直接投递到目标端,生成远程trail文件,那么这时候,Pump进程就可以不用配置(存在)了。4)Replicat进程运行在数据库目标端 ,负责读取源端传送到目标端的trail文件中的内容,并将其解析为DML或 DDL语句, 然后应用到目标数据库中。Replicat两种模式A.传统模式:基于sql交付,最常用的一种方式
B.Integrated模式(OGG12c)对应这种模式,官方文档是这样描述的Replicat 操作的过程如下:读取源端传输过来的 trail 文件;执行data 数据过滤和转换操作;DML操作:根据commit顺序,构造LCR;DDL 操作:Replicat 进程直接apply;Replicat 通过lightweight streaming 接口和目标库后台进程(Database inbound server)建立 连接;将LCR传输到inbound server,然后apply 这些数据到目标数据库。
两个Apply Servers的Integrated模式
其实对于进程的Capture and Apply Modes可以参考一下官方文档:http://docs.oracle.com/goldengate/1212/gg-winux/GIORA/process_mode.htm#GIORA5545)Server Collector进程运行于目标端,作用就是把Extract/Pump进程投递过来的数据块重新组装成Trail文件。运行期间无需任何配置。是一个完全自动的进程好了对GoldenGate的进程介绍到处就告一段落了。6、OGG相关目录dirbdbdirchk:检查点文件,记录了该进程的检查点信息dircrddirdat:trail日志文件,存放收取接手的日志文件dirdef:用来存放通过DEFGEN工具生成的源或目标端数据定义文件dirdmpdiroutdirpcs:用来存放进程状态文件dirprm:用来存放参数文件,该进程所配置的参数(edit param 进程组名 就是配置该文件)dirrpt:用来存放进程报告(report)文件,可以查看该进程运行时的报错信息等(view report 进程组名 就是看该文件)dirsql:用来存放SQL脚本文件dirtmp:当事物所需要的内存超过已分配内存时,缺省存在此目录dirwltdirwww二、Oracle GoldenGate安装1、安装环境源端目标端IP地址10.20.11.17610.20.32.23操作系统RHEL6.6_X64RHEL6.6_X64数据库ORACLE_11.2.0.4ORACLE_11.2.0.4OGGVersion 12.2.0.1.1Version 12.2.0.1.12、OGG下载http://www.oracle.com/technetwork/middleware/goldengate/downloads/index.htmlhttp://download.oracle.com/otn/goldengate/12301/123012_fbo_ggs_Linux_x64_shiphome.zip3、OGG安装1)源端:tnsname配置开启归档等
archive list supplemental data; supplemental_log_data_min v$; force logging; (不是必须);备注: 在oracle中可以通过rowid来定位某条记录,但是目标端的数据库和源端数据库的数据库可能完全不一样,所以无法通过rowid来确定源端数据库的逻辑变化,这时附加日志supplemental log便登上了表演的舞台。数据库在开启附加日志功能后,对于源端的修改操作,oracle会同时追加能够唯一标示记录的列到redo log。这样目标端数据库就可以知道源端发生了哪些具体的变化。
ogg安装(略)2)目标端:tnsname配置开启归档等ogg安装
[root@node1 oracle]# unzip 123012_fbo_ggs_Linux_x64_shiphome.zip [root@node1 oracle]# mv fbo_ggs_Linux_x64_shiphome /home/oracle/ogg_install[root@node1 oracle]# chown -R oracle:oinstall /home/oracle/ogg_install[oracle@node1 response]$ vi /home/oracle/ogg_install/Disk1/response/oggcore.rsp INSTALL_OPTION=ORA11gSOFTWARE_LOCATION=/home/oracle/ogg_installSTART_MANAGER=falseMANAGER_PORT=DATABASE_LOCATION=INVENTORY_LOCATION=/home/oracle/ogg_install/ogg_logUNIX_GROUP_NAME=oinstall[oracle@node1 Disk1]$ ./runInstaller -silent -responseFile /home/oracle/ogg_install/Disk1/response/oggcore.rsp
至此OGG软件安装成功,为方便管理OGG可以设置环境变量[oracle@node1 ogg_install]$ vi ~/.bash_profilePATH=$PATH:$HOME/bin:/home/oracle/ogg_install[oracle@node1 ogg_install]$ source ~/.bash_profile
[oracle@node1 ~]$ ggsciOracle GoldenGate Command Interpreter for OracleVersion 12.3.0.1.2 OGGCORE_12.3.0.1.0_PLATFORMS_171208.0005_FBOLinux, x64, 64bit (optimized), Oracle 11g on Dec 8 2017 21:13:00Operating system character set identified as UTF-8.Copyright (C) 1995, 2017, Oracle and/or its affiliates. All rights reserved.GGSCI (node1) 1> create subdirs Creating subdirectories under current directory /home/oracleParameter file /home/oracle/ogg_install/dirprm: created.Report file /home/oracle/ogg_install/dirrpt: created.Checkpoint file /home/oracle/ogg_install/dirchk: created.Process status files /home/oracle/ogg_install/dirpcs: created.SQL script files /home/oracle/ogg_install/dirsql: created.Database definitions files /home/oracle/ogg_install/dirdef: created.Extract data files /home/oracle/ogg_install/dirdat: created.Temporary files /home/oracle/ogg_install/dirtmp: created.Credential store files /home/oracle/ogg_install/dircrd: created.Masterkey wallet files /home/oracle/ogg_install/dirwlt: created.Dump files /home/oracle/ogg_install/dirdmp: created.
至此真正的OGG安装完毕,可以进行下面的数据同步部署了。三、Oracle GoldenGate数据同步1、源端配置1)创建数据同步用户、表空间
SQL> create tablespace ogg_tbs datafile '/U01/app/oracle/oradata/testdb/ogg_data.dbf' size 30M autoextend on next 10M;Tablespace created.SQL> create user ogguser identified by "ogguser";User created.SQL> alter user ogguser default tablespace ogg_tbs;User altered.SQL> grant create session to ogguser;Grant succeeded.SQL> grant dba to ogguser;Grant succeeded.
2)更改相关数据库配置归档检查和更改:
SQL> archive log listDatabase log mode Archive ModeAutomatic archival EnabledArchive destination /oracle/archivelogOldest online log sequence 180Next log sequence to archive 181Current log sequence 181若处于非归档模式,则改为归档模式:SQL> shutdown immediateDatabase closed.Database dismounted.ORACLE instance shut down.SQL> startup mount;ORACLE instance started.SQL> alter database archivelog;Database altered.SQL> alter database open;Database altered.
force_logging开启
SQL> select force_logging from v$database;FOR---NOSQL> alter database force logging;Database altered.SQL> select force_logging from v$database;FOR---YES
supplemental log开启
SQL supplemental_log_data_min v$;SUPPLEMENOSQL supplemental data; altered.##切换日志,使更改生效SQL system switch logfile;System altered.SQL supplemental_log_data_min v$;SUPPLEMEYES
关闭回收站(10g需要,11g可以不关闭)
SQL> show parameter recyclebinNAME TYPE VALUE------------------------------------ ----------- ------------------------------recyclebin string onSQL> alter system set recyclebin=off;System altered.SQL> show parameter recyclebinNAME TYPE VALUE------------------------------------ -------------------------------recyclebin string OFF
3)配置manager进程GGSCI (dg) 1> edit params mgr
port 7809DYNAMICPORTLIST 7810-7880--AUTORESTART ER *,RETRIES 5,WAITMINUTES 7PURGEOLDEXTRACTS ./dirdat/*,usecheckpoints, minkeepdays 7LAGREPORTHOURS 1LAGINFOMINUTES 30LAGCRITICALMINUTES 45
MANAGER进程参数配置说明:PORT:指定服务监听端口;这里以7839为例,默认端口为7809DYNAMICPORTLIST:动态端口:可以制定最大256个可用端口的动态列表,当指定的端口不可用时,管理进程将会从列表中选择一个可用的端口,源端和目标段的Collector、Replicat、GGSCI进程通信也会使用这些端口;COMMENT:注释行,也可以用--来代替;AUTOSTART:指定在管理进程启动时自动启动哪些进程;AUTORESTART:自动重启参数设置:本处设置表示每7分钟尝试重新启动所有EXTRACT进程,共尝试5次;PURGEOLDEXTRACTS:定期清理trail文件设置:本处设置表示对于超过7天的trail文件进行删除。LAGREPORT、LAGINFO、LAGCRITICAL:定义数据延迟的预警机制:本处设置表示MGR进程每隔1小时检查EXTRACT的延迟情况,如果超过了30分钟就把延迟作为信息记录到错误日志中,如果延迟超过了45分钟,则把它作为警告写到错误日志中。
4)配置extract进程ogg 12版本可创建用户别名:
[oracle@dg ogg]$ ggsci GGSCI (dg) 3> add credentialstoreCredential store created in ./dircrd/.GGSCI (dg) 4> alter credentialstore add user gguser alias ogguserPassword: Credential store in ./dircrd/ altered.--这里就可以使用别名登录 (正常用户名密码 dblogin userid ggs,password ggs)GGSCI (dg) 3> dblogin useridalias ogguserSuccessfully logged into database.
GGSCI (dg) extract EXT01,tranlog, nowEXTRACT added.###可以通过命令add extract EXT01,tranlog,begin 2018-05-06 08:05:14,制定开始抽取的日志时间GGSCI (customerdg) edit params EXT01##extract配置文件内容如下EXTRACT ext01SETENV (ORACLE_HOME"U01apporacleproduct.")setenv (NLS_LANG"AMERICAN_AMERICA.AL32UTF8")setenv (ORACLE_SID"testdb")useridalias ogguserGETTRUNCATESREPORTCOUNT EVERY MINUTES, RATEDISCARDFILE .dirrptext01.dsc,APPEND,MEGABYTES WARNLONGTRANS 2h,CHECKINTERVAL 10mEXTTRAIL .dirdatexTRANLOGOPTIONS EXCLUDEUSER gguserTRANLOGOPTIONS MINEFROMACTIVEDGDBOPTIONS ALLOWUNUSEDCOLUMNDYNAMICRESOLUTIONFETCHOPTIONS FETCHPKUPDATECOLS CUSTOMER.t_t1; CUSTOMER.t_t2; CUSTOMER.t_t3;
添加该extract对应的trail文件,用来存储抽取的数据。单个文件大小设置为100MBGGSCI (dg) exttrail ,extract ext01 MEGABYTES EXTTRAIL added.5)配置pump进程为了避免primary extract受到网络的影响,我们在源端和目标端之间增加一个data pump,这样的话,primary extract负责将数据从源数据中抽取出来,存在本地的trail文件中,然后data pump进程负责将本地trail文件中的数据传输到目标端的trail文件里。这样能提高更高的灵活性和可用性(当源和目标端之间的网络出现故障时,primary extract会继续抽取数据存到本地的trail中)
GGSCI (dg) 5> edit params pump01EXTRACT pump01RMTHOST 10.20.32.23, MGRPORT 7809, compressPASSTHRURMTTRAIL /home/oracle/ogg_install/dirdat/rtDYNAMICRESOLUTION--tabletable CUSTOMER.t_t1;table CUSTOMER.t_t2;table CUSTOMER.t_t3;GGSCI (dg) 14> add extract pump01,exttrailsource ./dirdat/exEXTRACT added.GGSCI (dg) 15> add rmttrail /home/oracle/ogg_install/dirdat/rt,extract pump01RMTTRAIL added.
2、目标端配置1)创建数据同步用户、表空间
SQL> create tablespace ogg_tbs datafile '/U01/app/oracle/oradata/testdb/ogg_data.dbf' size 30M autoextend on next 10M;Tablespace created.SQL> create user ogguser identified by "ogguser";User created.SQL> alter user ogguser default tablespace ogg_tbs;User altered.SQL> grant create session to ogguser;Grant succeeded.SQL> grant dba to ogguser;Grant succeeded.
2)归档开启3)配置manager进程
GGSCI (node1) 1> add credentialstoreCredential store created.GGSCI (node1) 2> alter credentialstore add user ogguser alias ogguserPassword: Credential store altered.GGSCI (node1) 3> dblogin useridalias ogguserSuccessfully logged into database.
GGSCI (node1 as ogguser@testdb) 4> edit params mgr
port 7809DYNAMICPORTLIST 7810-7880--AUTORESTART ER *,RETRIES 5,WAITMINUTES 7PURGEOLDEXTRACTS ./dirdat/*,usecheckpoints, minkeepdays 7LAGREPORTHOURS 1LAGINFOMINUTES 30LAGCRITICALMINUTES 45
GGSCI (node1) 1> start mgrManager started.GGSCI (node1) 2> info allProgram Status Group Lag at Chkpt Time Since ChkptMANAGER RUNNING
4)配置replicate进程
REPLICAT rep01SETENV (ORACLE_HOME="/U01/app/oracle/product/11.2.0.4")setenv (NLS_LANG="AMERICAN_AMERICA.AL32UTF8")setenv (ORACLE_SID="testdb")useridalias ogguserREPORT AT 01:59REPORTCOUNT EVERY 30 MINUTES, RATEREPERROR DEFAULT, ABENDHANDLECOLLISIONSassumetargetdefsDISCARDFILE ./dirrpt/rep01.dsc, APPEND, MEGABYTES 1000GETTRUNCATESALLOWNOOPUPDATES--tablemap gguser.t_t1, target gguser.t_t1; map gguser.t_t2, target gguser.t_t2; map gguser.t_t3, target gguser.t_t3;
5)、添加checkpoint表GGSCI (node1) 5> dblogin useridalias ogguserSuccessfully logged into database.GGSCI (node1 as ogguser@testdb) 6> add checkpointtable ogguser.checkpointtabSuccessfully created checkpoint table ogguser.checkpointtab.GGSCI (node1 as ogguser@testdb) 7> add replicat rep01,exttrail /home/oracle/ogg_install/dirdat/rt,checkpointtable ogguser.checkpointtabREPLICAT added.3、源端进程启动1)源端启动mgr进程、extract进程、pump进程
GGSCI (dg) 20> start mgrManager started.GGSCI (dg as gguser@testdb) 9> start ext01Sending START request to MANAGER ...EXTRACT EXT01 startingGGSCI (dg as gguser@testdb) 12> start PUMP01Sending START request to MANAGER ...EXTRACT PUMP01 startingGGSCI (dg as gguser@testdb) 14> info allProgram Status Group Lag at Chkpt Time Since ChkptMANAGER RUNNING EXTRACT RUNNING EXT01 00:00:03 00:00:03 EXTRACT RUNNING PUMP01 00:00:00 00:00:08
4、数据库初始化1)源端导出数据##创建导出文件目录、查看到处点SCN
select * from dba_directories; create directory DATA_PUMP_DIR as '/U01/oracle/oggdump';col CURRENT_SCN format 999999999999999999999999;set line 200;set pagesize 20000;select current_scn from v$database;select dbms_flashback.get_system_change_number current_scn from dual;
##导出表数据
##只导出数据expdp \ directoryDATA_PUMP_DIR .dmp tablesogguser.t_t1,ogguser.t_t2,ogguser.t3 grantsn none triggersn compressn contentdata_only FLASHBACK_SCN expdp.##只导出表结构expdp \##导出表结构和数据expdp \
2)目标端数据导入##若表结构事前没有创建好,需要表结构和表数据一起导入,若表结构已创建,只需要导入表数据即可
##导入表数据impdp \'/ as sysdba\' DIRECTORY=DATA_PUMP_DIR DUMPFILE=20180506.dmp REMAP_SCHEMA=source_user:target_user content=data_only LOGFILE=impdp.log ##导入表结构impdp \'/ as sysdba\' DIRECTORY=DATA_PUMP_DIR DUMPFILE=20180506.dmp REMAP_SCHEMA=source_user:target_user content=METADATA_ONLY LOGFILE=impdp.log ##导入表结构和数据impdp \'/ as sysdba\' DIRECTORY=DATA_PUMP_DIR DUMPFILE=20180506.dmp REMAP_SCHEMA=source_user:target_user LOGFILE=impdp.log
5、目标端进程启动目标端启动mgr、replicate进程
GGSCI (node1 as ogguser@testdb) 9> start mgrManager started.GGSCI (node1) 2> start rep01,aftercsn 54995990597Sending START request to MANAGER ...REPLICAT REP01 startingGGSCI (node1) 4> info allProgram Status Group Lag at Chkpt Time Since ChkptMANAGER RUNNING REPLICAT RUNNING REP01 00:00:00 00:00:00
四、Oracle GoldenGate管理1、进程启动、关闭以及状态查看启动:
GGSCI (node1) start mgrManager started.##可以通过SCN启动start rep01,aftercsn 54995990597##可以通过时间启动add extract EXT01,tranlog,begin 2018-05-06 08:05:14
关闭:
GGSCI (node1) 3> stop mgrManager process is required by other GGS processes.Are you sure you want to stop it (y/n)?yesSending STOP request to MANAGER ...Request processed.Manager stopped.
状态查看:
GGSCI (node1) 1> info allProgram Status Group Lag at Chkpt Time Since ChkptMANAGER RUNNING REPLICAT STOPPED REP01 00:00:00 00:14:29 GGSCI (node1) 2> info rep01REPLICAT REP01 Initialized 2018-05-04 16:29 Status STOPPEDCheckpoint Lag 00:00:00 (updated 00:14:51 ago)Log Read Checkpoint File /home/oracle/ogg_install/dirdat/rt000000000 First Record RBA 0
2、进程添加、编辑、删除、配置文件查看进程文件添加:
##抽取进程 extract ext5,tranlog, now exttrail usrlocalmysqloggdirdatex,extract ext5##pump进程 extract pump01,exttrailsource usrlocalmysqloggdirdatex rmttrail oracleappoggdirdatpt,extract pump5##检查点 checkpointtable ogg.checkpointtab replicat rep2,exttrail oracleappoggdirdatpt,checkpointtable ogg.checkpointtab##别名
文件编辑:
##管理进程文件edit params mgr##抽取进程文件edit params ext01##传输进程文件edit params pump01##应用进程文件edit params rep01
文件查看:
##管理进程文件view params mgr##抽取进程文件view params ext01##传输进程文件view params pump01##应用进程文件view params rep01
文件删除:
GGSCI (node1) 5> dblogin useridalias ogguserSuccessfully logged into database.GGSCI (node1 ogguser) checkpointtable ogguser.checkpointtabThis may be required other installations. Are you sure you want this ? yesSuccessfully deleted ogguser.checkpointtab.GGSCI (node1 ogguser) rep01Deleted REPLICAT REP01.
3、SCN和时间互转to_char(scn_to_timestamp(),) dual;timestamp_to_scn(to_timestamp(,)) dual;