---
title: 如何使用 DataX 工具迁移 MySQL 数据到 OceanBase-OceanBase数据库使用指南
description: 了解OceanBase数据库在实际应用中关于如何使用 DataX 工具迁移 MySQL 数据到 OceanBase相关的常见问题和使用技巧，帮助您快速解决如何使用 DataX 工具迁移 MySQL 数据到 OceanBase的难题。
image: https://mdn.alipayobjects.com/huamei_22khvb/afts/img/A*OSPzQ6GUQF4AAAAAQHAAAAgAeiGDAQ/original
---
切换语言

- 中文站 - 简体中文
- International - English
- 日本站 - 日本語

划线反馈

# 如何使用 DataX 工具迁移 MySQL 数据到 OceanBase

更新时间：2026-08-25 02:41

适用版本： V4.0.x 内容类型：TechNote  

## 总结说明

本文主要描述如何使用 DataX 工具将 MySQL 数据迁移到 OceanBase MySQL。

## 详细说明

### 源端、目标端环境准备

```sql
MySQL [test11]> CREATE TABLE departments (
    ->   dept_id INT PRIMARY KEY,
    ->   dept_name VARCHAR(50) NOT NULL,
    ->   dept_location VARCHAR(50),
    ->   dept_manager VARCHAR(50)
    -> );
Query OK, 0 rows affected (0.01 sec)

MySQL [test11]> INSERT INTO departments (dept_id, dept_name, dept_location, dept_manager) VALUES (1, '人事部', '北京市', '王经理');
Query OK, 1 row affected (0.01 sec)

MySQL [test11]> INSERT INTO departments (dept_id, dept_name, dept_location, dept_manager) VALUES (2, '财务部', '上海市', '张经理');
Query OK, 1 row affected (0.00 sec)

-- ...省略插入数据

MySQL [test11]> INSERT INTO departments (dept_id, dept_name, dept_location, dept_manager) VALUES (9, '采购部', '杭州市', '钱经理');
Query OK, 1 row affected (0.00 sec)

MySQL [test11]> INSERT INTO departments (dept_id, dept_name, dept_location, dept_manager) VALUES (10, '翻译部', '重庆市', '孙经理');
Query OK, 1 row affected (0.01 sec)

MySQL [test11]> select count(1) from departments;
+----------+
| count(1) |
+----------+
|       10 |
+----------+
1 row in set (0.01 sec)

```

```sql
obclient> CREATE TABLE departments (
    ->   dept_id INT PRIMARY KEY,
    ->   dept_name VARCHAR(50) NOT NULL,
    ->   dept_location VARCHAR(50),
    ->   dept_manager VARCHAR(50)
    -> );
Query OK, 0 rows affected (1.11 sec)

obclient> select count(1) from departments;
+----------+
| count(1) |
+----------+
|        0 |
+----------+
1 row in set (0.23 sec)

```

### 安装 DataX

1. 下载并解压安装文件：

```bash
wget https://datax-opensource.oss-cn-hangzhou.aliyuncs.com/202303/datax.tar.gz
tar zxvf datax.tar.gz
cd datax

```

解压后的目录结构如下：

```plain
$ tree -L 1 --filelimit 30
.
├── bin
├── conf
├── job
├── lib
├── log
├── log_perf
├── plugin
├── script
└── tmp

```

解压目录说明：

| 目录名 | 说明 |
| --- | --- |
| bin | 可执行文件目录。该目录下的 datax.py 为 DataX 任务的启动脚本 |
| conf | 日志文件配置目录。该目录下存放 DataX 与任务无关的配置文件 |
| lib | 运行时期依赖的包。该目录存放 DataX 运行所需要的全局 jar 文件 |
| job | 该目录下有一个用于测试验证 DataX 安装的任务配置文件 |
| log | 日志文件目录。该目录下存放 DataX 任务运行的日志；DataX 运行时，默认会将日志输出到标准输出，同时写入到 log 目录下 |
| plugin | 插件文件目录。该目录下保存 DataX 支持的各种数据源插件 |

### 配置 job 文件

1. 将以下配置文件按照源端和目标端的信息正确填写后，传到 `datax/job/` 目录下准备。

> 请直接下载并使用此 JSON 文件，下面的示例代码中含注释，如果直接复制使用的话格式可能会有问题。

