內容簡介
本文將介紹一款從oracle數據庫遷移數據至postgres、greenplum數據庫的工具的簡介和安裝。
Ora2Pg是什麼
Ora2Pg是一個免費工具,用於將Oracle或MySQL數據庫中數據遷移到PostgreSQL。它連接Oracle數據庫,自動掃描並提取其結構或數據,然後生成可以加載到PostgreSQL數據庫中的SQL腳本。由於Greenplum與PostgreSQL兼容,所以也可以用來遷移到Greenplum。
Ora2Pg可用於從逆向工程Oracle數據庫到大型企業數據庫遷移,或者只是將一些Oracle數據複製到PostgreSQL數據庫中。它非常易於使用,除了提供連接Oracle數據庫所需的參數之外,不需要任何Oracle數據庫知識。更多信息可以參看官網的說明。
安裝所需的準備
我是在CentOS上安裝的Ora2Pg。其他系統如Windows系統的安裝,應該安裝Strawberry Perl(http://strawberryperl.com/)和操作系統對應的Oracle客戶端,本文就不做贅述。
系統上需要安裝Oracle Instant Client或完整的Oracle instaaltion。Ora2Pg的官網上說可以從Oracle下載中心下載數據庫對應版本或是更高版的RPM:
oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm
oracle-instantclient12.2-devel-12.2.0.1.0-1.x86_64.rpm
oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.x86_64.rpm
地址是:
http://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index.html
由於下載需要先登錄,所以先要在oracle上先申請帳號後才能下載。但經過本人的點擊註冊oracle的帳號後,並不能登錄進去以及下載對應軟件。
後面是在百度網盤上搜索才找到對應的安裝包,若有更好的下載方式,可以留言告訴我。
發現官網有個不用帳號就能下載的網址入口:
https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html
其他的依賴可以在centos中下載安裝,相關要求:
- 1、 perl版本5.10以上
- 2、安裝DBD::Oracle
- 3、安裝DBI
- 4、安裝DBD::Pg 【可選,需要直接導入postgres數據庫時安裝】
安裝步驟
1.安裝perf和cpan
yum install -y perf cpan
2.安裝oracle-instantclient安裝包,安裝basic、devel、sqlplus三個rpm包
rpm -ivh oracle-instantclient*.rpm
3.安裝oracle-instantclient完畢後,添加oracle-instantclient配置文件
echo "/usr/lib/oracle/12.2/client64/lib" > /etc/ld.so.conf.d/oracle_client.conf
ldconfig
ldconfig -p|grep oracle
libsqlplusic.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libsqlplusic.so
libsqlplus.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libsqlplus.so
liboramysql12.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/liboramysql12.so
libons.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libons.so
libocijdbc12.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libocijdbc12.so
libociei.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libociei.so
libocci.so.12.1 (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libocci.so.12.1
libocci.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libocci.so
libnnz12.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libnnz12.so
libmql1.so (libc6,x86-64) => /usr/lib/oracle/12.2/client64/lib/libmql1.so
...
注意版本替換成下載的版本
4.測試oracle-instantclient是否安裝成功
sqlplus64 test/[email protected]:1521/ora11g
5.安裝 DBI
cpan install DBI
6.安裝 DBD-Oracle
export ORACLE_HOME=/usr/lib/oracle/12.2/client64
export PATH=$ORACLE_HOME/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
wget http://search.cpan.org/CPAN/authors/id/P/PY/PYTHIAN/DBD-Oracle-1.74.tar.gz
tar -zxvf DBD-Oracle-1.74.tar.gz
cd DBD-Oracle-1.74
perl Makefile.PL -l
make && make test
make install
7.安裝Ora2pg
wget https://github.com/darold/ora2pg/archive/v18.2.tar.gz
tar -zxvf v18.2.tar.gz
cd ora2pg-18.2/
perl Makefile.PL
make && make install
8.導出數據測試,編輯導出配置文件
vi ora.conf
#---------------------------------#
#---------------------------------#
# Set the Oracle home directory
ORACLE_HOME /usr/lib/oracle/12.2/client64
# Set Oracle database connection (data source, user, password)
ORACLE_DSN dbi:Oracle:host=xxx.xxx.xxx.xxx;sid=ora11g;port=1521
ORACLE_USER test
ORACLE_PWD test
# Oracle schema/owner to use
#SCHEMA SCHEMA_NAME
SCHEMA test
#--------------------------
# EXPORT SECTION (Export type and filters)
#--------------------------
# Type of export. Values can be the following keyword:
# TABLE Export tables, constraints, indexes, …
# PACKAGE Export packages
# INSERT Export data from table as INSERT statement
# COPY Export data from table as COPY statement
# VIEW Export views
# GRANT Export grants
# SEQUENCE Export sequences
# TRIGGER Export triggers
# FUNCTION Export functions
# PROCEDURE Export procedures
# TABLESPACE Export tablespace (PostgreSQL >= 8 only)
# TYPE Export user-defined Oracle types
# PARTITION Export range or list partition (PostgreSQL >= v8.4)
# FDW Export table as foreign data wrapper tables
# MVIEW Export materialized view as snapshot refresh view
# QUERY Convert Oracle SQL queries from a file.
# KETTLE Generate XML ktr template files to be used by Kettle.
TYPE INSERT
# By default all output is dump to STDOUT if not send directly to PostgreSQL
# database (see above). Give a filename to save export to it. If you want
# a Gzip’d compressed file just add the extension .gz to the filename (you
# need perl module Compress::Zlib from CPAN). Add extension .bz2 to use Bzip2
# compression.
OUTPUT output.sql
# Base directory where all dumped files must be written
#OUTPUT_DIR /var/tmp
OUTPUT_DIR /tmp
9.使用配置文件導出
ora2pg -c ./ora.conf
10.檢查導出結果
cat /tmp/output.sql
參考資料
[1] 源碼github地址 https://github.com/darold/ora2pg
[2] https://www.cnblogs.com/lottu/p/9114959.html
[3] https://www.jianshu.com/p/08abf07f57be
[4] https://github.com/simapple/postgresdigal/blob/master/201804/20180423_02.md