ogg oracle 到mysql数据同步 安装及配置

因缘巧合需要把oracle数据同步到mysql中,看了很多人写的ogg方案,都或多或少有些小错误,因此自己整理一篇。

目标: mysql数据库test,需要使用oracle数据库中的student表的数据(owner:cms_cluster)
操作: 使用goldengate来完成oracle数据到mysql数据库的同步

源:centos6.3 + oracle11.2 + goldengate12.3
目标:centos7.3 + mysql5.7 + goldengate12.3

安装

ogg软件下载地址:http://www.oracle.com/technetwork/cn/middleware/goldengate/downloads/index.html
https://www.oracle.com/middleware/technologies/goldengate-downloads.html
选择适合自己环境的ogg,例如下载:
适用于 Linux x86-64 上 Oracle 的 Oracle GoldenGate 12.3.0.1.4
适用于 Linux x86-64 上与 MySQL兼容的数据库的 Oracle GoldenGate 12.3.0.1.5

1 源端

1.1 oracle数据库操作

(1)开启归档日志

sqlplus / as sysdba

SQL> alter system set log_archive_dest_1='location=/u01/archivelog';
SQL> shutdown immediate
SQL> startup mount
SQL> alter database archivelog;
SQL> archive log list;
Database log mode             Archive Mode
Automatic archival            Enabled
Archive destination           /u01/arch
SQL> alter database open;

注:关闭附加日志命令:alter database noarchivelog;

(2)开启附加日志

SQL> select supplemental_log_data_min from v$database;
     SUPPLEME
   ---------------
        NO
SQL> alter database add supplemental log data;
SQL> select supplemental_log_data_min from v$database;
       SUPPLEME
   ---------------
        YES

(3)开启强制写日志(可以不开启,最好开启)

SQL> select force_logging from v$database;
        FOR
        ------
        NO
SQL> alter database force logging;
SQL> select force_logging from v$database;
        FOR
        ------
        YES

(4)创建用户并授权

SQL> create tablespace ggtbs datafile '/u01/oradata/db1/ggtbs01.dbf' size 200m autoextend on;
SQL> create user ggs identified by ggs default tablespace ggtbs quota unlimited on ggtbs;
SQL> grant connect,resource,create session,alter session to ggs;
SQL> grant select any dictionary,select any table, alter any table, create any table,flashback any table to ggs;
SQL> grant execute on dbms_flashback to ggs;

注: 创建用户ggs,密码ggs,可以赋予dba权限

1.2 GoldenGate源端的安装

注:以oracle 身份登录完成安装

(1)解压安装包

/home/oracle/ogg_install
unzip 123012_fbo_ggs_Linux_x64_shiphome.zip 

(2)静默安装

#修改oggcore.rsp
vim /home/oracle/ogg_install/fbo_ggs_Linux_x64_shiphome/Disk1/response/oggcore.rsp

INSTALL_OPTION=ORA11g
SOFTWARE_LOCATION=/home/oracle/ogg
START_MANAGER=false
MANAGER_PORT=1530
DATABASE_LOCATION=/home/oracle/app/oracle/product/11.2.0/dbhome_1
INVENTORY_LOCATION=/home/oracle/app/oracle/oraInventory
UNIX_GROUP_NAME=oinstall

#安装
/home/oracle/ogg_install/fbo_ggs_Linux_x64_shiphome/Disk1/runInstaller -ignoreSysPrereqs -silent -responseFile /home/oracle/ogg_install/fbo_ggs_Linux_x64_shiphome/Disk1/response/oggcore.rsp

此时,GoldenGate 源端产品安装部分已经完成。

(3)配置环境变量

vim /home/oracle/.bash_profile

ORACLE_BASE=/home/oracle/app/oracle
ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1
ORACLE_SID=dbtest
LD_LIBRARY_PATH=$ORACLE_HOME/lib
PATH=$ORACLE_HOME/bin:$PATH:$HOME/bin:/home/oracle/ogg
export PATH ORACLE_BASE ORACLE_SID ORACLE_HOME LD_LIBRARY_PATH
#以下两个设置是用来设置rlwrap插件的环境,用来支持命令行回显功能  
alias sqlplus="rlwrap sqlplus"
alias rman="rlwrap rman"
alias ggsci="rlwrap /home/oracle/ogg/ggsci"

1.3 配置

登录控制台,创建文件夹

