本案例使用PLSQL导出Oracle表结构的SQL文件,随后通过sqlines将Oracle表结构,转换为MySQL表结构,人工核对后,再使用Oracle GoldenGate(OGG),进行数据的全量加增量迁移。请注意本次迁移不包括Oracle的存储过程和触发器。只迁移表结构和数据到MySQL。
IP | 服务 |
---|---|
172.22.138.219 | Oracle:11g |
172.22.138.220 | MySQL:5.7 |
# 下面需要将oracle设置为mount模式进行参数修改,生产环境需要谨慎操作。
SQL> shutdown immediate;
SQL> startup mount;
SQL> alter database archivelog;
SQL> alter database force logging;
SQL> alter database add supplemental log data;
SQL> alter system switch logfile;
SQL> alter system set enable_goldengate_replication=true;
SQL> alter database open;
#开启不区分大小写的参数
root # vim /etc/my.cnf
[mysqld]
...
lower-case-table-names = 1
...
#重启mysqld服务
root # systemctl restart mysqld
sqlines表结构转换工具,下载地址:https://gitee.com/opengauss/openGauss-tools-sqlines
可以通过PLSQL一键导出,也可以编写脚本,通过Oracle的DBMS_METADATA.GET_DDL视图,导出Oracle建表语句,我这里使用PLSQL导出oracle的SQL文件,就不再过多赘述了。
1.安装sqlines工具,并转换oracle表结构。
#解压安装好sqlines工具包,过程就掠过了,安装过程就略过了。-in=这里填写你通过plsql备份的oracle表结构文件。-out这里表示输出文件。
root # sqlines -s=oracle -t=mysql -in=oracle.sql -out=mysql.sql
#调整sqlines输出后的文件,修剪掉多余的信息
sed -i '/PL\/SQL Developer/d' mysql.sql
sed -i '/^spool/d' mysql.sql
sed -i '/^prompt/d' mysql.sql
2.人工检查转换后的表结构,重点关注数据类型,是否合适。
3.MySQL创建与oracle用户名称一致的库名,字符集要与oracle一致。
MySQL [(none)]> create database test charset gbk;
MySQL [(none)]> source mysql.sql
#检查表与表结构是否无误
MySQL [(none)]> show tables;
MySQL [(none)]> show create table table_name;
OGG版本:
Oracle GoldenGate 12.3.0.1.4 for Oracle on Linux x86-64
Oracle GoldenGate 12.3.0.1.5 for MySQL-compatible DBs on Linux x86-64
OGG下载地址 :https://www.oracle.com/middleware/technologies/goldengate-downloads.html
1.解压安装包,并编辑好应答文件后执行安装程序
oracle # mkdir /opt/oracle/ogg -p
oracle # unzip /opt/oracle/V975837-01.zip
oracle # vim ./fbo_ggs_Linux_x64_shiphome/Disk1/response/oggcore.rsp
...
INSTALL_OPTION=ORA11g
SOFTWARE_LOCATION=/opt/oracle/ogg
START_MANAGER=false
#执行安装文件(此处不支持相对路径)
oracle # ./fbo_ggs_Linux_x64_shiphome/Disk1/runInstaller -silent -responseFile /opt/oracle/ogg/fbo_ggs_Linux_x64_shiphome/Disk1/response/oggcore.rsp
2.添加环境变量到.bash_profile中
oracle # vim /home/oracle/.bash_profile
...
export GG_HOME=/opt/oracle/ogg
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$GG_HOME
export PATH=$ORACLE_HOME:$GG_HOME:$PATH
alias ggsci='cd GG_HOME; ggsci'
#使环境变量生效
oracle # source /home/oracle/.bash_profile
3.测试ogg命令是否正常运行
oracle # ggsci
#OGG中创建相关目录
GGSCI ( oracle ) 1> create subdirs
1.解压安装包
mkdir /opt/ogg
unzip V978711-01.zip
tar xf ./ggs_Linux_x64_MySQL_64bit.tar -C /opt/ogg
2.进入到解压后的目录测试运行,并创建相关目录
./ggsci
GGSCI ( mysql) 1> create subdirs
创建同步的用户并进行授权
MySQL [(none)]> grant all on *.* to mysql@'%' identified by 'mysql';
1.创建OGG专属用户,goldengate 用户,并赋予相关权限
SQL> create tablespace goldengate datafile '/data/app/oracle/oradata/ldmpdb/goldengate001.dbf' size 4G;
SQL> create user goldengate identified by goldengate123 default tablespace goldengate;
SQL> grant dba to goldengate;
2.OGG设置免密登陆
oracle # ggsci
GGSCI ( oracle) 1> create subdirs
GGSCI ( oracle) 1> add credentialstore
GGSCI ( oracle) 1> alter credentialstore add user goldengate,password goldengate123
登陆测试:
GGSCI ( oracle) 1> dblogin useridalias goldengate
基本配置中,主要是两套进程:
1.MGR配置
GGSCI ( oracle) 1> edit param mgr
...
port 9001
DYNAMICPORTLIST 9001-9020
--AUTORESTART ER *,RETRIES 5,WAITMINUTES 7
PURGEOLDEXTRACTS ./dirdat/*,usecheckpoints, minkeepdays 2
LAGREPORTHOURS 1
LAGINFOMINUTES 30
LAGCRITICALMINUTES 45
ACCESSRULE, PROG *, IPADDR *.*.*.*, ALLOW
#启动mgr进程
GGSCI ( oracle) 1> start mgr
2.Extract 进程配置信息
GGSCI ( oracle) 1> edit param ext_ora
EXTRACT ext_ora
SETENV (ORACLE_HOME="/data/oracle/11.2.0.4")
setenv (NLS_LANG="AMERICAN_AMERICA.ZHS16GBK")
setenv (ORACLE_SID="orcl")
useridalias goldengate
GETTRUNCATES
REPORTCOUNT EVERY 1 MINUTES, RATE
DISCARDFILE ./dirrpt/ext_ora.dsc,APPEND,MEGABYTES 1000
WARNLONGTRANS 2h,CHECKINTERVAL 10m
EXTTRAIL ./dirdat/sk
TRANLOGOPTIONS EXCLUDEUSER goldengate
TRANLOGOPTIONS MINEFROMACTIVEDG
DBOPTIONS ALLOWUNUSEDCOLUMN
DYNAMICRESOLUTION
FETCHOPTIONS FETCHPKUPDATECOLS,INCONSISTENTROW ALLOW
getupdatebefores
nocompressdeletes
nocompressupdates
table test.*;
3.创建一个抽取进程
GGSCI (oracle) 3> add extract ext_ora,TRANLOG, begin now
#创建一个抽取进程的数据保存目录,并于新建的抽取进程进行关联
GGSCI (oracle) 4> add ExtTrail ./dirdat/sk, Extract ext_ora, Megabytes 1024
4.Pump进程配置
GGSCI (oracle) 4> edit param tdpm_ora
EXTRACT tdpm_ora
RMTHOST 172.22.138.220 MGRPORT 9001, compress
PASSTHRU
RMTTRAIL /opt/ogg/dirdat/sk
DYNAMICRESOLUTION
--table
table test.*;
5.增加一个传输进程,并于抽取进程的文件进行关联
GGSCI (oracle) 4> add extract tdpm_ora, exttrailsource ./dirdat/sk
#传输进程推送目录,这里推送的是目标段的目录,需要观察目标端目录是否存在
GGSCI (oracle) 4> add rmttrail /opt/ogg/dirdat/sk, extract tdpm_ora, megabytes 100
6.配置配置全量抽取进程,oracle端配置
GGSCI (oracle) 4> edit param init_ora ldmpdp
extract init_ora
setenv (NLS_LANG=AMERICAN_AMERICA.ZHS16GBK)
setenv (ORACLE_SID="HXYDB")
useridalias goldengate
rmthost 172.22,138,220,mgrport 9001
rmttask replicat,group rnit_ora
table test.*;
添加进程
GGSCI (oracle) 4> add extract init_ora,sourceistable
1.MGR 进程配置信息
GGSCI (mysql) 2> edit param mgr
port 9001
DYNAMICPORTLIST 9001-9120
ACCESSRULE, PROG *, IPADDR *.*.*.*, ALLOW
--AUTORESTART ER *,RETRIES 5,WAITMINUTES 7
PURGEOLDEXTRACTS ./dirdat/*,usecheckpoints, minkeepdays 10
LAGREPORTHOURS 1
LAGINFOMINUTES 30
LAGCRITICALMINUTES 45
GGSCI (mysql) 2> start mgr
2.Replicat 进程配置信息
Replicat 进程是运行在目标端系统的一个进程,负责读取 Extract 进程提取到的数据(变更的事务或 DDL 变化)并应用到目标数据库MySQL上。
#创建 checkpoint table (该步骤为一次性工作)
GGSCI (mysql) 2> dblogin sourcedb [email protected]:3306 userid mysql password mysql
首先创建出ogg数据库
GGSCI (mysql) 2> add checkpointtable ogg.checkpoint
3.编辑replicat 进程的参数
GGSCI (mysql) 2> edit param rep_ora
replicat rep_ora
targetdb [email protected]:3306 userid mysql password mysql
SQLEXEC "show databases;"
handlecollisions
MAXTRANSOPS 10000
discardfile /opt/ogg/dirrpt/repora.dsc,purge
map test.*,target test.*;
#添加replicat进程
GGSCI (mysql) 2> add replicat rep_ora,exttrail /opt/ogg/dirdat/sk,checkpointtable ogg.checkpoint
4.配置全量应用进程,mysql端配置
GGSCI (mysql) 2> edit param rnit_ora
replicat rnit_ora
SETENV (NLS_LANG=AMERICAN_AMERICA.ZHS16GBK)
targetdb [email protected]:3306 userid mysql password mysql
discardfile /opt/ogg/dirrpt/rnit_ora.dsc,purge
map test.*, target test.*;
#添加进程
GGSCI (mysql) 2> add replicat rnit_ora,specialrun
首先配置好 MGR 进程,抽取进程,投递进程,应用进程,全量抽取进程,全量应用进程。
#启动抽取和投递进程
start ext_ora
start tdpm_ora
#启动全量抽取进程
start init_ora
#启动增量进程
start rep_ora
#启动全量应用进程
start rnit_ora
启动报错1:2022-10-10T01:24:11.982-0400 ERROR OGG-02801 Oracle GoldenGate Capture for Oracle, ext_ora.prm: Parameter MINEFROMACTIVEDG can only be used when the database is in READ ONLY mode.
解决方式:
启动报错2: ERROR OGG-00529 Oracle GoldenGate Capture for Oracle, ext_ora.prm: DDL Replication is enabled but table goldengate.GGS_DDL_HIST is not found. Please check DDL installation in the database.
解决方式: