暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

分布式数据库学习Note209:OceanBase社区版中,使用 DBCAT 迁移 OceanBase 表结构到 MySQL 数据库

欢迎访问OceanBase官网获取更多信息:https://www.oceanbase.com/


DBCAT 是一款轻量级的命令行工具,可用于提供数据库之间 DDL 转换和 Schema 比对等功能。这里以 DBCAT 迁移表结构为示例进行介绍。

DBCAT 安装包文件名为 dbcat-[版本号]-SNAPSHOT.tar.gz,下载后解压缩即可使用,可执行文件名为 dbcat

注意

DBCAT 是一款 OceanBase 自研的 Schema Convert & Compare 工具。使用Java以插件化的形式开发,所以它很容易被产品集成,例如:OMS。但是 DBCAT 也会以独立命令行工具的方式供用户使用。

环境准备

DBCAT 能运行在 CentOS、macOS 和 Windows 下。需要安装 JDK 1.8 以上(含)版本。可以使用 OpenJDK,安装好后配置环境变量 JAVA_HOME

CentOS 安装 OpenJDK 示例:

$sudo yum -y install java-1.8.0-openjdk.x86_64

$which java
/usr/local/java/jdk1.8.0_261/bin/java

echo 'export JAVA_HOME=/usr/local/java/jdk1.8.0_261/' >> ~/.bash_profile
. ~/.bash_profile

解压安装文件:

tar zxvf dbcat-1.8.0-SNAPSHOT.tar.gz
cd dbcat-1.8.0-SNAPSHOT/
chmod +x bin/dbcat

$tree -L 3 --filelimit 30
.
├── bin
│   ├── dbcat
│   ├── dbcat.bat
│   └── dbcat-debug
├── conf
│   ├── dbcat.properties
│   └── logback.xml
├── docs
│   ├── README.docx
│   ├── README.md
│   └── README.txt
├── LEGAL.md
├── lib [45 entries exceeds filelimit, not opening dir]
├── LICENSE
├── meta
│   └── README
└── NOTICE

5 directories, 12 files

安装文件中有以下几个目录需要了解:

目录名说明
bin可执行文件目录。
conf日志文件配置目录。
lib运行时期依赖的包。
meta离线转换场景下,导出字典表数据。
~/outputSQL 文件与报告文件,运行时生成。

导出 OceanBase 数据库的 MySQL 租户下的表结构

DBCAT 具有在线转换功能,该功能是指 DBCAT 能直连源端数据库,将数据库中的对象导出。当对象非常多时(如超过 1 万),导出过程可能会有点慢。

dbcat 导出命令如下:

bin/dbcat convert -H<host> -P<port> -u<user> -p<password>  -D <database> --from <from> --to <to> --all

您可运行命令 bin/dbcat help convert 查看更多参数信息。

必选参数:

选项有无参数英文描述中文描述
-H/--hostYHost on which database is located数据库服务器的 IP 地址
-P/--portYTCP/IP port number for connection数据库服务器的端口
-u/--userYDatabase user to use when connecting to server登录数据库的用户名
-t/--tenantYInterpret the tenant name for oceanbase连接 OceanBase 集群需要提供租户名
-c/--clusterYInterpret the cluster name for oceanbase连接 OceanBase 集群需要提供集群名
-p/--passwordYPassword to use when connecting to server登录数据库的密码
-D/--databaseYInterpret the name as source database name数据库名(源库),DB2 须区分数据库名和模式名
--service-idYInterpret the service id for oracle连接 Oracle 数据库需要提供服务 ID
--service-nameYInterpret the service name for oracle连接 Oracle 数据库需要提供服务名
--as-sysdbaNIterpret logon as sysdba role for oracle连接 Oracle 数据库 sysdba 角色
--sys-userYInterpret username for sys tenant连接 OceanBase 集群系统租户的用户名
--sys-passwordYInterpret password for sys tenant连接 OceanBase 集群系统租户的密码
--schemaYInterpret the name as source schema for DB2模式名(源库),非DB2,模式名与数据名相同
--fromYInterpret the type as source database type源库的类型
--toYInterpret the type as target database type目标库的类型
--allNResolve all the database objects所有的数据库对象(默认:TABLE, VIEW)