chown oracle:oinstall /home/oracle/ogg
cd /home/oracle/ogg
./ggsci
GGSCI (pos300-db01) 1> create subdirs
Creating subdirectories under current directory /home/oracle/ogg
Parameter files                /home/oracle/ogg/dirprm: created
Report files                   /home/oracle/ogg/dirrpt: created
Checkpoint files               /home/oracle/ogg/dirchk: created
Process status files           /home/oracle/ogg/dirpcs: created
SQL script files               /home/oracle/ogg/dirsql: created
Database definitions files     /home/oracle/ogg/dirdef: created
Extract data files             /home/oracle/ogg/dirdat: created
Temporary files                /home/oracle/ogg/dirtmp: created
Stdout files                   /home/oracle/ogg/dirout: created

各目录用途:
Name Purpose
dirchk Checkpoint files
dirdat GoldenGate trails
dirdef Data definition files
dirprm Parameter files
dirpcs Process status files
dirrpt Report files
dirsql SQL script files
dirtmp Temporary files

2 目标端

2.1 mysql数据库操作

(1)mysql开启二进制日志

vim /usr/local/mysql/my.cnf

#在[mysqld]下增加
log_bin=/usr/local/mysql/mysql-bin
server_id=1

#重启mysql
./mysqld --defaults-file=/usr/local/mysql/my.cnf --user=root

(2)创建数据库test

mysql> create database test;

(3)创建用户ggs,密码ggs,并给予操作数据库test的全部权限(通过HediSql操作界面完成)

(4)创建表student,字段如oracle库中的student表

