一、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相关进程
6、OGG相关目录
dirbdb
dirchk:检查点文件,记录了该进程的检查点信息
dircrd
dirdat:trail日志文件,存放收取接手的日志文件
dirdef:用来存放通过DEFGEN工具生成的源或目标端数据定义文件
dirdmp
dirout
dirpcs:用来存放进程状态文件
dirprm:用来存放参数文件,该进程所配置的参数(edit param 进程组名 就是配置该文件)
dirrpt:用来存放进程报告(report)文件,可以查看该进程运行时的报错信息等(view report 进程组名 就是看该文件)
dirsql:用来存放SQL脚本文件
dirtmp:当事物所需要的内存超过已分配内存时,缺省存在此目录
dirwlt
dirwww
二、Oracle GoldenGate安装
1、安装环境
源端 |
目标端 |
|
IP地址 |
10.20.11.176 |
10.20.32.23 |
操作系统 |
RHEL6.6_X64 |
RHEL6.6_X64 |
数据库 |
ORACLE_11.2.0.4 |
ORACLE_11.2.0.4 |
OGG |
Version 12.2.0.1.1 |
Version 12.2.0.1.1 |
2、OGG下载
http://www.oracle.com/technetwork/middleware/goldengate/downloads/index.html
http://download.oracle.com/otn/goldengate/12301/123012_fbo_ggs_Linux_x64_shiphome.zip
3、OGG安装
1)源端:
tnsname配置
开启归档等
archive log list alter database add supplemental log data; select supplemental_log_data_min from v$database; alter database force logging; (不是必须)
alter system set recyclebin=off;(更好的支持DDL)
show parameter recyclebin;
备注: 在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=ORA11g SOFTWARE_LOCATION=/home/oracle/ogg_install START_MANAGER=false MANAGER_PORT= DATABASE_LOCATION= INVENTORY_LOCATION=/home/oracle/ogg_install/ogg_log UNIX_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_profile
PATH=$PATH:$HOME/bin:/home/oracle/ogg_install
[oracle@node1 ogg_install]$ source ~/.bash_profile
[oracle@node1 ~]$ ggsci Oracle GoldenGate Command Interpreter for Oracle Version 12.3.0.1.2 OGGCORE_12.3.0.1.0_PLATFORMS_171208.0005_FBO Linux, x64, 64bit (optimized), Oracle 11g on Dec 8 2017 21:13:00 Operating 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/oracle Parameter 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 list Database log mode Archive Mode Automatic archival Enabled Archive destination /oracle/archivelog Oldest online log sequence 180 Next log sequence to archive 181 Current log sequence 181 若处于非归档模式,则改为归档模式: SQL> shutdown immediate Database 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 --- NO SQL> alter database force logging; Database altered. SQL> select force_logging from v$database; FOR --- YES
supplemental log开启
SQL> select supplemental_log_data_min from v$database; SUPPLEME -------- NO
SQL> alter database add supplemental log data; Database altered.
##切换日志,使更改生效
SQL> alter system switch logfile; System altered.
SQL> select supplemental_log_data_min from v$database; SUPPLEME -------- YES
关闭回收站(10g需要,11g可以不关闭)
SQL> show parameter recyclebin NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ recyclebin string on SQL> alter system set recyclebin=off; System altered. SQL> show parameter recyclebin NAME TYPE VALUE ------------------------------------ ------------------------------- recyclebin string OFF
3)配置manager进程
GGSCI (dg) 1> edit params mgr
port 7809 DYNAMICPORTLIST 7810-7880 --AUTORESTART ER *,RETRIES 5,WAITMINUTES 7 PURGEOLDEXTRACTS ./dirdat/*,usecheckpoints, minkeepdays 7 LAGREPORTHOURS 1 LAGINFOMINUTES 30 LAGCRITICALMINUTES 45
MANAGER进程参数配置说明: PORT:指定服务监听端口;这里以7839为例,默认端口为7809 DYNAMICPORTLIST:动态端口:可以制定最大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 credentialstore Credential store created in ./dircrd/. GGSCI (dg) 4> alter credentialstore add user gguser alias ogguser Password: Credential store in ./dircrd/ altered. --这里就可以使用别名登录 (正常用户名密码 dblogin userid ggs,password ggs) GGSCI (dg) 3> dblogin useridalias ogguser Successfully logged into database.
GGSCI (dg) 5> add extract EXT01,tranlog,begin now EXTRACT added.
###可以通过命令add extract EXT01,tranlog,begin 2018-05-06 08:05:14,制定开始抽取的日志时间
GGSCI (customerdg) 6> edit params EXT01
##extract配置文件内容如下
EXTRACT ext01 SETENV (ORACLE_HOME="/U01/app/oracle/product/11.2.0.4") setenv (NLS_LANG="AMERICAN_AMERICA.AL32UTF8") setenv (ORACLE_SID="testdb") useridalias ogguser GETTRUNCATES REPORTCOUNT EVERY 1 MINUTES, RATE DISCARDFILE ./dirrpt/ext01.dsc,APPEND,MEGABYTES 1000 WARNLONGTRANS 2h,CHECKINTERVAL 10m EXTTRAIL ./dirdat/ex TRANLOGOPTIONS EXCLUDEUSER gguser TRANLOGOPTIONS MINEFROMACTIVEDG DBOPTIONS ALLOWUNUSEDCOLUMN DYNAMICRESOLUTION FETCHOPTIONS FETCHPKUPDATECOLS --table table CUSTOMER.t_t1; table CUSTOMER.t_t2; table CUSTOMER.t_t3;
添加该extract对应的trail文件,用来存储抽取的数据。单个文件大小设置为100MB
GGSCI (dg) 1> add exttrail /home/oracle/ogg_install/dirdat/ex,extract ext01 MEGABYTES 100
EXTTRAIL added.
5)配置pump进程
为了避免primary extract受到网络的影响,我们在源端和目标端之间增加一个data pump,这样的话,primary extract负责将数据从源数据中抽取出来,存在本地的trail文件中,然后data pump进程负责将本地trail文件中的数据传输到目标端的trail文件里。这样能提高更高的灵活性和可用性(当源和目标端之间的网络出现故障时,primary extract会继续抽取数据存到本地的trail中)
GGSCI (dg) 5> edit params pump01 EXTRACT pump01 RMTHOST 10.20.32.23, MGRPORT 7809, compress PASSTHRU RMTTRAIL /home/oracle/ogg_install/dirdat/rt DYNAMICRESOLUTION --table table CUSTOMER.t_t1; table CUSTOMER.t_t2; table CUSTOMER.t_t3; GGSCI (dg) 14> add extract pump01,exttrailsource ./dirdat/ex EXTRACT added. GGSCI (dg) 15> add rmttrail /home/oracle/ogg_install/dirdat/rt,extract pump01 RMTTRAIL 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 credentialstore Credential store created. GGSCI (node1) 2> alter credentialstore add user ogguser alias ogguser Password: Credential store altered. GGSCI (node1) 3> dblogin useridalias ogguser Successfully logged into database.
GGSCI (node1 as ogguser@testdb) 4> edit params mgr
port 7809 DYNAMICPORTLIST 7810-7880 --AUTORESTART ER *,RETRIES 5,WAITMINUTES 7 PURGEOLDEXTRACTS ./dirdat/*,usecheckpoints, minkeepdays 7 LAGREPORTHOURS 1 LAGINFOMINUTES 30 LAGCRITICALMINUTES 45
GGSCI (node1) 1> start mgr Manager started. GGSCI (node1) 2> info all Program Status Group Lag at Chkpt Time Since Chkpt MANAGER RUNNING
4)配置replicate进程
REPLICAT rep01 SETENV (ORACLE_HOME="/U01/app/oracle/product/11.2.0.4") setenv (NLS_LANG="AMERICAN_AMERICA.AL32UTF8") setenv (ORACLE_SID="testdb") useridalias ogguser REPORT AT 01:59 REPORTCOUNT EVERY 30 MINUTES, RATE REPERROR DEFAULT, ABEND HANDLECOLLISIONS assumetargetdefs DISCARDFILE ./dirrpt/rep01.dsc, APPEND, MEGABYTES 1000 GETTRUNCATES ALLOWNOOPUPDATES --table map 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 ogguser Successfully logged into database. GGSCI (node1 as ogguser@testdb) 6> add checkpointtable ogguser.checkpointtab Successfully created checkpoint table ogguser.checkpointtab.
GGSCI (node1 as ogguser@testdb) 7> add replicat rep01,exttrail /home/oracle/ogg_install/dirdat/rt,checkpointtable ogguser.checkpointtab REPLICAT added.
3、源端进程启动
1)源端启动mgr进程、extract进程、pump进程
GGSCI (dg) 20> start mgr Manager started. GGSCI (dg as gguser@testdb) 9> start ext01 Sending START request to MANAGER ... EXTRACT EXT01 starting GGSCI (dg as gguser@testdb) 12> start PUMP01 Sending START request to MANAGER ... EXTRACT PUMP01 starting GGSCI (dg as gguser@testdb) 14> info all Program Status Group Lag at Chkpt Time Since Chkpt MANAGER 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 \'/ as sysdba\' directory=DATA_PUMP_DIR file=20180506.dmp tables=ogguser.t_t1,ogguser.t_t2,ogguser.t3 grants=n statistics=none triggers=n compress=n content=data_only FLASHBACK_SCN=54995990597 log=expdp.log
##只导出表结构
expdp \'/ as sysdba\' directory=DATA_PUMP_DIR file=20180506.dmp tables=ogguser.t_t1,ogguser.t_t2,ogguser.t3 grants=n statistics=none triggers=n compress=n content=METADATA_ONLY FLASHBACK_SCN=54995990597 log=expdp.log
##导出表结构和数据
expdp \'/ as sysdba\' directory=DATA_PUMP_DIR file=20180506.dmp tables=ogguser.t_t1,ogguser.t_t2,ogguser.t3 grants=n statistics=none triggers=n compress=n FLASHBACK_SCN=54995990597 log=expdp.log
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 mgr Manager started. GGSCI (node1) 2> start rep01,aftercsn 54995990597 Sending START request to MANAGER ... REPLICAT REP01 starting GGSCI (node1) 4> info all Program Status Group Lag at Chkpt Time Since Chkpt MANAGER RUNNING REPLICAT RUNNING REP01 00:00:00 00:00:00
四、Oracle GoldenGate管理
1、进程启动、关闭以及状态查看
启动:
GGSCI (node1) 1> start mgr Manager started.
##可以通过SCN启动
start rep01,aftercsn 54995990597
##可以通过时间启动
add extract EXT01,tranlog,begin 2018-05-06 08:05:14
关闭:
GGSCI (node1) 3> stop mgr Manager process is required by other GGS processes. Are you sure you want to stop it (y/n)?yes Sending STOP request to MANAGER ... Request processed. Manager stopped.
状态查看:
GGSCI (node1) 1> info all Program Status Group Lag at Chkpt Time Since Chkpt MANAGER RUNNING REPLICAT STOPPED REP01 00:00:00 00:14:29 GGSCI (node1) 2> info rep01 REPLICAT REP01 Initialized 2018-05-04 16:29 Status STOPPED Checkpoint Lag 00:00:00 (updated 00:14:51 ago) Log Read Checkpoint File /home/oracle/ogg_install/dirdat/rt000000000 First Record RBA 0
2、进程添加、编辑、删除、配置文件查看
进程文件添加:
##抽取进程 add extract ext5,tranlog,begin now add exttrail /usr/local/mysql/ogg/dirdat/ex,extract ext5 ##pump进程 add extract pump01,exttrailsource /usr/local/mysql/ogg/dirdat/ex add rmttrail /oracle/app/ogg/dirdat/pt,extract pump5 ##检查点 add checkpointtable ogg.checkpointtab add replicat rep2,exttrail /oracle/app/ogg/dirdat/pt,checkpointtable ogg.checkpointtab ##别名
add credentialstore alter credentialstore add user ogguser alias ogguser
文件编辑:
##管理进程文件
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 ogguser
Successfully logged into database.
GGSCI (node1 as ogguser@testdb) 8> delete checkpointtable ogguser.checkpointtab This checkpoint table may be required for other installations. Are you sure you want to delete this checkpoint table? yes Successfully deleted checkpoint table ogguser.checkpointtab. GGSCI (node1 as ogguser@testdb) 9> delete rep01 Deleted REPLICAT REP01.
3、SCN和时间互转
##SCN转为时间戳
select to_char(scn_to_timestamp(54995990597),'yyyy-mm-dd hh24:mi:ss') from dual;
##时间戳转为为SCN
select timestamp_to_scn(to_timestamp('2018-05-06 08:05:14','yyyy-mm-dd hh24:mi:ss')) from dual;