可选参数:

选项有无参数英文描述中文描述
-f/--fileYInterpret the output path of the *.sqlsql 文件的输出路径
--offlineNUse offline mode使用离线模式
--target-schemaYInterpret the name as target schema模式名(目标库)
--tableYResolve the specified tables in the database导出的表
--viewYResolve the specified views in the database导出的视图
--triggerYResolve the specified triggers in the database导出的触发器
--synonymYResolve the specified synonyms in the database导出的同义词
--sequenceYResolve the specified sequences in the database导出的序列
--functionYResolve the specified functions in the database导出的函数
--procedureYResolve the specified procedures in the database导出的存储过程
--dblinkYResolve the specified dblinks in the database导出所有的 DBLink
--typeYResolve the specified types in the database导出的 type
--type-bodyYResolve the specified type bodies in the database导出的 type body
--packageYResolve the specified packages in the database导出的 package
--package-bodyYResolve the specified package bodies in the database导出的 package body
--no-quoteNGenerate DDL without quote character产生的 DDL 不带引号
--no-schemaNGenerate DDL without schema name产生的 DDL 不带模式名
--target-schemaYInterpret the name as source database name产生的 DDL 中使用指定的模式名
--exclude-typeYInterpret the name as target schema搭配 --all 使用,如:--all --exclude-type 'TABLE' 表示排除 TABLE 类型

这里以导出 4.0.0 版本的 OceanBase 集群中的 MySQL 租户下 database 为 test 的所有对象的结构,并将其迁移到 MySQL 5.7 版本下为示例。

bin/dbcat convert -H 172.30.xxx.xxx  -P 2883 -uroot -pxxxxx -D test --from obmysql40 --to mysql57 --all

特别说明:

  • dbcat 不需要直接安装在数据库主机上,安装在可直连数据库主机的主机上即可。

  • 参数中的 --from 和 --to 为源端和目的端的数据库类型,需要详细到版本号。当前 dbcat 支持的源端和目标端数据库详细如下:

    源端数据库类型目标端数据库类型
    TiDBOBMYSQL
    PGOBMYSQL
    SYBASEOBORACLE
    MYSQLOBMYSQL
    ORACLEOBORACLE
    ORACLEOBMYSQL
    DB2 IBM iOBORACLE
    DB2 LUWOBORACLE
    DB2 LUWOBMYSQL
    OBMYSQLMYSQL
    OBORACLEORACLE

    其中 OBMYSQL 为 OceanBase 数据库的 MySQL租户,OBORACLE 为 OceanBase 数据库的 Oracle 租户。

  • 当前支持的源端和目标端数据库详细的版本,详情如下。

    数据库类型数据库版本
    TiDBtidb4
    tidb5
    PGpgsql10
    SYBASEsybase15
    DB2 IBM idb2ibmi71
    DB2 LUWdb2luw970
    db2luw1010
    db2luw1050
    db2luw111
    db2luw115
    MYSQLmysql56
    mysql57
    mysql</80>
    ORACLEoracle9i
    oracle10g
    oracle11g
    oracle12c
    oracle18c
    oracle19c
    OBMYSQLobmysql14x
    obmysql21x
    obmysql22x
    obmysql200
    obmysql211
    obmysql2210
    obmysql2230
    obmysql2250
    obmysql2271 ~ obmysql2277
    obmysql30x
    obmysql31x
    obmysql32x
    obmysql322
    obmysql40
    OBORACLEoboracle2220
    oboracle2230
    oboracle2250
    oboracle2270 ~ oboracle2277
    oboracle21x
    oboracle22x
    oborcle30x
    oboracle31x
    oboracle32x
    oboracle322
    oboracle40

运行后的输出文件在用户 home 目录的 output 下。

$tree ~/output/dbcat-20xx-xx-xx-164533/
/home/qing.meiq/output/dbcat-20xx-xx-xx-164533/
├── tpccdb
│   └── TABLE-schema.sql
└── tpccdb-conversion.html

1 directory, 2 files

导入 MySQL 数据库

使用 DBCAT 导出的文件格式为 SQL 文件,可以使用 source 命令,示例如下:

MySQL [test]> source TABLE-schema.sql
Query OK, 0 rows affected (0.044 sec)

注意

如果 sql 文件不在当前目录下,则需要使用绝对地址。

导数结果验证

示例:查看一个表结构在 MySQL 里的书写方式 和 OceanBase 数据库里的表结构。

查看源数据库 OceanBase 的表 bmsql_customer 的表结构:

obclient [test]> desc bmsql_customer;
+----------------+---------------+------+-----+-------------------+-----------------------------+
| Field          | Type          | Null | Key | Default           | Extra                       |
+----------------+---------------+------+-----+-------------------+-----------------------------+
| c_w_id         | bigint(20)    | NO   | PRI | NULL              |                             |
| c_d_id         | bigint(20)    | NO   | PRI | NULL              |                             |
| c_id           | bigint(20)    | NO   | PRI | NULL              |                             |
| c_discount     | decimal(4,4)  | YES  |     | NULL              |                             |
| c_credit       | char(2)       | YES  |     | NULL              |                             |
| c_last         | varchar(16)   | YES  |     | NULL              |                             |
| c_first        | varchar(16)   | YES  |     | NULL              |                             |
| c_credit_lim   | decimal(12,2) | YES  |     | NULL              |                             |
| c_balance      | decimal(12,2) | YES  |     | NULL              |                             |
| c_ytd_payment  | decimal(12,2) | YES  |     | NULL              |                             |
| c_payment_cnt  | bigint(20)    | YES  |     | NULL              |                             |
| c_delivery_cnt | bigint(20)    | YES  |     | NULL              |                             |
| c_street_1     | varchar(20)   | YES  |     | NULL              |                             |
| c_street_2     | varchar(20)   | YES  |     | NULL              |                             |
| c_city         | varchar(20)   | YES  |     | NULL              |                             |
| c_state        | char(2)       | YES  |     | NULL              |                             |
| c_zip          | char(9)       | YES  |     | NULL              |                             |
| c_phone        | char(16)      | YES  |     | NULL              |                             |
| c_since        | timestamp     | NO   |     | CURRENT_TIMESTAMP | ON UPDATE CURRENT_TIMESTAMP |
| c_middle       | char(2)       | YES  |     | NULL              |                             |
| c_data         | varchar(500)  | YES  |     | NULL              |                             |
+----------------+---------------+------+-----+-------------------+-----------------------------+
21 rows in set (0.004 sec)

查看目标数据库 MySQL 的表 bmsql_customer 的建表 SQL:

MySQL [test]> show create table bmsql_customer \G
*************************** 1. row ***************************
       Table: bmsql_customer
Create Table: CREATE TABLE `bmsql_customer` (
  `c_w_id` bigint(20) NOT NULL,
  `c_d_id` bigint(20) NOT NULL,
  `c_id` bigint(20) NOT NULL,
  `c_discount` decimal(4,4) DEFAULT NULL,
  `c_credit` char(2) COLLATE utf8_unicode_ci DEFAULT NULL,
  `c_last` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
  `c_first` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
  `c_credit_lim` decimal(12,2) DEFAULT NULL,
  `c_balance` decimal(12,2) DEFAULT NULL,
  `c_ytd_payment` decimal(12,2) DEFAULT NULL,
  `c_payment_cnt` bigint(20) DEFAULT NULL,
  `c_delivery_cnt` bigint(20) DEFAULT NULL,
  `c_street_1` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `c_street_2` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `c_city` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `c_state` char(2) COLLATE utf8_unicode_ci DEFAULT NULL,
  `c_zip` char(9) COLLATE utf8_unicode_ci DEFAULT NULL,
  `c_phone` char(16) COLLATE utf8_unicode_ci DEFAULT NULL,
  `c_since` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `c_middle` char(2) COLLATE utf8_unicode_ci DEFAULT NULL,
  `c_data` varchar(500) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`c_w_id`,`c_d_id`,`c_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
1 row in set (0.01 sec)

经对比是一致的。






欢迎访问OceanBase官网获取更多信息:https://www.oceanbase.com/

最后修改时间:2023-03-30 15:47:38
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论