(5)将oracle已有的数据导入到mysql中,可使用navicat(链接:http://blog.csdn.net/u010587433/article/details/49101737),或者使用goldengate完成(使用goldengate初始化数据没有研究)

2.2 GoldenGate目标端的安装

解压即可用

#解压安装包
cd /home/mysql/
unzip 123012_ggs_Linux_x64_MySQL_64bit.zip
tar xvf ggs_Linux_x64_MySQL_64bit.tar
#安装包被解压在/home/mysql/goldengate中

2.3 配置

登录控制台,创建文件夹

cd /home/mysql/ogg
./ggsci
GGSCI (zoop300) 1> create subdirs
Creating subdirectories under current directory /home/mysql/ogg
Parameter files                /home/mysql/ogg/dirprm: created
Report files                   /home/mysql/ogg/dirrpt: created
Checkpoint files               /home/mysql/ogg/dirchk: created
Process status files           /home/mysql/ogg/dirpcs: created
SQL script files               /home/mysql/ogg/dirsql: created
Database definitions files     /home/mysql/ogg/dirdef: created
Extract data files             /home/mysql/ogg/dirdat: created
Temporary files                /home/mysql/ogg/dirtmp: created
Stdout files                   /home/mysql/ogg/dirout: created

配置

1 源端

(1)为表级添加附加日志

#登录
GGSCI (pos300-db01) 2> dblogin userid ggs password ggs
    Successfully logged into database.
#为student表添加附加日志
GGSCI (pos300-db01) 3> add trandata cms_cluster.student

注:会有一个WARNING,该WARNING貌似是改版本的一个bug,暂时未发现影响业务
Could not find schematrandata function in source database: failed to find function.

#查看
GGSCI (pos300-db01) 4> info trandata cms_cluster.student
Logging of supplemental redo log data is disabled for table CMS_CLUSTER.STUDENT.

(2)配置global
GGSCI (pos300-db01) 5> edit params ./GLOBAL

#文件内容
ggschema ggs

(3)创建管理进程,并启动

GGSCI (pos300-db01) 6> edit params mgr

#文件内容
PORT 7809
DYNAMICPORTLIST 7810-7909
--AUTOSTART ER *
AUTORESTART EXTRACT *, RETRIES 5, WAITMINUTES 3
PURGEOLDEXTRACTS /home/oracle/ogg/dirdat/*, usecheckpoints, minkeepdays 3
LAGREPORTHOURS 1
LAGINFOMINUTES 30
LAGCRITICALMINUTES 45
ACCESSRULE, PROG SERVER, ALLOW

#启动
GGSCI (pos300-db01) 7> start mgr
#查看状态
GGSCI (pos300-db01) 8> info all
#可以看到状态为running

(4)创建extract进程,并启动

GGSCI (pos300-db01) 9> edit params ext1

#文件内容
extract ext1
setenv(NLS_LANG = "AMERICAN_AMERICA.ZHS16GBK")
userid ggs,password ggs
exttrail /home/oracle/ogg/dirdat/e1
table cms_cluster.student;

#table后面的分号要注意

GGSCI (pos300-db01) 10> add extract ext1,tranlog,begin now
GGSCI (pos300-db01) 11> add exttrail /home/oracle/ogg/dirdat/e1,extract ext1
#查看
GGSCI (pos300-db01) 12> info all
#此时可以看到有个EXTRACT(EXT1)stopped
#启动
GGSCI (pos300-db01) 13> start ext1

(5)创建pump进程,并启动

GGSCI (pos300-db01) 14> edit params push

#文件内容
extract push
setenv(NLS_LANG = "AMERICAN_AMERICA.ZHS16GBK")
passthru
userid ggs,password ggs
rmthost 192.168.16.65,mgrport 7809
rmttrail /home/mysql/goldengate/dirdat/e1
table cms_cluster.student;

#exttrailsource /home/oracle/ogg/dirdat/e1 路径与ext1进程抽取到的数据路径一致
GGSCI (pos300-db01) 15> add extract push,exttrailsource /home/oracle/ogg/dirdat/e1

#rmttrail /home/mysql/goldengate/dirdat/e1 路径与mysql端获取数据的路径一致	
GGSCI (pos300-db01) 16> add rmttrail /home/mysql/goldengate/dirdat/e1,extract push

GGSCI (pos300-db01) 17> info all
#此时可以看到有个EXTRACT(PUSH)stopped
#启动
GGSCI (pos300-db01) 18> start push

(6)创建要同步的表的定义

#创建参数文件
GGSCI (pos300-db01) 19> edit param test

#文件内容
defsfile ./dirdef/test.def
userid ggs, password ggs
table cms_cluster.student;

GGSCI (pos300-db01) 20> exit

#生成表定义文件
./defgen paramfile ./dirprm/test.prm
#此时在目录dirdef生成文件test.def

#传至目标库目录
scp /home/oracle/ogg/dirdef/test.def [email protected]:/home/mysql/goldengate/dirdef/

2 目标端

(1)创建管理进程,并启动

GGSCI (zoop300) 2>  edit params mgr

#文件内容
PORT 7809
DYNAMICPORTLIST 7810-7909
--AUTOSTART ER *
AUTORESTART EXTRACT *,RETRIES 5,WAITMINUTES 3
PURGEOLDEXTRACTS /home/mysql/goldengate/dirdat/*,usecheckpoints, minkeepdays 3
LAGREPORTHOURS 1
LAGINFOMINUTES 30
LAGCRITICALMINUTES 45
ACCESSRULE, PROG SERVER, ALLOW

#启动
GGSCI (zoop300) 3> start mgr

(2)配置global

GGSCI (zoop300) 4> edit params ./GLOBALS
#文件内容
ENABLEMONITORING

GGSCI (zoop300) 5> edit params ./globals
#文件内容	
checkpointtable test.checkpoint_table

(3)配置检查点

#登录
GGSCI (zoop300) 6> dblogin sourcedb [email protected]:3306 userid ggs password ggs
#配置检查点
GGSCI (zoop300) 7> add checkpointtable test.checkpoint_table

#查看检查点
GGSCI (zoop300) 8> info checkpointtable test.checkpoint_table

注:在数据库test中可以看到checkpoint_table表

(4)创建replicat进程并启动

GGSCI (zoop300) 9> edit params REP1

#文件内容
replicat rep1
sourcedefs /home/mysql/goldengate/dirdef/test.def
TARGETDB [email protected]:3306,userid ggs,password ggs
reperror default,discard
discardfile /home/mysql/goldengate/dirrpt/rep1.dsc,append,megabytes 50
allownoopupdates
map cms_cluster.student,target test.student;

GGSCI (zoop300) 10> add replicat rep1,exttrail /home/mysql/goldengate/dirdat/e1,checkpointtable test.checkpoint_table

#启动
GGSCI (zoop300) 11> start rep1

常用命令

1 进程查看

info all

2 详细信息查看

#如查看ext1
info ext1,detail

#查看所有
info extract *
info replicat *

3 启动停止进程

start ext1
stop ext1

4 删除进程

需要登录数据库

#源端登录
dblogin userid ggs password ggs

#目标端登录
dblogin sourcedb [email protected]:3306 userid ggs password ggs

#删除进程
delete ext1

5 查看配置文件

view params 进程名称

6 查看日志信息

view report 进程名 

7 日志文件查看

tail -f ggserr.log

你可能感兴趣的:(centos,ogg)