```json
{
    "job": {
        "setting": {
            "speed": {
                "channel": 4
            },
            "errorLimit": {
                "record": 0,
                "percentage": 0.1
            }
        },
        "content": [
            {
                "reader": {
                    "name": "mysqlreader",
                    "parameter": {
                        "username": "root",
                        "password": "xxxxxx",
                        "column": ["*"],
                        "connection": [
                            {
                                "table": ["departments"],
                                "jdbcUrl": ["jdbc:mysql://xx.xx.xx.xx:3406/test11?useSSL=false&useUnicode=true&characterEncoding=utf8"]
                            }
                        ]
                    }
                },
                "writer": {
                    "name": "oceanbasev10writer",
                    "parameter": {
                        "obWriteMode": "insert",
                        "column": ["*"],
                        "preSql": ["truncate table departments"],
                        "connection": [
                            {
                                "jdbcUrl": "jdbc:oceanbase://xx.xx.xx.xx:2883/test11?useSSL=false&useUnicode=true&characterEncoding=utf8",
                                "table": ["departments"]
                            }
                        ],
                        "username": "root@mysql#obcluster",
                        "password": "xxxxxx",
                        "writerThreadCount": 10,
                        "batchSize": 1000,
                        "memstoreThreshold": "0.9"
                    }
                }
            }
        ]
    }
}

```

2. reader 相关重要参数解释：

| 参数 | 说明 |
| --- | --- |
| username | 用户名 |
| password | 密码 |
| column | 表示读取或写入的列名 |
| table | 需要迁移数据的表名 |
| jdbcUrl | JDBC 连接串，注意最后需要填写 useSSL、useUnicode、characterEncoding 参数 |

3. writer 相关重要参数解释：

| 参数 | 说明 |
| --- | --- |
| username | 表示连接 OceanBase 数据库时使用的用户名；格式需要 用户@租户#集群 |
| password | 表示连接 OceanBase 数据库时使用的密码 |
| column | 表示读取或写入的列名 |
| preSql | 表示在写入数据之前需要执行的 SQL 语句 |
| table | 需要迁移数据的表名 |
| jdbcUrl | JDBC 连接串，注意最后需要填写 useSSL、useUnicode、characterEncoding 参数 |

### 执行 job

配置好 job 文件后，执行该 job：

```bash
cd datax/bin
python datax.py ../job/mysqljob.json

```

### 执行结果

1. 经过一段时间的等待，DataX 任务执行结束会有一个简单的任务报告，包含任务平均流量、记录写入速度和读写失败总数等信息。
 2. 最后可以到目标端 OceanBase MySQL 库检查该表数据是否迁移成功：

```sql
obclient> select * from DEPARTMENTS;
+---------+-----------+---------------+--------------+
| dept_id | dept_name | dept_location | dept_manager |
+---------+-----------+---------------+--------------+
|       1 | 人事部    | 北京市        | 王经理       |
|       2 | 财务部    | 上海市        | 张经理       |
|       3 | 市场部    | 广州市        | 李经理       |
|       4 | 研发部    | 深圳市        | 赵经理       |
|       5 | 销售部    | 成都市        | 刘经理       |
|       6 | 运营部    | 武汉市        | 周经理       |
|       7 | 客服部    | 南京市        | 黄经理       |
|       8 | 技术部    | 天津市        | 陈经理       |
|       9 | 采购部    | 杭州市        | 钱经理       |
|      10 | 翻译部    | 重庆市        | 孙经理       |
+---------+-----------+---------------+--------------+
10 rows in set (0.05 sec)

```

## 适用版本

OceanBase V4.0 之前的所有版本。DataX 工具的最新版本（[下载地址](https://datax-opensource.oss-cn-hangzhou.aliyuncs.com/202303/datax.tar.gz)）。

## 注意事项

1. DataX 不支持迁移表结构，只支持迁移数据，所以目标端的表必须事先存在。并且源表/目标表都可以是无主键表。
 2. DataX 迁移数据以任务的形式，每个任务只处理一张表，每个任务有一个 JSON 格式的配置文件。
 3. MySQL 数据库 JDBC 连接需要加 `useSSL=false`，否则会出现如下报错：DataX 无法连接对应的数据库，可能原因是：1）配置的 ip/port/database/jdbc 错误，无法连接。2）配置的 username/password 错误，鉴权失败。请和 DBA 确认该数据库的连接信息是否正确。
 4. 尽量选用最新版本的 DataX 软件，旧版本 writer 存在问题，数据迁移时可能会报 `java.lang.NumberFormatException: multiple points` 错误。
 5. 如果 OceanBase 通过 OBProxy 连接的话，用户名必须带上 `#集群`，否则迁移时会连接不上。可能出现如下报错：`Access denied for user 'root@mysql'@'xx.xx.xx.xx' (using password: YES)`。

上一篇

[DataX 抽取 OceanBase 数据慢优化](https://www.oceanbase.com/knowledge-base/oceanbase-database-1000000003899270)

下一篇

[如何使用 DataX 工具迁移 Oracle 数据到 OceanBase](https://www.oceanbase.com/knowledge-base/oceanbase-database-1000000006820459) ![有帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*y6ocSqN8cqsAAAAAAAAAAAAAARQnAQ)![无帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*BG9IQJyLHF8AAAAAAAAAAAAAARQnAQ)![反馈](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*eTWdQKCRKHwAAAAAAAAAAAAAARQnAQ)[AI](https://www.oceanbase.com/obi) 咨询热线
