欢迎访问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 | 离线转换场景下,导出字典表数据。 |
| ~/output | SQL 文件与报告文件,运行时生成。 |
导出 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/--host | Y | Host on which database is located | 数据库服务器的 IP 地址 |
| -P/--port | Y | TCP/IP port number for connection | 数据库服务器的端口 |
| -u/--user | Y | Database user to use when connecting to server | 登录数据库的用户名 |
| -t/--tenant | Y | Interpret the tenant name for oceanbase | 连接 OceanBase 集群需要提供租户名 |
| -c/--cluster | Y | Interpret the cluster name for oceanbase | 连接 OceanBase 集群需要提供集群名 |
| -p/--password | Y | Password to use when connecting to server | 登录数据库的密码 |
| -D/--database | Y | Interpret the name as source database name | 数据库名(源库),DB2 须区分数据库名和模式名 |
| --service-id | Y | Interpret the service id for oracle | 连接 Oracle 数据库需要提供服务 ID |
| --service-name | Y | Interpret the service name for oracle | 连接 Oracle 数据库需要提供服务名 |
| --as-sysdba | N | Iterpret logon as sysdba role for oracle | 连接 Oracle 数据库 sysdba 角色 |
| --sys-user | Y | Interpret username for sys tenant | 连接 OceanBase 集群系统租户的用户名 |
| --sys-password | Y | Interpret password for sys tenant | 连接 OceanBase 集群系统租户的密码 |
| --schema | Y | Interpret the name as source schema for DB2 | 模式名(源库),非DB2,模式名与数据名相同 |
| --from | Y | Interpret the type as source database type | 源库的类型 |
| --to | Y | Interpret the type as target database type | 目标库的类型 |
| --all | N | Resolve all the database objects | 所有的数据库对象(默认:TABLE, VIEW) |
可选参数:
| 选项 | 有无参数 | 英文描述 | 中文描述 |
|---|---|---|---|
| -f/--file | Y | Interpret the output path of the *.sql | sql 文件的输出路径 |
| --offline | N | Use offline mode | 使用离线模式 |
| --target-schema | Y | Interpret the name as target schema | 模式名(目标库) |
| --table | Y | Resolve the specified tables in the database | 导出的表 |
| --view | Y | Resolve the specified views in the database | 导出的视图 |
| --trigger | Y | Resolve the specified triggers in the database | 导出的触发器 |
| --synonym | Y | Resolve the specified synonyms in the database | 导出的同义词 |
| --sequence | Y | Resolve the specified sequences in the database | 导出的序列 |
| --function | Y | Resolve the specified functions in the database | 导出的函数 |
| --procedure | Y | Resolve the specified procedures in the database | 导出的存储过程 |
| --dblink | Y | Resolve the specified dblinks in the database | 导出所有的 DBLink |
| --type | Y | Resolve the specified types in the database | 导出的 type |
| --type-body | Y | Resolve the specified type bodies in the database | 导出的 type body |
| --package | Y | Resolve the specified packages in the database | 导出的 package |
| --package-body | Y | Resolve the specified package bodies in the database | 导出的 package body |
| --no-quote | N | Generate DDL without quote character | 产生的 DDL 不带引号 |
| --no-schema | N | Generate DDL without schema name | 产生的 DDL 不带模式名 |
| --target-schema | Y | Interpret the name as source database name | 产生的 DDL 中使用指定的模式名 |
| --exclude-type | Y | Interpret 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 支持的源端和目标端数据库详细如下:
源端数据库类型 目标端数据库类型 TiDB OBMYSQL PG OBMYSQL SYBASE OBORACLE MYSQL OBMYSQL ORACLE OBORACLE ORACLE OBMYSQL DB2 IBM i OBORACLE DB2 LUW OBORACLE DB2 LUW OBMYSQL OBMYSQL MYSQL OBORACLE ORACLE 其中 OBMYSQL 为 OceanBase 数据库的 MySQL租户,OBORACLE 为 OceanBase 数据库的 Oracle 租户。
当前支持的源端和目标端数据库详细的版本,详情如下。
数据库类型 数据库版本 TiDB tidb4
tidb5PG pgsql10 SYBASE sybase15 DB2 IBM i db2ibmi71 DB2 LUW db2luw970
db2luw1010
db2luw1050
db2luw111
db2luw115MYSQL mysql56
mysql57
mysql</80>ORACLE oracle9i
oracle10g
oracle11g
oracle12c
oracle18c
oracle19cOBMYSQL obmysql14x
obmysql21x
obmysql22x
obmysql200
obmysql211
obmysql2210
obmysql2230
obmysql2250
obmysql2271 ~ obmysql2277
obmysql30x
obmysql31x
obmysql32x
obmysql322
obmysql40OBORACLE oboracle2220
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/




